一. 获取版本号和应用名称
线上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"];
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"]
顺便加一个获取app名称的代码:
NSString *app_Name = [infoDictionary objectForKey:@"CFBundleDisplayName"];
- 二.版本检查更新
-(void)getUpdate
{
NSDictionary *infoDict = [[NSBundlemainBundle] infoDictionary];
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 = [[UIAlertViewalloc] initWithTitle:nilmessage:@"版本有更新"delegate:selfcancelButtonTitle:@"取消"otherButtonTitles:@"更新",nil];
[alert show];
[alert release];
} else {
UIAlertView *alert = [[UIAlertViewalloc] initWithTitle: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];
}
}