.h文件中
<UIAlertViewDelegate>
.m文件中
#import "SBJson.h" //解析sbjson 數據
- (void)viewDidLoad { [super viewDidLoad]; ⋯⋯ [self checkVersion]; //檢測升級 }
#pragma mark - 實現升級功能 //檢測軟件是否需要升級 -(void)checkVersion { NSString *newVersion; NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/cn/lookup?id=692579125"]; //通過url獲取數據 NSString *jsonResponseString = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; NSLog(@"通過appStore獲取的數據是:%@",jsonResponseString); //解析json數據爲數據字典 NSDictionary *loginAuthenticationResponse = [self dictionaryFromJsonFormatOriginalData:jsonResponseString]; //從數據字典中檢出版本號數據 NSArray *configData = [loginAuthenticationResponse valueForKey:@"results"]; for(id config in configData) { newVersion = [config valueForKey:@"version"]; } NSLog(@"通過appStore獲取的版本號是:%@",newVersion); //獲取本地軟件的版本號 NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"]; NSString *msg = [NSString stringWithFormat:@"你當前的版本是V%@,發現新版本V%@,是否下載新版本?",localVersion,newVersion]; //對比發現的新版本和本地的版本 if ([newVersion floatValue] > [localVersion floatValue]) { UIAlertView *createUserResponseAlert = [[UIAlertView alloc] initWithTitle:@"升級提示!" message:msg delegate:self cancelButtonTitle:@"下次再說" otherButtonTitles: @"現在升級", nil]; [createUserResponseAlert show]; [createUserResponseAlert release]; } } //響應升級提示按鈕 - (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex { //如果選擇“現在升級” if (buttonIndex == 1) { //打開iTunes 方法一 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/wan-zhuan-quan-cheng/id692579125?mt=8"]]; /* // 打開iTunes 方法二:此方法總是提示“無法連接到itunes”,不推薦使用 NSString *iTunesLink = @"itms-apps://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftwareUpdate?id=692579125&mt=8"; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]]; */ } } #pragma mark - 輔助方法:將json 格式的原始數據轉解析成數據字典 //將json 格式的原始數據轉解析成數據字典 -(NSMutableDictionary *)dictionaryFromJsonFormatOriginalData:(NSString *)str { SBJsonParser *sbJsonParser = [[SBJsonParser alloc]init]; NSError *error = nil; //添加autorelease 解決 內存泄漏問題 NSMutableDictionary *tempDictionary = [[[NSMutableDictionary alloc]initWithDictionary:[sbJsonParser objectWithString:str error:&error]]autorelease]; return tempDictionary; }