需求:低版本正常使用,新版本提交审核;审核通过后,新版本正常,低版本提示更新。
app运行中的几种状态
- 第一次启动程序
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
- 从后台跳前台
- (void)applicationDidBecomeActive:(UIApplication *)application;
第一次启动后需要
id first = [[NSUserDefaults standardUserDefaults] objectForKey:@"appVersion"];
if (![first isKindOfClass:[NSString class]])
{
[self performSelectorOnMainThread:@selector(setupTheStart:) withObject:nil waitUntilDone:YES];
}
请求服务端检测审核状态
服务端控制新版本发布后,旧版本是允许继续使用,还是强制更新。
-(void)setupTheStart:(NSDictionary *)userInfo
{
__weak typeof(self) weakSelf = self;
NSString * url = @"获取审核状态接口"
[TIMHttpTool GET:url parameters:nil success:^(id responseObject) {
if ([responseObject[@"code"] intValue] == 0)
{
NSDate *nowDate = [NSDate date];
[ [NSUserDefaults standardUserDefaults] setObject:nowDate forKey:@"nowDate"];
[dataUser synchronize];
if ([responseObject[@"detail"][@"is_check"] intValue] == 0)
{
[NSUserDefaults saveInfo:@"" withKey:@"appState"];
}
else if([responseObject[@"detail"][@"is_check"] intValue] == 1)
{
[NSUserDefaults saveInfo:@"check" withKey:@"appState"];
}
switch ([responseObject[@"detail"][@"update"] intValue])
{
case 0:
[NSUserDefaults saveInfo:@"" withKey:@"UPDATE"];
break;
case 1:// 可更新
case 2:// 强制
{
NSDictionary * dic = responseObject[@"detail"];
[[NSUserDefaults standardUserDefaults] setObject:dic forKey:@"UPDATE"];
[[NSUserDefaults standardUserDefaults] synchronize];
[weakSelf compareWith:responseObject[@"detail"]];
}
break;
default:
break;
}
}
else
{
[NSUserDefaults saveInfo:@"" withKey:@"UPDATE"];
[NSUserDefaults saveInfo:@"check" withKey:@"appState"];
}
} failure:^(NSError *error) {
[NSUserDefaults saveInfo:@"" withKey:@"UPDATE"];
[NSUserDefaults saveInfo:@"check" withKey:@"appState"];
}];
}
强制更新
- (void)compareWith:(NSDictionary *)dic
{
if (dic)
{
NSArray * infoArray = dic[@"update_detail"];
NSMutableString * infoStr = [[NSMutableString alloc]init];
for (int i = 0 ; i < infoArray.count; i++)
{
[infoStr appendString:[NSString stringWithFormat:@"%d、%@\n",i+1,infoArray[i]]];
}
UIAlertController * alertCon = [UIAlertController alertControllerWithTitle:@"更新提示" message: infoStr preferredStyle:UIAlertControllerStyleAlert];
if ([dic[@"update"] intValue] == 1)
{
[alertCon addAction:[UIAlertAction actionWithTitle:@"现在更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[[UIApplication sharedApplication ]openURL:[NSURL URLWithString:dic[@"redirect"]]];
}]];
[alertCon addAction:[UIAlertAction actionWithTitle:@"暂不更新" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){
[NSUserDefaults saveInfo:@"" withKey:@"UPDATE"];
}]];
}
else if([dic[@"update"] intValue] == 2)
{
[alertCon addAction:[UIAlertAction actionWithTitle:@"现在更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){
[[UIApplication sharedApplication ]openURL:[NSURL URLWithString:dic[@"redirect"]]];
[[IMAPlatform sharedInstance] logout:^{
[[AppDelegate sharedAppDelegate] enterLoginUI];
} fail:^(int code, NSString *err) {
[[AppDelegate sharedAppDelegate] enterLoginUI];
}];
}]];
}
[self.window.rootViewController presentViewController:alertCon animated:YES completion:nil];
}
}
登录后,本地存当前版本号
[[NSUserDefaults standardUserDefaults] setObject:KAppVersion forKey:@"appVersion"];
appState当前状态:审核版: @"check"
正式版:@""
id state = [[NSUserDefaults standardUserDefaults] objectForKey:@"appState"];
获取app当前版本:
#define KAppVersion [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]