iOS升级更新提醒功能

苹果爸爸很早在系统设置里面提供了应用自动升级的功能,所有就明令限制了应用内弹出更新升级提醒的功能,凡是包含这个弹框提醒的功能,甚至是应用内包含相关的更新按钮在审核的时候都不会通过。


iOS升级更新提醒功能_第1张图片

但是上有政策,下有对策,基本来说有两个方式展示升级提示,并且不会被苹果审核发现:
1、后端控制。
后端写个更新的接口,通过接口下发参数控制是否展示升级弹框。所有可以在应用提交审核的期间关闭或者在接口参数返回状态不弹框,审核通过后开放。当然如果你只是普通的更新提示,倒是不用接口,但是如果要做强制更新的话,还是必须接口下发控制。
2、对比APPStore上的版本信息,接口本地版本判断。
首先通过

https://itunes.apple.com/lookup?id=你的应用ID

可以获取应用在APPStore上的信息,会反馈一串JSON内容,里面包含了这个应用的所有信息。
例如下面这些信息(筛选出主要需要的信息)

{  
    resultCount = 1;  
    results =     (  
                {  
            artistId = 开发者 ID;  
            artistName = 开发者名称; 
            price = 0; 
            isGameCenterEnabled = 0;  
            kind = software;  
            languageCodesISO2A =             (  
                EN  
            ); 
            trackCensoredName = 审查名称;  
            trackContentRating = 评级;  
            trackId = 应用程序 ID;  
            trackName = 应用程序名称";  
            trackViewUrl = 应用程序介绍网址;  
            userRatingCount = 用户评级;  
            userRatingCountForCurrentVersion = 1;  
            version = 版本号;  
            wrapperType = software; 
      }  
    );  
}  

然后从NSArray *results = [resultDic objectForKey:@"results"];中取出需要的信息如下:

//版本号
NSString *appstoreVersion = [[results objectAtIndex:0] valueForKey:@"version"];
//更新内容
NSString *releaseNotes = [[results objectAtIndex:0] valueForKey:@"releaseNotes"];

通过对比本地版本号进行判断,是否需要弹框提醒。
完整代码如下:

-(void)checkUpdateInfo
{
#ifdef LOG_DEBUG
#ifdef APP_VERSION_DEBUG
    
#else
    
#endif
#else
    if (![[DataManagerObject shareDataManagerObject] getUserLogined]) {
        return;
    }
    
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    sessionManager.requestSerializer.stringEncoding = NSUTF8StringEncoding;
    sessionManager.responseSerializer = [AFJSONResponseSerializer serializer];
    sessionManager.requestSerializer.timeoutInterval = 60;
    
    NSString *url = [[@"https://itunes.apple.com/lookup?id=" stringByAppendingString:YT_APP_ID] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    [sessionManager GET:url parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *resultDic = responseObject;
        if ([resultDic[@"resultCount"] boolValue]) {
            NSArray *results = [resultDic objectForKey:@"results"];
            if (results.count) {
                //AppStore版本信息
                NSString *appstoreVersion = [[results objectAtIndex:0] valueForKey:@"version"];
                NSString *releaseNotes = [[results objectAtIndex:0] valueForKey:@"releaseNotes"];
                
                //本地版本信息
                NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
                NSString *currentVersion = [infoDic valueForKey:@"CFBundleShortVersionString"];
                
                DLog(@"appstoreVersion == %@,currentVersion == %@",appstoreVersion,currentVersion);
                NSArray *appStoreVersionArray = [appstoreVersion componentsSeparatedByString:@"."];
                NSArray *localVersionArray = [currentVersion componentsSeparatedByString:@"."];
                for (int index = 0; index < appStoreVersionArray.count; index ++) {
                    int appstoreCount = [appStoreVersionArray[index] intValue];
                    int localCount = [localVersionArray[index] intValue];
                    if (appstoreCount > localCount) {
                        [self showUpdateAlertView:releaseNotes];
                        break;
                    }
                }
            }
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
#endif
}
- (void)showUpdateAlertView:(NSString *)updateMessage
{
    [ABAlertHelper alertWithTitle:YTLocalizedString(@"有新的版本") message:updateMessage cancelActionTitle:YTLocalizedString(@"下次提醒") destructiveActionTitle:YTLocalizedString(@"前往更新") actionClick:^(UIAlertAction *action, NSInteger index) {
        if (index == -1) {
            NSString *appstoreUrl = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/ye-tu/id%@", YT_APP_ID];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appstoreUrl]];
        }
    } otherActionTitles:nil, nil];
}

至此,主要功能实现了,这里有个小点稍微提下:

NSArray *appStoreVersionArray = [appstoreVersion componentsSeparatedByString:@"."];
NSArray *localVersionArray = [currentVersion componentsSeparatedByString:@"."];
for (int index = 0; index < appStoreVersionArray.count; index ++) {
     int appstoreCount = [appStoreVersionArray[index] intValue];
     int localCount = [localVersionArray[index] intValue];
     if (appstoreCount > localCount) {
         //弹框提醒
         break;
      }
}

因为目前版本是常见的1.0.0这种三位,所以把版本字符串@"1.0.0"通过.来分割成一个包含三位元素的素组@[@"1",@"0",@"0"];通过遍历对比每一位上的数组来判断是否有新的版本,这种判断是很严谨的。

顺便附带一个强制升级下的点击处理:

[ABAlertHelper alertWithTitle:YTLocalizedString(@"前方高能!!!!!") message:@"强制升级" cancelActionTitle:nil destructiveActionTitle:nil actionClick:^(UIAlertAction *action, NSInteger index) {
        if (index == 1) {
            AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
            UIWindow *window = app.window;
            [UIView animateWithDuration:0.5 animations:^{
                window.alpha = 0;
                window.transform = CGAffineTransformMakeScale(0.1, 0.1);
            } completion:^(BOOL finished) {
                NSString *appstoreUrl = [NSString stringWithFormat:@"https://itunes.apple.com/cn/app/ye-tu/id%@", YT_APP_ID];
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appstoreUrl]];
                exit(0);
            }];
        }
    } otherActionTitles:YTLocalizedString(@"前往更新"), nil];

你可能感兴趣的:(iOS升级更新提醒功能)