检测版本更新

如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息。

当前运行版本信息可以通过info.plist文件中的bundle version中获取:

[cpp] view
plain
copy

  1. NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
  2. ];

这样就获取到当前运行的app的版本了

要获取当前app store上的最新的版本,有两种方法,

一、在某特定的服务器上,发布和存储app最新的版本信息,需要的时候向该服务器请求查询。

二、从app store上查询,可以获取到app的作者,连接,版本等。官方相关文档

www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.htm

具体步骤如下:
1,用 POST 方式发送请求:
http://itunes.apple.com/search?term=你的应用程序名称&entity=software

更加精准的做法是根据 app 的 id 来查找:
http://itunes.apple.com/lookup?id=你的应用程序的ID

#define APP_URL http://itunes.apple.com/lookup?id=你的应用程序的ID

你的应用程序的ID 是 itunes connect里的 Apple ID

2,从获得的 response 数据中解析需要的数据。因为从 appstore 查询得到的信息是 JSON 格式的,所以需要经过解析。解析之后得到的原始数据就是如下这个样子的:
{  
    resultCount = 1;  
    results =     (  
                {  
            artistId = 开发者 ID;  
            artistName = 开发者名称; 
            price = 0; 
            isGameCenterEnabled = 0;  
            kind = software;  
            languageCodesISO2A =             (  
                EN  
            ); 
            trackCensoredName = 审查名称;  
            trackContentRating = 评级;  
            trackId = 应用程序 ID;  
            trackName = 应用程序名称";  
            trackViewUrl = 应用程序介绍网址;  
            userRatingCount = 用户评级;  
            userRatingCountForCurrentVersion = 1;  
            version = 版本号;  
            wrapperType = software; 
      }  
    );  
}

然后从中取得 results 数组即可,具体代码如下所示:

NSDictionary *jsonData = [dataPayload JSONValue];  
NSArray *infoArray = [jsonData objectForKey:@"results"];  
NSDictionary *releaseInfo = [infoArray objectAtIndex:0];  
NSString *latestVersion = [releaseInfo objectForKey:@"version"];  
NSString *trackViewUrl = [releaseInfo objectForKey:@"trackViewUrl"];

如果你拷贝 trackViewUrl 的实际地址,然后在浏览器中打开,就会打开你的应用程序在 appstore 中的介绍页面。当然我们也可以在代码中调用 safari 来打开它。
UIApplication *application = [UIApplication sharedApplication];  
[application openURL:[NSURL URLWithString:trackViewUrl]];

代码如下:

-(void)onCheckVersion

{

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

//CFShow((__bridge CFTypeRef)(infoDic));

NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"];

NSString *URL = @"http://itunes.apple.com/lookup?id=你的应用程序的ID";

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:URL]];

[request setHTTPMethod:@"POST"];

NSHTTPURLResponse *urlResponse = nil;

NSError *error = nil;

NSData *recervedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponseerror:&error];

NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedData length]encoding:NSUTF8StringEncoding];

NSDictionary *dic = [results JSONValue];

NSArray *infoArray = [dic objectForKey:@"results"];

if ([infoArray count]) {

NSDictionary *releaseInfo = [infoArray objectAtIndex:0];

NSString *lastVersion = [releaseInfo objectForKey:@"version"];

if (![lastVersion isEqualToString:currentVersion]) {

//trackViewURL = [releaseInfo objectForKey:@"trackVireUrl"];

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"有新的版本更新,是否前往更新?"delegate:self cancelButtonTitle:@"关闭" otherButtonTitles:@"更新", nil];

alert.tag = 10000;

[alert show];

}

else

{

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"更新" message:@"此版本为最新版本" delegate:selfcancelButtonTitle:@"确定" otherButtonTitles:nil, nil];

alert.tag = 10001;

[alert show];

}

}

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

{

if (alertView.tag==10000) {

if (buttonIndex==1) {

NSURL *url = [NSURL URLWithString:@"https://itunes.apple.com"];

[[UIApplication sharedApplication]openURL:url];

}

}

}

检测版本更新

时间: 2024-07-31 09:49:07

检测版本更新的相关文章

iOS 检测版本更新(02)

iOS 检测版本更新 如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle version中获取: [cpp] view plaincopy NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDic); NSString *appVersion = [infoDic

iOS 检测版本更新

iOS 检测版本更新 分类: IOS_XCODE 2013-07-19 16:52 18252人阅读 评论(5) 收藏 举报 如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle version中获取: [cpp] view plaincopy NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionar

安卓APP采用观察者模式实现检测版本更新

第一步:定义观察者 public interface CheckVersionObserver { /** * 在MainActivity里面检测版本更新成功 * @param mainEntity */ public void onCheckNewVerSuccInMain(MainEntity mainEntity); /** * 检测新版本失败 * @param errorCode * @param msg */ public void onCheckNewVerFail(int erro

iOS通过iTunes search检测版本更新,并提示用户更新

如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle version中获取: NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; CFShow(infoDic); NSString *appVersion = [infoDic objectForKey:@"CFBundleVersion&

iOS自动检测版本更新

虽然苹果官方是不允许应用自动检测更新,提示用户下载,因为苹果会提示你有多少个软件需要更新,但是有的时候提示用户一下有新版还是很有必要的. 首先说一下原理: 每个上架的苹果应用程序,都会有一个应用程序的ID,根据这个ID我们就可以获取到当前程序的最新版本号,然后和自己的版本号作比较,如果一样的话就是最新版,反之就不是新版,就可以提示用户来手动下载最新版的程序.因为有ID所以就可以定位到这个APP,点击下载即可. 源码: 一般建议检测更新的代码放到主页控制器里. 首先还要导入一个头文件用来打开App

iOS 检测版本更新处理

如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息. 当前运行版本信息可以通过info.plist文件中的bundle version中获取: NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary]; NSString *currentVersion = [infoDic objectForKey:@"CFBundleVersion"]; 这样就获取

android检测版本更新

原理就是从服务器获取版本号和本得apk的版本号对比更新: //检查更新        Activity activity = this;        while(activity.getParent()!=null){            activity = activity.getParent();        }        new UpdateManager(activity, true).checkUpdate(); 下面是UpdateManager.java类: packag

应用在AppStore检测版本更新

// appstore的情况下,版本检查升级的处理 #define  kShiperAppID     @"AppStore中查看AppId" #define  kURLLookup   @"http://itunes.apple.com/cn/lookup?id=%@" #pragma mark - appStore检查更新 - (void)checkUpdate:(id)sender{ __weak typeof(self) wself = self; [MBP

android关于版本更新问题

在Android中,检测版本更新是每个app很常见的功能,我们也尝试简单实现一下吧~ 布局代码这里就不需要写了,一个按钮即可检测更新,这里我是以一个gson文件作为示例请求比对版本... 第一步解析数据: //请求网络服务器上的版本 final String url="http://192.168.253.2:8080/gson/banbenwenjian.json"; HttpUtils utils=new HttpUtils(); utils.configTimeout(5000)