iOSiOS开发技巧首页投稿(暂停使用,暂停投稿)

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

2016-05-10  本文已影响1314人  鹿丸眼中的云

版本更新提醒

使用条件:

图片展示

版本提醒.png

代码

#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
#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

使用

上一篇下一篇

猜你喜欢

热点阅读