一.前言
1.第一次写博客,可以说网上资源的总结一下,雷同多有担待。
2.随着15年3月起app上架AppStore官方审核不允许提示更新升级的字样。可规则是死的 ,人是活的 。还是有空可钻的。
二.提示版本更新的两种方式
提示更新大致无非有两种:
1.在设置里有“检查更新”处,检查版本,不做重点介绍 (PS:安卓截图,IOS暂时不要想了;不过也可以让后台控制,审查期间让后台隐藏掉,审核通过后再显示。不过当升级版本的时候也不大好控制。综合考虑:还是不要这个了吧 .....AppStore有现成的提示更新就行了。)
2.启动APP的时候已弹窗的形式提示更新 (重点介绍)
三.实现方法逻辑机制及原代码
1.实现思路:a.取自身版本号;b.取AppStore版本号;c.版本号的比较 d.弹框的显示 e.跳转AppStore更新
2.代码实现
a.取自身版本号,Xcode 的Version (这个版本号就是1.0.3)
//获取APP自身版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
b.取AppStore版本号
1.首先得获取App的AppID (红色箭头所指。PS:涂鸦感)
//定义的App地址
NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",AppID];
//AppID即是如图红色箭头获取的AppID
//PS:有的时候可能会请求不到数据,但是AppID对了,有可能是App是上架区域范围的原因,建议使用在com末尾加上“/cn”
//例:NSString *url = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",AppID];
//网络请求App的信息(我们取Version就够了)
NSURL *url = [NSURL URLWithString:url];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:10];
[request setHTTPMethod:@"POST"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSMutableDictionary *receiveStatusDic=[[NSMutableDictionary alloc]init];
if (data) {
//data是有关于App所有的信息
NSDictionary *receiveDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
if ([[receiveDic valueForKey:@"resultCount"] intValue]>0) {
[receiveStatusDic setValue:@"1" forKey:@"status"];
[receiveStatusDic setValue:[[[receiveDic valueForKey:@"results"] objectAtIndex:0] valueForKey:@"version"] forKey:@"version"];
//请求的有数据,进行版本比较
[self performSelectorOnMainThread:@selector(receiveData:) withObject:receiveStatusDic waitUntilDone:NO];
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}else{
[receiveStatusDic setValue:@"-1" forKey:@"status"];
}
}];
[task resume];
c
1.弹窗比较
-(void)receiveData:(id)sender
{
//获取APP自身版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary]objectForKey:@"CFBundleShortVersionString"];
NSArray *localArray = [localVersion componentsSeparatedByString:@"."];
NSArray *versionArray = [sender[@"version"] componentsSeparatedByString:@"."];
if ((versionArray.count == 3) && (localArray.count == versionArray.count)) {
if ([localArray[0] intValue] < [versionArray[0] intValue]) {
[self updateVersion];
}else if ([localArray[0] intValue] == [versionArray[0] intValue]){
if ([localArray[1] intValue] < [versionArray[1] intValue]) {
[self updateVersion];
}else if ([localArray[1] intValue] == [versionArray[1] intValue]){
if ([localArray[2] intValue] < [versionArray[2] intValue]) {
[self updateVersion];
}
}
}
}
}
d,e升级提示及跳转
-(void)updateVersion{
NSString *msg = [NSString stringWithFormat:@"又出新版本啦,快点更新吧!"];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"升级提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
// Create the actions.
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"下次再说" style:UIAlertActionStyleCancel handler:nil];
UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"现在升级"style:UIAlertActionStyleDestructive handler:^(UIAlertAction*action) {
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:kBuyerAppUpdateUrl]];
[[UIApplication sharedApplication]openURL:url];
}];
// Add the actions.
[alertController addAction:cancelAction];
[alertController addAction:otherAction];
[self.window.rootViewController presentViewController:alertController animated:YES completion:nil];
}
四.结语
至此,关于弹窗提示升级就结束了 。PS:这里比较版本号不是不一样就提示更新升级。而是当前版本号如果比AppStore版备号小的时候提示弹框升级。这样做的最大好处就是苹果在审核App时不会出现提示升级。当然如果你的版本号比AppStore设置小了(不可能),那你就。。。。