iOS檢測更新APP

iOS APP檢查更新有兩種方法:

1、在某特定的服務(wù)器上,發(fā)布和存儲app最新的版本信息,需要的時候向該服務(wù)器請求查詢。
post 請求的url
"http://itunes.apple.com/search?term=你的應(yīng)用名稱&entity=software"

2、從app store上查詢,可以獲取到app的作者,連接,版本等。
post 請求的url
http://itunes.apple.com/lookup?id=你的應(yīng)用程序的ID (在蘋果iTunes connect上申請的APP ID)

兩個方法,都是拿到APP的詳細信息,之后在APP Store上下載,比較常規(guī)的方法是第二種,如果使用第一種方法的話,由于蘋果需要審核,中間會有時間差,這個時間不好把握。

APP的詳細信息獲取下來后是json格式的數(shù)據(jù),解析后的數(shù)據(jù),大概是這樣的:
{
resultCount = 1;
results = [ {
artistId = 開發(fā)者 ID;
artistName = 開發(fā)者名稱;
price = 0;
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = (
EN
);
trackCensoredName = 審查名稱;
trackContentRating = 評級;
trackId = 應(yīng)用程序 ID;
trackName = 應(yīng)用程序名稱";
trackViewUrl = 應(yīng)用程序介紹網(wǎng)址;
userRatingCount = 用戶評級;
userRatingCountForCurrentVersion = 1;
version = 版本號;
wrapperType = software;
}
]
}
我們可以從中獲取我們需要的信息。


這里使用的是第二種方法,app store上獲取詳細信息,代碼如下:(實際上用第一種方法,只是換了url而已)
- (void)updateApp
{
  // kAPP_URL : @"http://itunes.apple.com/lookup?id=";
//    kAppID : 在iTunes connect上申請的APP ID;
    NSString *urlStr = [NSString stringWithFormat:@"%@%@", kAPP_URL, kAppID];
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    //網(wǎng)絡(luò)請求
    NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    NSURLSessionTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        NSError *err;
        //NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
        NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&err];
    
        if (err) {
            NSLog(@"%@", err.description);
            return;
        }

        NSArray *resultArray = [appInfoDict objectForKey:@"results"];
        if (![resultArray count]) {
            NSLog(@"error : resultArray == nil");
            return;
        }
    
        NSDictionary *infoDict = [resultArray objectAtIndex:0];

        //獲取服務(wù)器上應(yīng)用的最新版本號
        NSString *updateVersion = infoDict[@"version"];
        NSString *trackName = infoDict[@"trackName"];

        //_trackViewUrl : 更新的時候用到的地址
        _trackViewUrl = infoDict[@"trackViewUrl"];

        //獲取當(dāng)前設(shè)備中應(yīng)用的版本號
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
    
    //判斷兩個版本是否相同
        if ([currentVersion doubleValue] < [updateVersion doubleValue]) {
            NSString *titleStr = [NSString stringWithFormat:@"檢查更新:%@", trackName];
            NSString *messageStr = [NSString stringWithFormat:@"發(fā)現(xiàn)新版本(%@),是否更新", updateVersion];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升級", nil];
            alert.tag = [kAppID intValue];
            [alert show];
        
        } else {  //版本號和app store上的一致
            NSString *titleStr = [NSString stringWithFormat:@"檢查更新:%@", trackName];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:@"暫無新版本" delegate:self cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
            alert.tag = [kAppID intValue] + 1;
            [alert show];
      }
   }];
    [task resume];
}
//判斷用戶點擊了哪一個按鈕
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == [kAppID intValue]) {
        if (buttonIndex == 1) { //點擊”升級“按鈕,就從打開app store上應(yīng)用的詳情頁面
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.trackViewUrl]];
        }
    }
 }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容