IOS判斷app在appstore是否有可用的更新

iTunes能夠提供app的版本信息,主要經過appid獲取,如 http://itunes.apple.com/lookup?id=946449501,使用時只須要到iTunes查找本身的appid,修改爲本身的appid便可json

使用HTTP模式讀取此連接能夠獲取app信息的json字符串app

貼出部分代碼spa

-(void)checkVersion
{
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:strURL]];//strURL爲你的appid地址
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startAsynchronous];
}

-(void)requestFinished:(ASIHTTPRequest *)request
{
    NSString *recStr = [[NSString alloc] initWithData:request.responseData encoding:NSUTF8StringEncoding];
    recStr = [recStr stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];//返回的字符串有前面有不少換行符,須要去除一下
    NSDictionary *resultDic = [JSONHelper DeserializerDictionary:recStr];//jsonhelper是我封裝的json解析類,你能夠使用本身方式解析
    
    NSArray *infoArray = [resultDic objectForKey:@"results"];
    if (infoArray.count > 0) {
        
        NSDictionary* releaseInfo =[infoArray objectAtIndex:0];
        NSString* appStoreVersion = [releaseInfo objectForKey:@"version"];
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        
        NSArray *curVerArr = [currentVersion componentsSeparatedByString:@"."];
        NSArray *appstoreVerArr = [appStoreVersion componentsSeparatedByString:@"."];
        BOOL needUpdate = NO;
        //比較版本號大小
        int maxv = (int)MAX(curVerArr.count, appstoreVerArr.count);
        int cver = 0;
        int aver = 0;
        for (int i = 0; i < maxv; i++) {
            if (appstoreVerArr.count > i) {
                aver = [NSString stringWithFormat:@"%@",appstoreVerArr[i]].intValue;
            }
            else{
                aver = 0;
            }
            if (curVerArr.count > i) {
                cver = [NSString stringWithFormat:@"%@",curVerArr[i]].intValue;
            }
            else{
                cver = 0;
            }
            if (aver > cver) {
                needUpdate = YES;
                break;
            }
        }
        
        //若是有可用的更新
        if (needUpdate){
            
            trackViewURL = [[NSString alloc] initWithString:[releaseInfo objectForKey:@"trackViewUrl"]];//trackViewURL臨時變量存儲app下載地址,能夠讓app跳轉到appstore
            UIAlertView* alertview =[[UIAlertView alloc] initWithTitle:@"版本升級" message:[NSString stringWithFormat:@"發現有新版本,是否升級?"] delegate:self cancelButtonTitle:@"暫不升級" otherButtonTitles:@"立刻升級", nil];
            [alertview show];
            
        }
        
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1){
        UIApplication *application = [UIApplication sharedApplication];
        [application openURL:[NSURL URLWithString:trackViewURL]];
    }
}
相關文章
相關標籤/搜索