- 提示更新效果图如下,当然也是可以自定义类似与AlertView相似的自定义view,如京东、网易云音乐都是自定义了这种提示框的view。以下只展示,从App Store获取到app信息、并解析app信息获取发布在App Store上的版本号与当前手机里安装的app版本号做对比,如果有更新就做提示。
- 在工程中新建一个NSObject类,将以下.h和.m文件中的代码拷贝至这个新建的类中。
-
// // HKCheckAppVersionMgr.h // HKTyy // // Created by isHakan on 17/3/24. // Copyright © 2017年 liuhuakun. All rights reserved. // #import <Foundation/Foundation.h> @interface HKCheckAppVersionMgr : NSObject + (HKCheckAppVersionMgr *)sharedInstance; - (void)isUpdataApp:(NSString *)appId; @end
-
// // HKCheckAppVersionMgr.m // HKTyy // // Created by isHakan on 17/3/24. // Copyright © 2017年 liuhuakun. All rights reserved. // #import "HKCheckAppVersionMgr.h" #import <UIKit/UIKit.h> @interface HKCheckAppVersionMgr ()<UIAlertViewDelegate> @property (nonatomic, strong) NSString *appId; @end @implementation HKCheckAppVersionMgr + (HKCheckAppVersionMgr *)sharedInstance { static HKCheckAppVersionMgr *instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[HKCheckAppVersionMgr alloc] init]; }); return instance; } - (void)isUpdataApp:(NSString *)appId { NSURL *appUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appId]]; NSString *appMsg = [NSString stringWithContentsOfURL:appUrl encoding:NSUTF8StringEncoding error:nil]; NSDictionary *appMsgDict = [self jsonStringToDictionary:appMsg]; NSDictionary *appResultsDict = [appMsgDict[@"results"] lastObject]; NSString *appStoreVersion = appResultsDict[@"version"]; float newVersionFloat = [appStoreVersion floatValue];//新发布的版本号 NSString *currentVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]; float currentVersionFloat = [currentVersion floatValue];//使用中的版本号 //当前版本小于App Store上的版本&用户未点击不再提示 if (currentVersionFloat<newVersionFloat && ![self isAlertUpdataAgain]) { self.appId = appId; UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"检测到新版本,是否去更新?" delegate:self cancelButtonTitle:@"去更新" otherButtonTitles:@"下次再说",@"不再提示", nil]; [alertView show]; } } - (NSDictionary *)jsonStringToDictionary:(NSString *)jsonStr { if (jsonStr == nil) { return nil; } NSData *jsonData = [jsonStr dataUsingEncoding:NSUTF8StringEncoding]; NSError *error; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; if (error) { //NSLog(@"json格式string解析失败:%@",error); return nil; } return dict; } #pragma mark UIAlertViewDelegate - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { if (buttonIndex==0) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/app/id%@",self.appId]]]; return; } if (buttonIndex==2) { [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"IS_ALERT_AGAIN"]; [[NSUserDefaults standardUserDefaults] synchronize]; return; } } - (BOOL)isAlertUpdataAgain { BOOL res = [[NSUserDefaults standardUserDefaults] objectForKey:@"IS_ALERT_AGAIN"]; return res; } @end
- 在需要检测App版本是否有更新的地方-[以下是以在ViewController处检测App版本更新为例]:
-
// ViewController.m // Demo // // Created by isHakan on 2017/7/21. // Copyright © 2017年 liuhuakun. All rights reserved. // #import "ViewController.h" //引入‘头文件’ #import "HKCheckAppVersionMgr.h" //发布在App Store的Apple ID #define kYourAppleID @"1234567890" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //一句代码检测更新 [[HKCheckAppVersionMgr sharedInstance] isUpdataApp:kYourAppleID]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end
转载于:https://www.cnblogs.com/liuhuakun/p/6699316.html