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];
}