ios自定义App版本升级提示

2021-08-12  本文已影响0人  浅_若清风

1.新建一个继承于NSObject的model类AppUpdater,声明弹窗提示函数

//AppUpdater.h文件实现
//头文件
#import <SystemConfiguration/SystemConfiguration.h>
@interface AppUpdater : NSObject
//单例
+ (id)sharedUpdater;
//检测更新
- (void)showUpdateWithConfirmation;
//更新提示
@property (nonatomic, weak) NSString *alertTitle;
//更新版本内容
@property (nonatomic, weak) NSString *alertMessage;
//前往更新
@property (nonatomic, weak) NSString *alertUpdateTitle;
//暂不更新
@property (nonatomic, weak) NSString *alertCancelTitle;
@end
//AppUpdater.m文件实现
@implementation AppUpdater
//appStore地址变量
NSString *appStoreURL = nil;
//单例
+ (id)sharedUpdater
{
    static AppUpdater *sharedUpdater;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedUpdater = [[AppUpdater alloc] init];
    });
    return sharedUpdater;
}
//初始化
- (id)init 
{
    self = [super init];
    if(self) 
    {
        self.alertTitle = @"更新提示";
        self.alertMessage = @"新版本%@已经上线,快来更新吧!";
        self.alertUpdateTitle =@"前往更新";
        self.alertCancelTitle = @"暂不更新";
    }
    return self;
}
//检测更新
- (void)showUpdateWithConfirmation
{
    BOOL isConnection = [self isConnection];
    if (!isConnection) return;
    [self checkNewAppVersion:^(BOOL newVersion, NSString *version)
    {
        if (newVersion) 
        {
            [[self alertUpdateForVersion:version withForce:NO] show];
        }
    }];
}
//监测网络状态
- (BOOL)isConnection
 {
    const char *host = "itunes.apple.com";
    BOOL reachable;
    BOOL success;
    SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host);
    SCNetworkReachabilityFlags flags;
    success = SCNetworkReachabilityGetFlags(reachability, &flags);
    reachable = success && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
    CFRelease(reachability);
    return reachable;
}
//获取AppStore上版本号,与当前版本号做比较
- (void)checkNewAppVersion:(void(^)(BOOL newVersion, NSString *version))completion
{
    NSString *currentVersion = VERSION_NUBER_STR;
    NSURL *lookupURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@", @"Apple Id"]];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void) {
        NSData *lookupResults = [NSData dataWithContentsOfURL:lookupURL];
        if(!lookupResults)
        {
            completion(NO, nil);
            return;
        }
        NSDictionary *jsonResults = [NSJSONSerialization JSONObjectWithData:lookupResults options:0 error:nil];
        dispatch_async(dispatch_get_main_queue(), ^(void) {
            NSUInteger resultCount = [jsonResults[@"resultCount"] integerValue];
            if (resultCount)
            {
                NSDictionary *appDetails = [jsonResults[@"results"] firstObject];
                NSString *appItunesUrl = [appDetails[@"trackViewUrl"] stringByReplacingOccurrencesOfString:@"&uo=4" withString:@""];
                NSString *latestVersion = appDetails[@"version"];
                if ([latestVersion compare:currentVersion options:NSNumericSearch] == NSOrderedDescending)
                {
                    appStoreURL = appItunesUrl;
                    completion(YES, latestVersion);
                }
                else
                {
                    completion(NO, nil);
                }
            }
            else
            {
                completion(NO, nil);
            }
        });
    });
}
//提示弹窗
- (UIAlertView *)alertUpdateForVersion:(NSString *)version withForce:(BOOL)force
{
    NSString *msg = [NSString stringWithFormat:self.alertMessage, version];
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:self.alertTitle message:msg delegate:self cancelButtonTitle:self.alertUpdateTitle otherButtonTitles:self.alertCancelTitle, nil];
    return alert;
}
//连接错误
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        NSURL *appUrl = [NSURL URLWithString:appStoreURL];
        if([[UIApplication sharedApplication] canOpenURL:appUrl])
        {
            [[UIApplication sharedApplication] openURL:appUrl];
        }
        else
        {
            UIAlertView *cantOpenUrlAlert = [[UIAlertView alloc] initWithTitle:@"无法使用" message:@"无法打开 AppStore,请稍后再试" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [cantOpenUrlAlert show];
        }
    }
}
@end

代码中的“ VERSION_NUBER_STR”宏表示APP当前的版本号,APP在AppStore上地址为"http://itunes.apple.com/cn/lookup?id=xxx",(xxx为你的Apple id,Apple id的获取可参考文章iOS查看App的Apple id)。使用compare...options...比较两个版本号,NSOrderedDescending是枚举值,表示左边的字符串大于右边的字符。
2.在AppDelegate的didFinishLaunchingWithOptions方法中调用App版本检测方法,以便在启动App的时候能监测到版本更新。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{
        //检测版本
        [[AppUpdater sharedUpdater]showUpdateWithConfirmation];
}

3.当AppStore上有更新版本时,效果如下


img_1.png
上一篇 下一篇

猜你喜欢

热点阅读