iOS app 版本更新

2015年12月15日星期二

关于苹果更新版本的思路

  • 1 根据已发布到app store上的应用版本号与已安装的APP版本号比较更新
    检测更新方法的优点是:检测版本号是实时同步的;缺点是:苹果网络不稳定,检测更新延时严重,部分APP获取不到任何参数。

  • 2 获取自己服务器的APP版本号与已安装的APP版本号比较
    检测更新方法的优点是:检测更新速度快、检测稳定;缺点是:和app store上的应用版本号不同步(app上架需要审核时间,不确定什么时候成功更新到app store上)

    总结

    如果版本更新比较快,建议使用自定义接口,每次进入app的时候进行判断服务器的版本号是否与当前的版本号是一致的。如果版本更新比较慢,或者对更新的要求不高可以省略 或者使用app store的更新,毕竟苹果官方是支持自动更新版本的(连上wifi,苹果就自动更新应用···挺好的)。

无码无真相

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
 //通过同步请求,解析json数据,得到了数据
NSError *error;
NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appID];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *appInfoDic = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

if (error) {
MMLog(@"error:%@",[error description]);
[MMHUD HUD:@"暂无更新"];
return;
}
NSArray *resultsArray = [appInfoDic objectForKey:@"results"];
if (![resultsArray count]) {
MMLog(@"error:resultsArray == nil");
[MMHUD HUD:@"暂无更新"];
return;
}

//这里需要,version,trackViewUrl,trackName
NSDictionary *infoDic = [resultsArray objectAtIndex:0];

NSString *latestVersion = [infoDic objectForKey:@"version"];
_trackViewUrl = [infoDic objectForKey:@"trackViewUrl"];//地址trackViewUrl
NSString *trackName = [infoDic objectForKey:@"trackName"];//trackName

//获取此应用的版本号
NSDictionary *infodict = [[NSBundle mainBundle] infoDictionary];

NSString *currentVersion = [infodict objectForKey:@"CFBundleVersion"];


double doubleCurrentVersion = [currentVersion doubleValue];
double doubleupdateVersion = [latestVersion doubleValue];

MMLog(@"%@",currentVersion);


//两个点的,最后那个是无效的
if (doubleCurrentVersion < doubleupdateVersion) {
NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@",trackName];
NSString *messageStr = [NSString stringWithFormat:@"发现新版本(%@),是否更新",latestVersion];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
alert.tag = 1;
[alert show];
}else{
NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@",trackName];
NSString *messageStr = [NSString stringWithFormat:@"暂无新版本"];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
alert.tag = 2;
[alert show];
}

#pragma mark - alertviewshow

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (alertView.tag == 1) {
//如果有新的版本,那么久跳转至下载页面,这里用到了trackViewUrl,trackViewUrl是全路径,直接请求。appURL
MMLog(@"直接请求appurl");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:_trackViewUrl]];
}
}

评论