iOS檢查版本更新
場景
在我們使用應用時,一打開應用,如果此應用有新的版本,常常能在應用中給出提示,是否要更新此應用。所以,我們就來看看,版本更新是如何實現的。
應用
蘋果給了我們一個接口,能根據應用id請求一些關于應用的信息。我們可以根據返回的信息,來判斷版本是否和應用的版本一致,如果不一致,那么就出現新的版本了。這時,就需要向用戶提醒有新的版本,需要更新。具體步驟如下:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]]; [request setHTTPMethod:@"GET"]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
這里,我們通過同步請求,解析json數據,得到了數據。
好的,我們這里需要,version,trackViewUrl,trackName。
NSString *latestVersion = [releaseInfo objectForKey:@"version"]; NSString *trackViewUrl1 = [releaseInfo objectForKey:@"trackViewUrl"];//地址trackViewUrl NSString *trackName = [releaseInfo objectForKey:@"trackName"];//trackName
獲取此應用的版本號
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
通過latestVersion和currentVersion的比較,來判斷是否有新的更新。
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary]; NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"]; double doubleCurrentVersion = [currentVersion doubleValue]; if (doubleCurrentVersion < doubleUpdateVersion) { UIAlertView *alert; alert = [[UIAlertView alloc] initWithTitle:trackName message:@"有新版本,是否升級!" delegate: self cancelButtonTitle:@"取消" otherButtonTitles: @"升級", nil]; alert.tag = 1001; [alert show]; } else{ UIAlertView *alert; alert = [[UIAlertView alloc] initWithTitle:trackName message:@"暫無新版本" delegate: nil cancelButtonTitle:@"好的" otherButtonTitles: nil, nil]; [alert show]; }
如果有新的版本,那么就跳轉至下載頁面,這里就用到了trackViewUrl,trackViewUrl是全路徑,直接請求。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]];
好的,這就是版本更新的全部步驟。
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!