h文件源码
下面XXX 为App ID,可以在iTunes Connect查看
//获取AppStore App Version
//id=“” App ID
static NSString * const urlStr = @"http://itunes.apple.com/lookup?id=XXX";
//跳转AppStore Url
static NSString * const appStoreUrl = @"https://itunes.apple.com/us/app/xiao-qi-dian/idXXX?ls=1&mt=8";
UpgrageAlertView:需要提示时,传入一个UIViewController
@interface UpgradeHintController : UIAlertController
/**
* 提示用户更新App
*
* 只支持8.0以上版本
*
* @param delegate ViewControler
*/
+ (void)UpgrageAlertView:(UIViewController *)delegate;
@end
m文件源码
取出AppStore中App Version,和本地的Version作比较,如果不相同时会弹出提示框
当点击OK按钮时会跳转到AppStore上
这里我做了个处理,如果点击Cannel就表明不更新,会自动退出程序,必须更新才能正常使用,可以把“exit(0)”注释
@implementation UpgradeHintController
+ (NSURL *)returnUrl:(NSString *)str
{
NSURL *url = [NSURL URLWithString:str];
return url;
}
+ (void)UpgrageAlertView:(UIViewController *)delegate
{
//取出本地Version
NSDictionary *info = [[NSBundle mainBundle]infoDictionary];
NSString *infoVersion = info[@"CFBundleShortVersionString"];
//取出AppStore 中App的Version
NSURLRequest *request = [NSURLRequest requestWithURL:[UpgradeHintController returnUrl:urlStr]];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
id obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
NSString *version = [[obj[@"results"] objectAtIndex:0] objectForKey:@"version"];
//判断Version
if (![infoVersion isEqualToString:version]) {
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Message" message:[NSString stringWithFormat:@"The latest version is %@, please update",version] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cannel = [UIAlertAction actionWithTitle:@"Cannel" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
//用户不更新,直接闪退
// exit(0);
}];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//跳转AppStore
[[UIApplication sharedApplication]openURL:[UpgradeHintController returnUrl:appStoreUrl]];
}];
[alert addAction:cannel];
[alert addAction:ok];
//弹出提示框
[delegate presentViewController:alert animated:YES completion:^{
}];
}else{
return ;
}
}];
[dataTask resume];
}