在iOS中,有时是需要写个更新提示的,经过自己晚上找的和实际实验的,总结如下:
在这个方法里
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//版本更新
[self VersionButton];
}然后获取想在运行的版本:
-(void)VersionButton{
//获取发布版本的verion
NSString *string = [NSString stringWithContentsOfURL:[NSURL URLWithString:[NSStringstringWithFormat:@"http://itunes.apple.com/lookup?id=%@",APPID]] encoding:NSUTF8StringEncodingerror:nil];
NSRange range;
range = [string rangeOfString:@"\"version\":"];
NSString *appVersion = [string substringWithRange:NSMakeRange(range.location + range.length+1, 5)];
[self checkAppUpdate:appVersion];
}
我这样是写的比较死的,因为我的版本号设置是1.0.1这样子的,自己可以灵活获取。然后在获取线上版本号来进行比较:
//当前版本与上线版本作比较
-(void)checkAppUpdate:(NSString *)appVersion{
//这是Build
NSString *version = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleVersion"];
//这是version
// NSString *version =[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
// NSString *appInfo1 = [appInfo substringFromIndex:[appInfo rangeOfString:@"\"version\":"].location+10];
// appInfo1 = [[appInfo1 substringToIndex:[appInfo1 rangeOfString:@","].location]stringByReplacingOccurrencesOfString:@"\"" withString:@""];
NSLog(@"新版本:%@,当前版本:%@ ",appVersion,version);
NSArray *a2 = [appVersion componentsSeparatedByString:@"."];
NSArray *a1 = [version componentsSeparatedByString:@"."];
for (int i = 0; i < [a1 count]; i++) {
if ([a2 count] > i) {
if ([[a1 objectAtIndex:i] floatValue] < [[a2 objectAtIndex:i] floatValue]) {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSStringstringWithFormat:@"新版本%@已发布!",appVersion] message:@"" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
alert.delegate = self;
[alert addButtonWithTitle:@"前往更新"];
[alert show];
alert.tag = 20;
}
else if ([[a1 objectAtIndex:i] floatValue] > [[a2 objectAtIndex:i] floatValue])
{
}
}
else
{
}
}
// float doubleVersion = [version floatValue];
// float doubleAppinfo1 = [appVersion floatValue];
// NSLog(@"新版本:%f,当前版本:%f ",doubleVersion,doubleAppinfo1);
// if (doubleAppinfo1 > doubleVersion) {
// UIAlertView *alert = [[UIAlertView alloc]initWithTitle:[NSString stringWithFormat:@"新版本%@已发布!",appVersion] message:@"" delegate:self cancelButtonTitle:@"知道了" otherButtonTitles:nil];
// alert.delegate = self;
// [alert addButtonWithTitle:@"前往更新"];
// [alert show];
// alert.tag = 20;
// }else{
[[[UIAlertView alloc]initWithTitle:@"已是最高版本" message:nil delegate:self cancelButtonTitle:@"知道了" otherButtonTitles: nil] show];
// }
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
if (buttonIndex == 1&&alertView.tag == 20) {
NSString *url = @"https://itunes.apple.com/us/app/he-yi-hui/id1059964988?l=zh&ls=1&mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
}
上面的url是你app所在App Store的页面。