zoukankan      html  css  js  c++  java
  • iOS自动检测版本更新

    虽然苹果官方是不允许应用自动检测更新,提示用户下载,因为苹果会提示你有多少个软件需要更新,但是有的时候提示用户一下有新版还是很有必要的。

    首先说一下原理:

    每个上架的苹果应用程序,都会有一个应用程序的ID,根据这个ID我们就可以获取到当前程序的最新版本号,然后和自己的版本号作比较,如果一样的话就是最新版,反之就不是新版,就可以提示用户来手动下载最新版的程序。因为有ID所以就可以定位到这个APP,点击下载即可。

    源码:

    一般建议检测更新的代码放到主页控制器里。

    首先还要导入一个头文件用来打开AppStore下载更新

    //AppStore
    #import <StoreKit/StoreKit.h>

    接着还有代理

    SKStoreProductViewControllerDelegate

    然后开始检测更新

    //检测版本,版本更新
        NSError *error;
        NSString *urlStr = @"http://itunes.apple.com/lookup?id=上架AppID";
        NSURL *url = [NSURL URLWithString:urlStr];
        NSURLRequest *request = [NSURLRequest requestWithURL:url];
        NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
        NSDictionary *appInfoDict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
        if (error)
        {
            return;
        }
        NSArray *resultArray = [appInfoDict objectForKey:@"results"];
        if (![resultArray count])
        {
            return;
        }
        
        NSDictionary *infoDict = [resultArray objectAtIndex:0];
        //获取服务器上应用的最新版本号
        NSArray* arr=[infoDict[@"version"] componentsSeparatedByString:@"."];
        NSInteger updateVersion=0;
        for (int i=0; i<arr.count; i++)
        {
            if(i==0)
            {
                updateVersion+=[arr[i] integerValue]*1000;
            }
            else if (i==1)
            {
                updateVersion+=[arr[i] integerValue]*100;
            }
            else if (i==2)
            {
                updateVersion+=[arr[i] integerValue]*10;
            }
            else if (i==3)
            {
                updateVersion+=[arr[i] integerValue]*1;
            }
        }
        NSString *trackName = infoDict[@"trackName"];
        _trackViewUrl = infoDict[@"trackViewUrl"];
    //获取当前设备中应用的版本号
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSArray* arr2=[[infoDic objectForKey:@"CFBundleShortVersionString"] componentsSeparatedByString:@"."];
        NSInteger currentVersion=0;
        
        for (int i=0; i<arr2.count; i++)
        {
            if(i==0)
            {
                currentVersion+=[arr2[i] integerValue]*1000;
            }
            else if (i==1)
            {
                currentVersion+=[arr2[i] integerValue]*100;
            }
            else if (i==2)
            {
                currentVersion+=[arr2[i] integerValue]*10;
            }
            else if (i==3)
            {
                currentVersion+=[arr2[i] integerValue]*1;
            }
        }
        
        //判断两个版本是否相同
        if (currentVersion < updateVersion)
        {
            NSString *titleStr = [NSString stringWithFormat:@"检查更新:%@", trackName];
            NSString *messageStr = [NSString stringWithFormat:@"发现新版本%@,是否更新", infoDict[@"version"]];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:titleStr message:messageStr delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"升级", nil];
         //为了区分其他弹出框而已 alert.tag
    = 1112427256; [alert show]; }

    接着就是用户更不更的问题了

    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
        
        if (alertView.tag == 1112427256)
        {
            if (buttonIndex == 1)
            {
                //点击”升级“按钮,就从打开app store上应用的详情页面
                SKStoreProductViewController *storeProductVC = [[SKStoreProductViewController alloc] init];
                storeProductVC.delegate = self;
                NSDictionary *dict = [NSDictionary dictionaryWithObject:@"上架AppID" forKey:SKStoreProductParameterITunesItemIdentifier];
                [storeProductVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError *error)
                 {
                     if (result)
                     {
                         [self presentViewController:storeProductVC animated:YES completion:nil];
                     }
                 }];
            }
        }
    }

    还有就是用户打开AppStore但是没有下载就返回回来的状况

    - (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController
    {
        [viewController dismissViewControllerAnimated:YES completion:nil];
    }

    OK到这里就结束了。这样的话就可以检测App是不是最新版了,而且用户也能实时看到,最关键的是苹果审核还能通过。

  • 相关阅读:
    换个角度思考问题
    云南印象
    子网掩码划分实例
    子网掩码划分工具下载
    实景地图
    AutoCAD图像输出(输出图像)技巧
    两种消费观念
    子网掩码划分计算方法及实例
    C/C++从入门到高手所有必备PDF书籍收藏
    WINCE6.0添加特定的软件键盘
  • 原文地址:https://www.cnblogs.com/BK-12345/p/6103788.html
Copyright © 2011-2022 走看看