iOS版本更新

版本更新
#pragma mark - 檢查版本更新

- (void)checkNewVersion {

//    NSString *url = @"https://itunes.apple.com/cn/app/ya-zi-kan-hu-ji-qi-ren/id1067342148?mt=8";

     NSString *url = @"http://itunes.apple.com/cn/lookup?id=1067342148";  //  ******************根據自己的應用改URL ******************

   

    [GJWHttpTool getWithURL:url params:nil success:^(id json) {

        // 請求成功

        // 字典接收請求到的JSon

        NSDictionary *reslut = [NSJSONSerialization JSONObjectWithData:json options:NSJSONReadingMutableContainers error:nil];

        NSDictionary *responseDict = [NSDictionary dictionaryWithDictionary:reslut];

        // 解析請求到的JSon

        NSArray *results =[responseDict objectForKey:@"results"];

        NSDictionary *finalDict = [results firstObject];

        // 獲取APP下載地址

        NSString *trackViewUrl =[finalDict objectForKey:@"trackViewUrl"];

        updateUrl = [NSURL URLWithString:trackViewUrl];

        // 獲取官網APP的版本號

        NSString *str1 = [finalDict objectForKey:@"version"];

        NSString *version1 = [str1 substringFromIndex:2];

        // 將版本號字符串轉換成float類型

        float newVersion = [version1floatValue];

       

        // 獲取本地項目版本號

        // 拿到項目的infoPlist文件中所有內容

        NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];

        // 獲取到當前工程版本號

        NSString *str2 = [infoDict objectForKey:@"CFBundleShortVersionString"];

        NSString *version2 = [str2 substringFromIndex:2];

        // 將版本號字符串轉換成float類型

        float localVersion =[version2 floatValue];

        // 對比兩處版本號

        if (newVersion >localVersion) {

 

        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"新版本 %@ 已發佈!",str1] delegate:self.class cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];

        alert.delegate = self;

        [alert addButtonWithTitle:@"前往更新"];

        [alert show];

        alert.tag = 20;

    } else {

        [[[UIAlertView alloc] initWithTitle:nil message:@"已是最高版本" delegate:self.class cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil] show];

    }

    

    } failure:^(NSError *error) {

        // 返回請求失敗的原因

        NSLog(@"NSError:%@", error);

    }];

   

   

}

 

#pragma mark – UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{

    if (buttonIndex ==1 & alertView.tag ==20) {


        [[UIApplication sharedApplication] openURL:updateUrl];

    }

}

發佈了37 篇原創文章 · 獲贊 9 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章