[置顶] 检查版本更新

1、

// FIR.im的自动更新

  

 NSString *bundleId = [[NSBundlemainBundle]infoDictionary][@"CFBundleIdentifier"];

    NSString *bundleIdUrlString = [NSStringstringWithFormat:@"http://api.fir.im/apps/latest/%@?api_token=71eb982be4f093719d2b7d985b236fb7&type=ios", bundleId];

    NSURL *requestURL = [NSURLURLWithString:bundleIdUrlString];

    NSURLRequest *request = [NSURLRequestrequestWithURL:requestURL];

    [NSURLConnectionsendAsynchronousRequest:requestqueue :[NSOperationQueuecurrentQueue]completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {

        if (connectionError) {

            //do something

            XNLog(@"版本检查失败");

        }else {

            NSError *jsonError =nil;

            id object = [NSJSONSerializationJSONObjectWithData:dataoptions:0error:&jsonError];

            if (!jsonError && [objectisKindOfClass:[NSDictionaryclass]]) {

                //do something

                //NSString * version=object[@"version"]; //对应 CFBundleVersion,对应Xcode项目配置"General"中的 Build

                NSString * versionShort=object[@"versionShort"];//对应 CFBundleShortVersionString,对应Xcode项目配置"General"中的 Version

                

                //NSString * localVersion=[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];

                NSString * localVersionShort=[[NSBundlemainBundle]infoDictionary][@"CFBundleShortVersionString"];

                nss_updateURL=object[@"update_url"];//如果有更新需要用Safari打开的地址

                //NSString *changelog=object[@"changelog"]; //如果有更新需要用Safari打开的地址

                if (![localVersionShort isEqualToString:versionShort]) {

                    [[AlertHandlersingleton]showConfirm_default_title:@"发现新版本"message:[NSStringstringWithFormat:@"发现新版本v%@",versionShort]delegate:self

                                                     cancelButtonTitle:@"忽略"otherButtonTitle:@"点击更新"tag:1];

                }

            }

        }

    }];



2、

<span style="font-size:18px;">if ([[CommFunsingleton]event_updateVersion] ==YES) {

            UIApplication *application = [UIApplicationsharedApplication];

            [applicationopenURL:[NSURLURLWithString:[UesrInfosingleton].updateURL]];

        }else{

            NSString * localVersionShort=[[NSBundlemainBundle]infoDictionary][@"CFBundleShortVersionString"];

            [[AlertHandlersingleton]showConfirm_default_title:[NSStringstringWithFormat:@"当前版本v%@", localVersionShort]message:@"当前已是最新版本"delegate:selfcancelButtonTitle:@"确定"otherButtonTitle:niltag:1];

        }


-(BOOL)event_updateVersion{

    // TODO:去掉版本检查(苹果审核不通过)

    [UesrInfosingleton].needUpdate =NO;

    returnNO;

    [UesrInfosingleton].needUpdate =YES;

    NSString *bundleId = [[NSBundlemainBundle]infoDictionary][@"CFBundleIdentifier"];

    NSString *bundleIdUrlString = [NSStringstringWithFormat:@"http://api.fir.im/apps/latest/%@?api_token=71eb982be4f093719d2b7d985b236fb7&type=ios", bundleId];

    NSURL *requestURL = [NSURLURLWithString:bundleIdUrlString];

    NSURLRequest *request = [NSURLRequestrequestWithURL:requestURL];

    [NSURLConnectionsendAsynchronousRequest:requestqueue :[NSOperationQueuecurrentQueue]completionHandler:^(NSURLResponse *response,NSData *data,NSError *connectionError) {

        if (connectionError) {

            //do something

            XNLog(@"版本检查失败");

        }else {

            NSError *jsonError =nil;

            id object = [NSJSONSerializationJSONObjectWithData:dataoptions:0error:&jsonError];

            if (!jsonError && [objectisKindOfClass:[NSDictionaryclass]]) {

                //do something

                //NSString * version=object[@"version"]; //对应 CFBundleVersion,对应Xcode项目配置"General"中的 Build

                NSString * versionShort=object[@"versionShort"];//对应 CFBundleShortVersionString,对应Xcode项目配置"General"中的 Version

                

                //NSString * localVersion=[[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"];

                NSString * localVersionShort=[[NSBundlemainBundle]infoDictionary][@"CFBundleShortVersionString"];

                NSString *nss_updateURL=object[@"update_url"];//如果有更新需要用Safari打开的地址

                //NSString *changelog=object[@"changelog"]; //如果有更新需要用Safari打开的地址

                if (![localVersionShort isEqualToString:versionShort]){

                    [UesrInfosingleton].needUpdate =YES;

                }else{

                    [UesrInfosingleton].needUpdate =NO;

                }

                [[UesrInfosingleton]setUpdateURL:nss_updateURL];

            }

        }

    }];

    

    return [UesrInfosingleton].needUpdate;

}

-(void)setUpdateURL:(NSString *)updateURL{

    NSMutableDictionary *updateURLDic = [[NSMutableDictionaryalloc]initWithObjectsAndKeys:updateURL,@"updateURL",nil];

    

    [PlistReaderdoWrite:@"updateURL"dic:updateURLDic];

}</span>

       

你可能感兴趣的:(版本更新)