//網絡請求app 的信息
-(void)VersonUpdate{
//定義的app的地址
NSString *urld = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",@"1242726744"];
//網絡請求app的信息,主要是取得我說需要的Version
NSURL *url = [NSURL URLWithString:urld];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
if (data) {
//data是有關于App所有的信息
NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) {
[receiveStatusDic setValue:@"1" forKey:@"status"];
[receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"]? forKey:@"version"];
//請求的有數據,進行版本比較
[self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}];
[task resume];
}
//獲取自身的版本號并與AppStore比較
-(void)receiveData:(id)sender
{
//獲取APP自身版本號
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
if ([localArray[0] intValue] <? [versionArray[0] intValue]) {
[self updateVersion];
}else if ([localArray[0] intValue]? ==? [versionArray[0] intValue]){
if ([localArray[1] intValue] <? [versionArray[1] intValue]) {
[self updateVersion];
}else if ([localArray[1] intValue] ==? [versionArray[1] intValue]){
if ([localArray[2] intValue] <? [versionArray[2] intValue]) {
[self updateVersion];
}
}
}
}
}
//根據比較的結果,實現彈窗
-(void)updateVersion{
NSString *msg = [NSString stringWithFormat:@"更新最新版本,優惠信息提前知"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升級提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"現在升級"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
//? ? ? ? "itms-apps ://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8"
//? ? ? ? NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/app/%E5%A8%84%E5%A4%8F%E7%A4%BE%E5%8C%BA/id1242726744?mt=8"]];
NSURL * url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/cn/app/%E5%A8%84%E5%A4%8F%E7%A4%BE%E5%8C%BA/id1242726744?mt=8"];
[[UIApplication sharedApplication]openURL:url];
}];
[alertController addAction:otherAction];
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}