两步完成iOS App版本更新提示

最近app里集合了更新

- (void)updateApp
{
   NSError *error;
  //kAPP_URL : http://itunes.apple.com/lookup?id=
  //kAppId : 在iTunes connect上申请的APP ID

NSString *appID;
NSString *identifier = [[NSBundle mainBundle] bundleIdentifier];

appID = @"108873****";

NSString *appUrl = @"http://itunes.apple.com/lookup?id=";
NSString *urlStr = [NSString stringWithFormat:@"%@%@", appUrl, appID];
NSURL *url = [NSURL URLWithString:urlStr];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];

NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];

if (error) {
    NSLog(@"%@", error.description);
    return;
}

NSArray *resultArray = [appInfoDict objectForKey:@"results"];

if (![resultArray count]) {
    NSLog(@"error : resultArray == nil");
    return;
}

NSDictionary *infoDict = [resultArray objectAtIndex:0];
//获取服务器上应用的最新版本号
NSString *updateVersion = infoDict[@"version"];
NSString *trackName = infoDict[@"trackName"];
NSString *note = [infoDict objectForKey:@"releaseNotes"];

_trackViewUrl = infoDict[@"trackViewUrl"];

//获取当前设备中应用的版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"] ;

//判断两个版本是否相同
if ([updateVersion compare:currentVersion options:NSNumericSearch] == NSOrderedDescending) {
    NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@", trackName];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:note delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
    
    alert.tag = 999;
    [alert show];
    
  }

}
//判断用户点击了哪一个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:      (NSInteger)buttonIndex
{
if (alertView.tag == 999) {
    if (buttonIndex == 1) { //点击”升级“按钮,就从打开app store上应用的详情页面
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:_trackViewUrl]];
    }
}
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,441评论 25 708
  • 一、新版本上架 1.创建证书 账号什么的就不说了哈,没有的话要公司买。 1.1 钥匙串 打开钥匙串,然后点击钥匙串...
    百里玄卿阅读 44,857评论 24 48
  • 出院…… “啊!要出院啦!真好啊!”依紫倩。 依紫倩坐起来,却看到亦紫轩爬在床上,熟睡着…… “啊---!...
    娜娜公主阅读 282评论 0 1
  • 我的初恋 叫做袁紫晴,她是我中学时的一个同学,文静,不爱说话,是个很内向的女孩。 记得她 刚来我们...
    江小神阅读 532评论 0 4
  • 生活中或工作中都会有朋友,朋友要经常联系,积极关心帮助朋友,等我们需要朋友帮助的时候她也会第一时间帮助我们。朋友嘛...
    Ding欣欣阅读 147评论 0 0