ios 實現版本更新檢查

    注:這裏網絡請求用的是第三方框架:SVHTTPRequest網絡

/*app

  第一步: 根據應用名稱搜索應用,而後根據應用綁定的ID在結果中篩選出咱們要找的應用,並取出應用的AppID框架

*/ ssh

- (void)getAppID {url

    processView  = [[UIProcessViewalloc]initWithFrame:self.view.framewithText:@"檢測中..."];spa

    [processViewstartAnimating];orm

    [self.viewaddSubview:processView];get

    [SVHTTPRequestPOST:@"http://itunes.apple.com/search"string

             parameters:[[NSDictionaryalloc] initWithObjectsAndKeys:APP_NAME,@"term",@"software",@"entity",nil]it

             completion:^(id response, NSHTTPURLResponse *urlResponse, NSError *error) {

                 

                 if (!error&&[urlResponse statusCode]==200) {

                     NSData *data = (NSData *)response;

                     id res = [NSJSONSerializationJSONObjectWithData:data options:NSJSONReadingMutableContainerserror:nil];

                     NSLog(@"res.class==%@",[res class]);

                     NSLog(@"res == %@",res);

                     NSLog(@"results class == %@",[[res objectForKey:@"results"]class]);

                     NSArray *arr = [res objectForKey:@"results"];

                     for (id config in arr)

                     {

                         NSString *bundle_id = [config valueForKey:@"bundleId"];

                         if ([bundle_id isEqualToString:APP_BUNDLE_IDENTIFIER]) {

                             [processView stopAnimating];

                             app_id  = [config valueForKey:@"trackId"];

                             updateURL = [config valueForKey:@"trackViewUrl"];

                             NSString *app_Name = [config valueForKey:@"trackName"];

                             NSString *version = [config valueForKey:@"version"];

                             NSLog(@"app_id == %@,app_Name == %@,version == %@",app_id,app_Name,version);

                             [self checkUpdate:version];

                         }

                     }

                 } else {

                     [processView stopAnimating];

                     [CTCommonUtilsshowAlertViewOnView:self.viewwithText:@"檢測失敗,當前無網絡鏈接!"];

                 }

             }];

}

 

/*

  第二步:經過比較從App Store獲取的應用版本與當前程序中設定的版本是否一致,而後判斷版本是否有更新

*/

- (void)checkUpdate:(NSString *)versionFromAppStroe {

    NSDictionary *infoDict = [[NSBundlemainBundle] infoDictionary];

    NSString *nowVersion = [infoDict objectForKey:@"CFBundleVersion"];

    NSLog(@"nowVersion == %@",nowVersion);

    [processViewstopAnimating];

    //檢查當前版本與appstore的版本是否一致

    if (![versionFromAppStroe isEqualToString:nowVersion])

    {

        UIAlertView *createUserResponseAlert = [[UIAlertView alloc] initWithTitle:@"提示" message: @"有新的版本可供下載" delegate:self cancelButtonTitle:@"下次再說" otherButtonTitles: @"去下載", nil];

        [createUserResponseAlert show];

    } else {

        [CTCommonUtilsshowAlertViewOnView:self.viewwithText:@"暫無新版本"];

    }

}

#pragma mark - AertView delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

    if (buttonIndex == 1) {

   //去appstore中更新

        //方法一:根據應用的id打開appstore,並跳轉到應用下載頁面

        //NSString *appStoreLink = [NSString stringWithFormat:@"http://itunes.apple.com/cn/app/id%@",app_id];

        //[[UIApplication sharedApplication] openURL:[NSURL URLWithString:appStoreLink]];

        

        //方法二:直接經過獲取到的url打開應用在appstore,並跳轉到應用下載頁面

        [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:updateURL]];

 

    } else if (buttonIndex == 2) {

        //去itunes中更新

        [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"itms://itunes.apple.com/cn/app/guang-dian-bi-zhi/id511587202?mt=8"]];

    }

 

}

#pragma mark -

相關文章
相關標籤/搜索