获得app的版本号和名称以及检查更新版本提示

一. 获取版本号和应用名称

线上app都有一个版本号,我们也经常会发现在设置中会有一个当前应用的版本号

iOS的版本号,一个叫做Version,一个叫做Build,这两个值都可以在Xcode 中选中target,点击General后看到。 Version在plist文件中的key是“CFBundleShortVersionString”,和AppStore上的版本号保持一致,Build在plist中的key是“CFBundleVersion”,代表build的版本号。

如何用代码获得这个版本号呢?

代码实现获得应用的Verison号:

[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

获得build号:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]

顺便加一个获取app名称的代码:

 NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];  

  1. 二.版本检查更新

-(void)getUpdate

{

    NSDictionary *infoDict = [[NSBundlemainBundleinfoDictionary];

    NSString *nowVersion = [infoDictobjectForKey:@"CFBundleVersion"];

   

    NSURL url = [NSURLURLWithString:@"http://itunes.apple.com/lookup?id=*******"];

    NSString * file =  [NSStringstringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];

    //"version":"1.0"

    NSRange substr = [file rangeOfString:@"\"version\":\""];

    NSRange range1 = NSMakeRange(substr.location+substr.length,10);

    NSRange substr2 =[file rangeOfString:@"\"" options:NULL range:range1];

    NSRange range2 = NSMakeRange(substr.location+substr.length, substr2.location-substr.location-substr.length);

    NSString *newVersion =[filesubstringWithRange:range2];

    if([nowVersion isEqualToString:newVersion]==NO)

    {

        UIAlertView *alert = [[UIAlertViewallocinitWithTitle:nilmessage:@"版本有更新"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];

       [alert show];

       [alert release];

    } else {

       UIAlertView *alert = [[UIAlertViewallocinitWithTitle:nilmessage:@"已是最新版本"delegate:selfcancelButtonTitle:@"确定"otherButtonTitles:nil];

       [alert show];

       [alert release];

    }

   

}

- (void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if(buttonIndex==1)

{

     //link地址,下载地址

       NSURL url = [NSURLURLWithString:@"https://itunes.apple.com/us/app/***-***-***/id*******?ls=1&mt=8"];

       [[UIApplication sharedApplication]openURL:url];

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值