1. 程式人生 > >iOS ——版本更新檢測 判斷應用版本和升級提示

iOS ——版本更新檢測 判斷應用版本和升級提示

ios中應用的版本判斷有兩種方法:

1.將你的應用版本號同步在你自己的伺服器上,開啟程式後去自己的伺服器獲取版本號和手機上的應用版本號做比較,然後去appstore升級

2.通過url獲取appstore上的最新版本資訊,然後和手機上的程式版本號做比較,判斷是否升級。

最常用的就是方法2,下面講講方法2的實現過程。

第一步是去獲取appstore上你的應用的版本資訊,需要用到的url    #define APP_URL    @"http://itunes.apple.com/lookup?id=662004496"

//連網獲取appstore 裡軟體的版本號與本地的版本號對比。當前版本號< appstore 裡的版本,提示更新。

    [NetWork getWithURL:homeUrl params:nil success:^(NSDictionary * responseObject) {
        NSArray *infoArray = [responseObject objectForKey:@"results"];
        if (infoArray.count > 0) {
            
            NSDictionary* releaseInfo =[infoArray objectAtIndex:0];
            
            //appstore 版本
            NSString* appStoreVersion = [releaseInfo objectForKey:@"version"];
            
            //當前版本
            NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
            NSString *currentVersion = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
            
            if ([currentVersion compare:appStoreVersion]==NSOrderedAscending)


            {
                
                self.trackStr=releaseInfo[@"trackViewUrl"];
                
                UIAlertView *alertView=[[UIAlertView alloc]initWithTitle:@"發現新版本" message:@"有新版本了,趕快更新看看吧!" delegate:self cancelButtonTitle:@"稍後再說" otherButtonTitles:@"立即升級", nil];
                [alertView show];
                
            }
         
        }
      
    } failure:^(NSError *error) {
        
    }];
    
    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==1)
    {
        NSURL *trackURL=[NSURL URLWithString:self.trackStr];
        [[UIApplication sharedApplication]openURL:trackURL];
        
        
    }
}