拿走即用之版本更新提醒(OC版)

作者: 鹿丸眼中的云 | 来源:发表于2016-05-10 17:33 被阅读1314次

版本更新提醒

  • 每当版本更新后,都会有一个提醒用户更新版本的需求,下面这段代码是为了应对这种需求编写的一个类

使用条件:

  • 用到了拿走即用之afn封装(OC版)(代码在本人的简书中)中的NetworkTools这个类

图片展示

版本提醒.png

代码

  • .h文件
#import <Foundation/Foundation.h>

@interface VersionUpdateAlert : NSObject

// 间隔多少次使用,再次出现弹框提醒,默认20
@property (nonatomic, assign) NSInteger interCount;

+ (instancetype)shareVersionUpdateAlert;

/**
 * appID:appleID
 * VC:alertView需要一个控制器引出,一般为设为rootViewController的那个控制器
 */

- (void)checkAndShowWithAppID:(NSString *)appID andController:(UIViewController *)VC;

@end
  • .m文件
#import "VersionUpdateAlert.h"
#import "NetworkTools.h"

@implementation VersionUpdateAlert

+ (instancetype)shareVersionUpdateAlert
{
    static VersionUpdateAlert *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[VersionUpdateAlert alloc] init];
    });
    return instance;
}

- (void)checkAndShowWithAppID:(NSString *)appID andController:(UIViewController *)VC
{
    NSString *urlString = [NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@",appID];
    [[NetworkTools shareNetworkTools] requestWithMethod:get andUrlString:urlString andParameters:nil andFinished:^(id response, NSError *error) {
        if (error == nil) {
            NSArray *array = response[@"results"];
            NSDictionary *dict = [array lastObject];
            NSLog(@"当前版本为:%@", dict[@"version"]);
            // 获取info的字典
            NSDictionary* infoDict = [NSBundle mainBundle].infoDictionary;
            // 版本号保存在本地
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            NSString* appVersion = infoDict[@"CFBundleShortVersionString"];
            NSString *versionString = [userDefaults objectForKey:@"versionString"];
            if (versionString == nil) {
                [userDefaults setObject:appVersion forKey:@"versionString"];
            }
            //当版本更新后重置interCount
            if (![[userDefaults objectForKey:@"versionString"] isEqualToString:appVersion]) {
                [userDefaults setObject:@"0" forKey:@"interCount"];
                [userDefaults setObject:appVersion forKey:@"versionString"];
            }
            if ([dict[@"version"] compare:appVersion] == NSOrderedDescending) {
                
                NSNumber *interCount = [userDefaults objectForKey:@"interCount"];
                if (interCount == nil) {
                    [self alertShowWithAppID:appID andController:VC];
                }
                if ([interCount integerValue]%(self.interCount ? self.interCount : 20) == 0) {
                    [self alertShowWithAppID:appID andController:VC];
                }
                NSInteger integerInterCount = [interCount integerValue];
                integerInterCount++;
                NSNumber *numberInterCount = [NSNumber numberWithInteger:integerInterCount];
                [userDefaults setObject:numberInterCount forKey:@"interCount"];
            }
        }
    }];
}

//弹出的alertView
- (void)alertShowWithAppID:(NSString *)appID andController:(UIViewController *)VC
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"版本更新提醒" message:@"更新的内容,更全面的小说,更佳的体验,快来更新吧" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *installAction = [UIAlertAction actionWithTitle:@"安装" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self openAppaleShopWithAppID:appID];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    [alertController addAction:installAction];
    [alertController addAction:cancelAction];
    [VC presentViewController:alertController animated:YES completion:nil];
}

//打开appStore
- (void)openAppaleShopWithAppID:(NSString *)appID
{
    NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",appID];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
}

@end

使用

  • 在appdelegate finish----方法中调用- (void)checkAndShowWithAppID:(NSString *)appID andController:(UIViewController *)VC;方法

相关文章

网友评论

  • WARRON:NetworkTools ? 在哪儿呢
    WARRON:@鹿丸眼中的云 写的厉害
    WARRON:@鹿丸眼中的云 找到啦,谢谢:stuck_out_tongue_winking_eye:
    鹿丸眼中的云:@WARRON 我简书另一篇文章,拿走即用之afn封装
  • 无心可活:好
    鹿丸眼中的云:@无心可活 谢谢
  • 春田花花幼儿园:这样上不了架吧……
    鹿丸眼中的云:@春田花花幼儿园 提醒更新的功能在app里不是很常见吗,这份代码提交审核,如果审核人员不去看代码的话,不会知道里面有提示更新的功能,因为提交的版本会比线上版本版本号高,不会出现弹框,这样的话应该会规避你提到的这个风险吧
    春田花花幼儿园:@鹿丸眼中的云 审核不允许你再应用内提示更新吧。我记得是。只能自己到appstore更新。
    鹿丸眼中的云:@春田花花幼儿园 为什么啊,求解释

本文标题:拿走即用之版本更新提醒(OC版)

本文链接:https://www.haomeiwen.com/subject/ixyyrttx.html