赫哥手打,喜欢就点赞下哈哈。
自己在做设置的时候遇到了问题,就是关于更新的,把自己的方法列一下。
这里先把你app的ID标进去:
http://itunes.apple.com/lookup?id=<Your app ID>
我先随机举例一个:
define APP_URL @"http://itunes.apple.com/cn/lookup?id=1093039842"
#pragma mark--检测版本号更新
-(void)checkVertion
{
//检测更新
AFHTTPRequestOperationManager *afManager = [AFHTTPRequestOperationManager manager];
[afManager POST:APP_URL parameters:nil success:^(AFHTTPRequestOperation * _Nonnull operation, id _Nonnull responseObject) {
NSLog(@"%@",responseObject);
/*responseObject是个字典{},有两个key
KEYresultCount = 1//表示搜到一个符合你要求的APP
results =()//这是个只有一个元素的数组,里面都是app信息,那一个元素就是一个字典。里面有各种key。其中有 trackName (名称)trackViewUrl = (下载地址)version (可显示的版本号)等等
*/
//具体实现为
NSArray *arr = [responseObject objectForKey:@"results"];
NSDictionary *dic = [arr firstObject];
NSString *versionStr = [dic objectForKey:@"version"];
NSString *trackViewUrl = [dic objectForKey:@"trackViewUrl"];
NSString *releaseNotes = [dic objectForKey:@"releaseNotes"];//更新日志
//NSString* buile = [[NSBundle mainBundle] objectForInfoDictionaryKey: (NSString*) kCFBundleVersionKey];build号
NSString* thisVersion = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
if ([self compareVersionsFormAppStore:versionStr WithAppVersion:thisVersion]) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:[NSString stringWithFormat:@"发现新版本:%@",versionStr] message:releaseNotes preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了取消");
}];
UIAlertAction *OKAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSLog(@"点击了知道了");
NSURL * url = [NSURL URLWithString:trackViewUrl];//itunesURL = trackViewUrl的内容
[[UIApplication sharedApplication] openURL:url];
}];
[alertVC addAction:cancelAction];
[alertVC addAction:OKAction];
[self presentViewController:alertVC animated:YES completion:nil];
}
} failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
NSLog(@"");
}];
}
//比较版本的方法,在这里用的是Version来比较的
- (BOOL)compareVersionsFormAppStore:(NSString*)AppStoreVersion WithAppVersion:(NSString*)AppVersion{
BOOL littleSunResult = false;
NSMutableArray* a = (NSMutableArray*) [AppStoreVersion componentsSeparatedByString: @"."];
NSMutableArray* b = (NSMutableArray*) [AppVersion componentsSeparatedByString: @"."];
while (a.count < b.count) { [a addObject: @"0"]; }
while (b.count < a.count) { [b addObject: @"0"]; }
for (int j = 0; j<a.count; j++) {
if ([[a objectAtIndex:j] integerValue] > [[b objectAtIndex:j] integerValue]) {
littleSunResult = true;
break;
}else if([[a objectAtIndex:j] integerValue] < [[b objectAtIndex:j] integerValue]){
littleSunResult = false;
break;
}else{
littleSunResult = false;
}
}
return littleSunResult;//true就是有新版本,false就是没有新版本
}
最后在需要的地方引用
[self checkVertion];
取消再弄个提示就行了。