iOS自動檢測版本更新

來源:互聯網
上載者:User

標籤:ios自動檢測版本更新   ios應用自動檢測版本更新   iosapp自動檢測版本更新   

雖然蘋果官方是不允許應用自動檢測更新,提示使用者下載,因為蘋果會提示你有多少個軟體需要更新,但是有的時候提示使用者一下有新版還是很有必要的。

 

首先說一下原理:

每個上架的蘋果應用程式,都會有一個應用程式的ID,根據這個ID我們就可以擷取到當前程式的最新版本號碼,然後和自己的版本號碼作比較,如果一樣的話就是最新版,反之就不是新版,就可以提示使用者來手動下載最新版的程式。因為有ID所以就可以定位到這個APP,點擊下載即可。

 

源碼:

一般建議檢測更新的代碼放到首頁控制器裡。

首先還要匯入一個標頭檔用來開啟AppStore下載更新

//AppStore#import <StoreKit/StoreKit.h>

接著還有代理

SKStoreProductViewControllerDelegate

然後開始檢測更新

    //檢測版本,版本更新    NSError *error;    NSString *urlStr = @"http://itunes.apple.com/lookup?id=上架AppID";    NSURL *url = [NSURL URLWithString:urlStr];    NSURLRequest *request = [NSURLRequest requestWithURL:url];    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];    NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];    if (error)    {        return;    }    NSArray *resultArray = [appInfoDict objectForKey:@"results"];     if (![resultArray count])    {        return;    }        NSDictionary *infoDict = [resultArray objectAtIndex:0];    //擷取伺服器上應用的最新版本號碼    NSArray* arr=[infoDict[@"version"] componentsSeparatedByString:@"."];    NSInteger updateVersion=0;       for (int i=0; i<arr.count; i++)    {                if(i==0)        {            updateVersion+=[arr[i] integerValue]*1000;        }        else if (i==1)        {            updateVersion+=[arr[i] integerValue]*100;        }        else if (i==2)        {            updateVersion+=[arr[i] integerValue]*10;        }        else if (i==3)        {            updateVersion+=[arr[i] integerValue]*1;        }    }    NSString *trackName = infoDict[@"trackName"];    _trackViewUrl = infoDict[@"trackViewUrl"];//擷取當前裝置中應用的版本號碼    NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];    NSArray* arr2=[[infoDic objectForKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."];    NSInteger currentVersion=0;        for (int i=0; i<arr2.count; i++)    {        if(i==0)        {            currentVersion+=[arr2[i] integerValue]*1000;        }        else if (i==1)        {            currentVersion+=[arr2[i] integerValue]*100;        }        else if (i==2)        {            currentVersion+=[arr2[i] integerValue]*10;        }        else if (i==3)        {            currentVersion+=[arr2[i] integerValue]*1;        }    }        //判斷兩個版本是否相同    if (currentVersion < updateVersion)    {        NSString *titleStr = [NSString stringWithFormat:@"檢查更新:%@", trackName];        NSString *messageStr = [NSString stringWithFormat:@"發現新版本%@,是否更新", infoDict[@"version"]];        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升級", nil];     //為了區分其他彈出框而已        alert.tag = 1112427256;        [alert show];            }

 

接著就是使用者更不更的問題了

 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {        if (alertView.tag == 1112427256)    {        if (buttonIndex == 1)        {            //點擊”升級“按鈕,就從開啟app store上應用的詳情頁面            SKStoreProductViewController *storeProductVC = [[SKStoreProductViewController alloc] init];            storeProductVC.delegate = self;            NSDictionary *dict = [NSDictionary dictionaryWithObject:@"上架AppID" forKey:SKStoreProductParameterITunesItemIdentifier];            [storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error)             {                 if (result)                 {                     [self presentViewController:storeProductVC animated:YES completion:nil];                 }             }];        }    }}

還有就是使用者開啟AppStore但是沒有下載就返回回來的狀況

- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{    [viewController dismissViewControllerAnimated:YES completion:nil];}

OK到這裡就結束了。這樣的話就可以檢測App是不是最新版了,而且使用者也能即時看到,最關鍵的是蘋果審核還能通過。




本文出自 “紅角羚羊” 部落格,請務必保留此出處http://2254359459.blog.51cto.com/10776102/1876839

iOS自動檢測版本更新

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.