您的位置:首页 > 移动开发

iOS appStore中的应用 实现版本升级功能

2015-08-22 09:45 447 查看

iOS appStore中的应用 实现升级功能

.h文件中

#import "SBJson.h" //解析sbjson 数据

- (void)viewDidLoad
{
[super viewDidLoad];

⋯⋯

[self checkVersion];   //检测升级

}


.m文件中

#pragma mark - 实现升级功能

//检测软件是否需要升级
-(void)checkVersion
{
NSString *newVersion;
NSURL *url = [NSURL URLWithString:@"http://itunes.apple.com/cn/lookup?id=692579125"];

//通过url获取数据
NSString *jsonResponseString =   [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSLog(@"通过appStore获取的数据是:%@",jsonResponseString);

//解析json数据为数据字典
NSDictionary *loginAuthenticationResponse = [self dictionaryFromJsonFormatOriginalData:jsonResponseString];

//从数据字典中检出版本号数据
NSArray *configData = [loginAuthenticationResponse valueForKey:@"results"];
for(id config in configData)
{
newVersion = [config valueForKey:@"version"];
}

NSLog(@"通过appStore获取的版本号是:%@",newVersion);

//获取本地软件的版本号
NSString *localVersion = [[[NSBundle mainBundle]infoDictionary] objectForKey:@"CFBundleVersion"];

NSString *msg = [NSString stringWithFormat:@"你当前的版本是V%@,发现新版本V%@,是否下载新版本?",localVersion,newVersion];

//对比发现的新版本和本地的版本
if ([newVersion floatValue] > [localVersion floatValue])
{
UIAlertView *createUserResponseAlert = [[UIAlertView alloc] initWithTitle:@"升级提示!" message:msg delegate:self cancelButtonTitle:@"下次再说" otherButtonTitles: @"现在升级", nil];
[createUserResponseAlert show];
[createUserResponseAlert release];
}
}

//响应升级提示按钮
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
//如果选择“现在升级”
if (buttonIndex == 1)
{
//打开iTunes  方法一
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/wan-zhuan-quan-cheng/id692579125?mt=8"]];

/*
// 打开iTunes 方法二:此方法总是提示“无法连接到itunes”,不推荐使用
NSString *iTunesLink = @"itms-apps://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftwareUpdate?id=692579125&mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]];
*/
}
}

#pragma mark - 辅助方法:将json 格式的原始数据转解析成数据字典
//将json 格式的原始数据转解析成数据字典
-(NSMutableDictionary *)dictionaryFromJsonFormatOriginalData:(NSString *)str
{
SBJsonParser *sbJsonParser = [[SBJsonParser alloc]init];
NSError *error = nil;

//添加autorelease 解决 内存泄漏问题
NSMutableDictionary *tempDictionary = [[[NSMutableDictionary alloc]initWithDictionary:[sbJsonParser objectWithString:str error:&error]]autorelease];
return tempDictionary;
}


参考:

//基于企业级证书的IOS应用打包升级功能介绍
http://blog.csdn.net/sbvfhp/article/details/10336715
//另一种代码实现思路
http://hi.baidu.com/wwssttt/item/7446105e3c98fa3933e0a9d5
//向appStore获取软件版本的代码,有步骤
http://blog.csdn.net/wave_1102/article/details/7463697
//向 appstore 查询已发布 APP 的信息--纯思路
http://hi.baidu.com/yanh105/item/7378a98ffca6a8804414cfa0
//官方帮助文档
http://www.apple.com/itunes/affiliates/resources/documentation/itunes-store-web-service-search-api.html
//如何改进iOS客户端的升级提醒功能
http://www.cocoachina.com/applenews/devnews/2013/0108/5495.html
//ios项目如何实现版本更新?
http://blog.csdn.net/mad1989/article/details/8130013
//解决向appStore 发送请求获取版本,没有返回信息的问题
http://www.cocoachina.com/ask/questions/show/56158
原文:http://www.cnblogs.com/ygm900/p/3334586.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息