筆記

//網絡請求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];

}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容