iOS App檢查更新

場景

在我們使用應用時,一打開應用,如果此應用有新的版本,常常能在應用中給出提示,是否要更新此應用。所以,我們就來看看,版本更新是如何實現的。

應用

蘋果給了我們一個接口,能根據應用id請求一些關於應用的信息。我們可以根據返回的信息,來判斷版本是否和應用的版本一致,如果不一致,那麼就出現新的版本了。這時,就需要向用戶提醒有新的版本,需要更新。具體步驟如下:
?
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數據,得到了數據。
好的,我們這裏需要,version,trackViewUrl,trackName。
?
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]];

好的,這就是版本更新的全部步驟
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章