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

版本更新提醒

  • 每當版本更新后,都會有一個提醒用戶更新版本的需求,下面這段代碼是為了應對這種需求編寫的一個類

使用條件:

  • 用到了拿走即用之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;方法
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容