今天遇到了关于版本升级的问题,弹出提示框给用户提醒升级,一个版本只提醒一次
http://www.jianshu.com/p/62a18e8ed92b 这个是原文,但是有错误 这里改动了一下加了些需求
#define kAPP_URL [AppDelegate isLanguageEnglish][email protected]"http://itunes.apple.com/lookup?id=":@"http://itunes.apple.com/cn/lookup?id="
#define kAppId @"1111111111"//appID
- (void)updateApp
{
NSError *error;
NSString *urlStr = [NSString stringWithFormat:@"%@%@",kAPP_URL,kAppId];
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:NSJSONReadingMutableContainers 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"];
_trackViewUrl = infoDict[@"trackViewUrl"];
//获取当前设备中应用的版本号
NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
NSArray<NSString*> *currentArray = [currentVersion componentsSeparatedByString:@"."];
NSArray<NSString*> *updateArray = [updateVersion componentsSeparatedByString:@"."];
NSMutableString *currentString = [NSMutableString string];
for (NSString *str in currentArray) {
NSString * tempstr = [NSString stringWithFormat:@"%03d",[str intValue]];
[currentString appendString:tempstr];
}
NSMutableString *updateString = [NSMutableString string];
for (NSString *str in updateArray) {
NSString * tempstr = [NSString stringWithFormat:@"%03d",[str intValue]];//补全位数再比较大小
[updateString appendString:tempstr];
}
if ([[NSUserDefaults standardUserDefaults]objectForKey:updateString]) {
return;
}else{
[[NSUserDefaults standardUserDefaults]setObject:@0 forKey:updateString];//一个版本只提醒一次
}
//判断两个版本是否相同
if ([currentString longLongValue] < [updateString longLongValue]) {
NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@", trackName];
NSString *messageStr = [NSString stringWithFormat:@"发现新版本 %@ 是否更新\n%@", updateVersion,infoDict[@"releaseNotes"]];//版本更新内容
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
alert.tag = [kAppId intValue];
[alert show];
}
}
//判断用户点击了哪一个按钮
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (alertView.tag == [kAppId intValue]) {
if (buttonIndex == 1) { //打开app store上应用的详情页面
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.trackViewUrl]];
}
}
}