iOS 检测版本更新
2014-10-16 21:11
344 查看
如果我们要检测app版本的更新,那么我们必须获取当前运行app版本的版本信息和appstore 上发布的最新版本的信息。
当前运行版本信息可以通过info.plist文件中的 version中获取:
NSDictionary *infoDic = [[NSBundlemainBundle]infoDictionary];
NSString *currentVersion = [infoDic
objectForKey:@"CFBundleShortVersionString"];
这样就获取到当前运行的app的版本了
具体步骤如下:
1,用 POST 方式发送请求:
中国的app 就需要在https://itunes.apple.com后面加上/cn
然后加上 /lookup?id=959208445 就好了
http://itunes.apple.com/lookup?id=你的应用程序的ID
#define APP_URL http://itunes.apple.com/lookup?id=你的应用程序的ID
你的应用程序的ID 是 itunes connect里的 Apple ID
NSString *URL =@"http://itunes.apple.com/lookup";
NSMutableURLRequest *request = [[NSMutableURLRequestalloc]init];
[request setURL:[NSURLURLWithString:URL]];
[request setHTTPMethod:@"POST"];
// ? 数据体
NSString *str = [NSStringstringWithFormat:@"id=你的应用程序的ID"];
// 将字符串转换成数据
request.HTTPBody = [strdataUsingEncoding:NSUTF8StringEncoding];
NSURLConnection *connection = [NSURLConnectionconnectionWithRequest:requestdelegate:self];
// 开始工作,在很多多线程技术中,start run
dispatch_async(dispatch_queue_create("demo",DISPATCH_QUEUE_CONCURRENT), ^{
[connection start];
});
NSURLConnection 代理中获取数据处理数据-这里不多说了。。
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];
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:requestreturningResponse:&urlResponse error:&error];
NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedDatalength] 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:self cancelButtonTitle:@"确定" 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:@"软件在appstore中的链接"];
[[UIApplication sharedApplication]openURL:url];
}
}
}
// 软件在appstore中的链接
找到应用程序的描述链接,比如:
http://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8
然后将 http:// 替换为 itms://
或者
itms-apps://:
itms://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8
itms-apps:// itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8
然后打开这个链接地址:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps ://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8"]];
当前运行版本信息可以通过info.plist文件中的 version中获取:
NSDictionary *infoDic = [[NSBundlemainBundle]infoDictionary];
NSString *currentVersion = [infoDic
objectForKey:@"CFBundleShortVersionString"];
这样就获取到当前运行的app的版本了
具体步骤如下:
1,用 POST 方式发送请求:
中国的app 就需要在https://itunes.apple.com后面加上/cn
然后加上 /lookup?id=959208445 就好了
http://itunes.apple.com/lookup?id=你的应用程序的ID
#define APP_URL http://itunes.apple.com/lookup?id=你的应用程序的ID
你的应用程序的ID 是 itunes connect里的 Apple ID
NSString *URL =@"http://itunes.apple.com/lookup";
NSMutableURLRequest *request = [[NSMutableURLRequestalloc]init];
[request setURL:[NSURLURLWithString:URL]];
[request setHTTPMethod:@"POST"];
// ? 数据体
NSString *str = [NSStringstringWithFormat:@"id=你的应用程序的ID"];
// 将字符串转换成数据
request.HTTPBody = [strdataUsingEncoding:NSUTF8StringEncoding];
NSURLConnection *connection = [NSURLConnectionconnectionWithRequest:requestdelegate:self];
// 开始工作,在很多多线程技术中,start run
dispatch_async(dispatch_queue_create("demo",DISPATCH_QUEUE_CONCURRENT), ^{
[connection start];
});
NSURLConnection 代理中获取数据处理数据-这里不多说了。。
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];
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:requestreturningResponse:&urlResponse error:&error];
NSString *results = [[NSString alloc] initWithBytes:[recervedData bytes] length:[recervedDatalength] 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:self cancelButtonTitle:@"确定" 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:@"软件在appstore中的链接"];
[[UIApplication sharedApplication]openURL:url];
}
}
}
// 软件在appstore中的链接
找到应用程序的描述链接,比如:
http://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8
然后将 http:// 替换为 itms://
或者
itms-apps://:
itms://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8
itms-apps:// itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8
然后打开这个链接地址:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8"]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps ://itunes.apple.com/gb/app/yi-dong-cai-bian/id391945719?mt=8"]];
相关文章推荐
- IOS检测版本更新(***为app id)
- iOS通过iTunes search检测版本更新,并提示用户更新!
- iOS通过iTunes search检测版本更新,并提示用户更新!
- iOS 检测版本更新
- iOS通过iTunes search检测版本更新,并提示用户更新!
- iOS通过iTunes search检测版本更新,并提示用户更新!
- IOS检测版本更新(***为app id)
- iOS通过iTunes search检测版本更新,并提示用户更新!
- iOS 检测版本更新
- iOS通过iTunes search检测版本更新,并提示用户更新!
- iOS通过iTunes search检测版本更新,并提示用户更新!
- iOS通过iTunes search检测版本更新,并提示用户更新!
- iOS通过iTunes search检测版本更新,并提示用户更新!
- iOS 检测版本更新
- 转载 -- iOS通过iTunes search检测版本更新,并提示用户更新!
- iOS通过iTunes search检测版本更新,并提示用户更新!
- IOS检测版本更新
- iOS开发 检测版本更新
- IOS 版本更新检测
- iOS通过iTunes search检测版本更新,并提示用户更新!