美文网首页
ios自定义App版本升级提示

ios自定义App版本升级提示

作者: 浅_若清风 | 来源:发表于2021-08-12 17:38 被阅读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

相关文章

  • ios自定义App版本升级提示

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

  • iOS版本升级提醒

    关于iOS版本升级,苹果是不允许用户有强制用户升级的提示的,但是为了让用户知道APP更新了,一般APP里面是会有版...

  • ionicapp ios版本升级遇到的问题

    最近需要升级ionic App ios版本,遇到了好多问题,在这里记录一下: 1.appId双重认证忘掉安全提示问...

  • APP内部提示版本升级

    版本提示升级的两种方式: 1.通过公司内部服务器发送版本号,来提示是否与当前版本一致 从而判断是否需要升级 2.通...

  • iOS实现对比App版本提示用户升级

    如果要在app里加入提醒用户升级的功能,只需要几个步骤: 获取用户本地app的版本号 获取App Store上本a...

  • 上架APP进行版本升级检测

    文章来源:iOS-App版本更新提示AppDelegate.m文件: 提示更新的界面增加:

  • iOS APP版本升级

    1.获取appStore的APP数据//获取appStore版本号 #pragma mark -- 获取appSt...

  • ios提示用户版本升级

    本文主要介绍的是两种提示用户的更新的方法,文字比较多。但都是逻辑 现在苹果审核不能看到版本提示更新的字样以及功能,...

  • iOS 提示用户版本升级

  • ios提示用户版本升级

    本文主要介绍的是两种提示用户的更新的方法,文字比较多。但都是逻辑 现在苹果审核不能看到版本提示更新的字样以及功能,...

网友评论

      本文标题:ios自定义App版本升级提示

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