iOS 更新版本

1. 在AppStore上线的应用(个人或者公司账号 99美元)

在AppDelegate.m中调用 

/**

* 版本升级

*/

[self versionCheckAndUpdate];

实现:

- (void)versionCheckAndUpdate {

// 获取当前版本  只需要Version,不需要Bulid

NSString *newVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

NSLog(@"%@", newVersion);

// 获取 AppStore版本号

NSString *urlStr = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", @"1136975672"];

[self getAppStoreVersionWithpath:urlStr completionHandler:^(NSString *version) {

if (![version isEqualToString:@""]) {

if (![version isEqualToString:newVersion]) {

NSLog(@"有新版本了");

}

} else {

NSLog(@"AppStore没有版本");

}

}];

}

#pragma mark -- 获取数据 --

- (void)getAppStoreVersionWithpath:(NSString *)path completionHandler:(void(^)(NSString *version))completionHandler {

// 使用NSURLSession去请求苹果给的接口,会有很多信息,只取version

NSURL *url = [NSURL URLWithString:path];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url

cachePolicy:NSURLRequestReloadIgnoringCacheData

timeoutInterval:10];

[request setHTTPMethod:@"POST"];

[[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

NSDictionary *resultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers | NSJSONReadingMutableLeaves error:nil];

if ([[resultDic valueForKey:@"resultCount"] intValue]>0) {

NSString *version = [[[resultDic valueForKey:@"results"] firstObject] valueForKey:@"version"];

dispatch_async(dispatch_get_main_queue(), ^{

completionHandler(version);

});

} else {

dispatch_async(dispatch_get_main_queue(), ^{

completionHandler(@"");

});

}

}] resume];

}

2. 企业应用 (299美元)

a. 之前可以用友盟,现在貌似新用户用不了;

b. 说到底,我们只需要一个当前用户正在使用的版本的版本号,然后和当前的版本号比较。写个简单的接口,让后台返回就行 

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