一 ,首先查看当前应用程序的最新版本
1.如果是AppStore上的应用
发送POST请求:http://itunes.apple.com/lookup?id=应用程序ID
如果是企业自己的服务器,就发送到该企业的服务器,以下代码是我正在做的项目
- (
void
)checkVersion
{
// 发送网络请求
NSURL *url = [ NSURL URLWithString : @"http://218.206.179.179/ctp/ipauto/getLastedVersionInfo.do?code=000&type=release" ];
ASIHTTPRequest *requst = [ ASIHTTPRequest requestWithURL :url];
[requst startAsynchronous ];
requst. delegate = self ;
}
{
// 发送网络请求
NSURL *url = [ NSURL URLWithString : @"http://218.206.179.179/ctp/ipauto/getLastedVersionInfo.do?code=000&type=release" ];
ASIHTTPRequest *requst = [ ASIHTTPRequest requestWithURL :url];
[requst startAsynchronous ];
requst. delegate = self ;
}
2.解析服务器返回的数据得到最新版本并与当前版本比较
- (
void
)requestFinished:(
ASIHTTPRequest
*)request
{
NSDictionary * dic = [ NSJSONSerialization JSONObjectWithData :[request responseData ] options : NSJSONReadingMutableContainers error : NULL ];
NSArray *detail = [dic objectForKey : @"Detail" ];
NSString *latestVersion = [[detail firstObject ] objectForKey : @"version" ];
NSString *appVersion = [ self currentVersion ];
NSLog ( @"---%s----,-----%d------,%@" , __FUNCTION__ ,__LINE__,appVersion);
if (![appVersion isEqualToString :latestVersion]) {
UIAlertView *alertView = [[ UIAlertView alloc ] initWithTitle : @" 监测到有新版本 " message: nil delegate : self cancelButtonTitle : @" 忽略 " otherButtonTitles: @" 立即更新 " , nil ];
[alertView show ];
}
}
{
NSDictionary * dic = [ NSJSONSerialization JSONObjectWithData :[request responseData ] options : NSJSONReadingMutableContainers error : NULL ];
NSArray *detail = [dic objectForKey : @"Detail" ];
NSString *latestVersion = [[detail firstObject ] objectForKey : @"version" ];
NSString *appVersion = [ self currentVersion ];
NSLog ( @"---%s----,-----%d------,%@" , __FUNCTION__ ,__LINE__,appVersion);
if (![appVersion isEqualToString :latestVersion]) {
UIAlertView *alertView = [[ UIAlertView alloc ] initWithTitle : @" 监测到有新版本 " message: nil delegate : self cancelButtonTitle : @" 忽略 " otherButtonTitles: @" 立即更新 " , nil ];
[alertView show ];
}
}
二.如何获取当前版本
1.常用的,得到字符串
- (
NSString
*)currentVersion
{
// 获取当前版本字符串
NSDictionary *infoDic = [[ NSBundle mainBundle ] infoDictionary ];
NSString *currentVer = [infoDic objectForKey :( NSString *) kCFBundleVersionKey ];
return currentVer;
}
{
// 获取当前版本字符串
NSDictionary *infoDic = [[ NSBundle mainBundle ] infoDictionary ];
NSString *currentVer = [infoDic objectForKey :( NSString *) kCFBundleVersionKey ];
return currentVer;
}
2.获取版本号,数字
UInt32
versionNum =
CFBundleGetVersionNumber
(
CFBundleGetMainBundle
());
不过,目前还不知道这个值用到哪里,发现AppStore返回的json没有相应的字段
三.打开新版本的下载页面
如果是在AppStore上的,可以点击按钮时,打开相应下载页面
企业自己发布的,可以使用Safari打开下载页面
这里关键是使用哪个URL
-(
void
)alertView:(
UIAlertView
*)alertView didDismissWithButtonIndex:(
NSInteger
)buttonIndex
{
if (buttonIndex == 1 ) {
NSURL *url = [ NSURL URLWithString : @"http://218.207.242.142:8080/ots/beta.html" ];
[[ UIApplication sharedApplication ] openURL :url];
}
}
{
if (buttonIndex == 1 ) {
NSURL *url = [ NSURL URLWithString : @"http://218.207.242.142:8080/ots/beta.html" ];
[[ UIApplication sharedApplication ] openURL :url];
}
}
打开AppStore如下
NSString
*str = [
NSString
stringWithFormat
:
@"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%@"
,
@"12345325"
];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];