iOS --版本更新

iOS版本更新机制
本文介绍了一个简单的iOS应用版本更新机制实现方案。通过自定义UIWindow扩展,在应用启动时检查当前版本与服务器上的最新版本,若存在更新则提示用户进行升级。

   我们开发APP避免不了版本更新,现在我把我个人封装了一个小小的版本更新的东西跟大家分享一下。

首先我们创建一个UIWindow+Extension文件  我们在.H文件里写一个方法并定义几个宏。来设置一些数据信息。

- (void)chooseRootViewController;
#define kHarpyAlertViewTitle        @"版本升级"
#define kHarpyCancelButtonTitle     @"下次再说"
#define kHarpyUpdateButtonTitle     @"马上升级"
#define kAppID                      @"XXXXXX" //这不用多说明什么 

我们在看下.M文件的东西 

- (void)chooseRootViewController{
    //取出Window
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    //加载info.plist,获取软件当前版本号
    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
    //获取本地版本号
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *sandboxVersion = [defaults valueForKey:@"sandboxVersion"];
    //判断是否显示新特性
    if ([currentVersion compare:sandboxVersion] == NSOrderedDescending) {
        // 显示新特性
        IntroduceViewController *nfVc = [[IntroduceViewController alloc] init];
        // 更新本地保存的版本号
        [defaults setValue:currentVersion forKey:@"sandboxVersion"];
       //可以做一些新特性东西  
    }else
    {
        // 设置根控制器
        window.rootViewController = [ViewController sharedInstance];
        //开启线程加载判断版本 
        [self performSelectorInBackground:@selector(test) withObject:nil];
    }
}

-(void)test{
    /**
     *  版本更新
     */
    NSString *storeString = [NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@", kAppID];
    NSURL *storeURL = [NSURL URLWithString:storeString];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:storeURL];
    [request setHTTPMethod:@"GET"];
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if ( [data length] > 0 && !error ) { // Success
            NSDictionary *appData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            dispatch_async(dispatch_get_main_queue(), ^{
                // All versions that have been uploaded to the AppStore
                NSArray *versionsInAppStore = [[appData valueForKey:@"results"] valueForKey:@"version"];
                if ( ![versionsInAppStore count] ) { // No versions of app in AppStore
                    
                    return;
                }
                else {
                    NSString *currentAppStoreVersion = [versionsInAppStore objectAtIndex:0];
                    NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
                    
                    if ([currentVersion compare:currentAppStoreVersion options:NSNumericSearch] == NSOrderedAscending) {
                        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:kHarpyAlertViewTitle
                                                                            message:[NSString stringWithFormat:@"发现新版本(%@),是否更新", currentAppStoreVersion]
                                                                           delegate:self
                                                                  cancelButtonTitle:kHarpyCancelButtonTitle
                                                                  otherButtonTitles:kHarpyUpdateButtonTitle, nil];
                        alertView.tag = [kAppID intValue];
                        [alertView show];
                    }
                    else {
                        MyLog(@"等于");
                        return;
                    }
                }
            });
        }
    }];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if ( alertView.tag == [kAppID intValue] ) {
        switch ( buttonIndex ) {
            case 0:{
                MyLog(@"----下次再说");
            } break;
            case 1:{ // Update
                NSString *iTunesString = [NSString stringWithFormat:@"https://itunes.apple.com/app/id%@", kAppID];
                NSURL *iTunesURL = [NSURL URLWithString:iTunesString];
                [[UIApplication sharedApplication] openURL:iTunesURL];
            } break;
            default:
                break;
        }
    }
}
其实这个东西很简单的东西。。。。如果对你有帮助 请点赞

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值