博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS开发:App版本更新提示框的使用方法
阅读量:138 次
发布时间:2019-02-27

本文共 5586 字,大约阅读时间需要 18 分钟。

       今天五一国际劳动节,依然在正常上班,就分享一个知识点呗。在iOS开发过程中,App上线之后,进行版本更新的时候,需要及时提醒用户更新最新的App版本,那么就用到了版本更新提示框的使用。本章就来介绍一下App版本更新提示框的使用方法,各取所需,仅供参考。

一、根据第三方实现版本更新提示框的方法

        根据第三方实现App版本更新提示框,进行了封装,然后直接将AppID作为参数传进去或者直接获取应用的bundleID来使用的,把工具类拖入到项目中,只需要Appdelegate文件里面一句代码就可集成实现的方法。原创的下载链接 具体实现操作步骤如下所示:

1、AppUpdater.h文件:

#import <UIKit/UIKit.h>

#import <SystemConfiguration/SystemConfiguration.h>

 

@interface AppUpdater : NSObject <UIAlertViewDelegate>

+ (id)sharedUpdater;

- (void)showUpdateWithForce;

- (void)showUpdateWithConfirmation;

- (void)forceOpenNewAppVersion:(BOOL)force

__attribute((deprecated("Use 'showUpdateWithForce' or 'showUpdateWithConfirmation' instead.")));

@property (nonatomic, weak) NSString *alertTitle;

@property (nonatomic, weak) NSString *alertMessage;

@property (nonatomic, weak) NSString *alertUpdateTitle;

@property (nonatomic, weak) NSString *alertCancelTitle;

@end

 

2、AppUpdater.m文件:

#import "AppUpdater.h"

@implementation AppUpdater

+ (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)showUpdateWithForce {

    BOOL isConnection = [self isConnection];

    if (!isConnection) return;

    [self checkNewAppVersion:^(BOOL newVersion, NSString *version) {

        if(newVersion) {

            [[self alertUpdateForVersion:version withForce:YES] show];

        }

    }];

}

 

- (void)showUpdateWithConfirmation {

    BOOL isConnection = [self isConnection];

    if (!isConnection) return;

    [self checkNewAppVersion:^(BOOL newVersion, NSString *version) {

        if (newVersion) {

            [[self alertUpdateForVersion:version withForce:NO] show];

        }

    }];

}

 

- (void)forceOpenNewAppVersion:(BOOL)force {

    BOOL isConnection = [self isConnection];

    if (!isConnection) return;

    [self checkNewAppVersion:^(BOOL newVersion, NSString *version) {

        if (newVersion) {

            [[self alertUpdateForVersion:version withForce:force] show];

        }

    }];

}

 

#pragma mark - Private Methods

- (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;

}

 

NSString *appStoreURL = nil;

- (void)checkNewAppVersion:(void(^)(BOOL newVersion, NSString *version))completion {

    NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];

    NSString *currentVersion = bundleInfo[@"CFBundleShortVersionString"];

    NSURL *lookupURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/cn/lookup?id=%@", KAPPID]];

    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 cancelTitle:force ? nil:self.alertUpdateTitle otherButtonTitles:force ? self.alertUpdateTitle: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:@"Not Available" message:@"Could not open the AppStore, please try again later." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];

            [cantOpenUrlAlert show];

        }

    }

}

@end

 

3AppDelegate.m文件:

AppDelegate里面使用的地方,具体代码如下所示:

#import "AppDelegate.h"

#import "BaseTabBarController.h"

#import "AppUpdater.h"

@interface AppDelegate ()

@end

 

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    sleep(3); //设置启动页显示时间

    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    //App版本更新提示

    [[AppUpdater sharedUpdater] showUpdateWithConfirmation];    

    BaseTabBarController *tabController = [BaseTabBarController new];

    self.window.rootViewController = tabController;

    return YES;

}

       代码图示如下所示:

       

       以上代码实现之后,可以测试一下弹框效果,看看有没有正常显示版本更新的弹框,具体方法就是在xcode里面把已经上线的版本修改的低一点,然后再运行一次,在模拟器中就可测试出来,具体操作如下所示:

         以上就是本章全部内容,欢迎关注三掌柜的微信公众号、微博,欢迎关注!

         三掌柜的微信公众号:

         三掌柜的新浪微博:

 

转载地址:http://nslf.baihongyu.com/

你可能感兴趣的文章