場景
在我們使用應用時,一打開應用,如果此應用有新的版本,常常能在應用中給出提示,是否要更新此應用。所以,我們就來看看,版本更新是如何實現的。
應用
1
2
3
4
5
|
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數據,得到了數據。
1
2
3
|
NSString
*latestVersion = [releaseInfo objectForKey:@ "version" ]; NSString
*trackViewUrl1 = [releaseInfo objectForKey:@ "trackViewUrl" ]; //地址trackViewUrl NSString
*trackName = [releaseInfo objectForKey:@ "trackName" ]; //trackName |
獲取此應用的版本號
1
|
NSString
*currentVersion = [infoDict objectForKey:@ "CFBundleVersion" ]; |
通過latestVersion和currentVersion的比較,來判斷是否有新的更新。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
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是全路徑,直接請求。
1
|
[[UIApplication
sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]]; |
好的,這就是版本更新的全部步驟