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]];
}
}
}