场景
在我们使用应用时,一打开应用,如果此应用有新的版本,常常能在应用中给出提示,是否要更新此应用。所以,我们就来看看,版本更新是如何实现的。
应用
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://itunes.apple.com/lookup?id=%@",appleID]]];
[request setHTTPMethod:@"GET"];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:nil];
这里,我们通过同步请求,解析json数据,得到了数据。
NSString *latestVersion = [releaseInfo objectForKey:@"version"];
NSString *trackViewUrl1 = [releaseInfo objectForKey:@"trackViewUrl"];//地址trackViewUrl
NSString *trackName = [releaseInfo objectForKey:@"trackName"];//trackName
获取此应用的版本号
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
通过latestVersion和currentVersion的比较,来判断是否有新的更新。
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *currentVersion = [infoDict objectForKey:@"CFBundleVersion"];
double doubleCurrentVersion = [currentVersion doubleValue];
if (doubleCurrentVersion < doubleUpdateVersion) {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:trackName
message:@"有新版本,是否升级!"
delegate: self
cancelButtonTitle:@"取消"
otherButtonTitles: @"升级", nil];
alert.tag = 1001;
[alert show];
}
else{
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:trackName
message:@"暂无新版本"
delegate: nil
cancelButtonTitle:@"好的"
otherButtonTitles: nil, nil];
[alert show];
}
如果有新的版本,那么就跳转至下载页面,这里就用到了trackViewUrl,trackViewUrl是全路径,直接请求。
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:trackViewUrl]];
还有另外一种方式,就是线下发布应用,虽然这种方式很容易被苹果毙掉,但是也有用的可能,利用企业服务器进行版本的升级
//网络请求确认是否需要更新
-(void)postManagerReturn{
int versionNos = [[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] intValue];
NSMutableDictionary *param = [NSMutableDictionary dictionary];
[param setValue:@"29" forKey:@"CLIENT_ID"];
[param setValue:@"1111111111111" forKey:@"IMEI"];
[param setValue:@"1111111111111" forKey:@"IMSI"];
[_manager POST:APP_UPDATE parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
NSArray *versionList = [dic valueForKey:@"versionList"];
if (versionList.count>0) {
NSDictionary *AppUpdateInfo = versionList[0];
NSString *md5 = [AppUpdateInfo valueForKey:@"md5"];
id version_no = [AppUpdateInfo valueForKey:@"version_no"];
int getVersionNo = [version_no intValue];
if (getVersionNo>versionNos) {
CMVersionUpdate *versionTarget = [[CMVersionUpdate alloc]init];
[versionTarget downLoadplist:md5];
}
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];
}
-(void)downLoadplist:(NSString *)md5{
NSURL * nsUrl = [NSURL URLWithString:[NSString stringWithFormat:@"itms-services://?action=download-manifest&url=https://www.tencent.com/plist/testIOS.plist"]];
[[UIApplication sharedApplication]openURL:nsUrl];
}
关于
无需AppStore,无线发布iOS应用(OTA)
参考:http://blog.sina.com.cn/s/blog_8280f5ec0101fr5x.html
一、OTA (Over The Air)
OTA 是苹果在 iOS4 中新增的一项功能,目的是让企业用户能够在脱离 Apple App Store 的情况下通过网页无线发布 iOS 应用;简单来讲就是在 Safari 中点击一个链接就可以在 SpringBoard 上下载并安装应用(目前很多越狱软件都是使用的这种发布方式)。
二、实现方法
想要实现 OAT 发布,你需要购买一个苹果的企业版证书,价格 $299 / Year。
当然如果只针对越狱用户,那么个人版的证书也是可以的。
具体实现:
-
使用 Xcode 生成 .ipa 安装包
因为我是负责前端部分,并没有涉及客户端的开发,这里只能列出简单的步骤,步骤是基于 Xcode 3.x 的,所以与最新版的 Xcode 4.5 有些出入,如果你对这部分比较了解,欢迎在留言中分享下实现步骤,以方便其他的开发者:)。- 使用 Build and Archive 打包方式
- 然后在 Xcode 的 Organizer 中查看已经归档的 Applications,并选择 Share Application…
- 在弹出的分享对话框中,选中 iPhone Distribution ,并点击 Distribution for Enterprise… 按键
- 在弹出的输入框中,输入必要的 ipa、iTunesArtwork、icon.png 的合法 URL 信息,然后点击 OK。(这里一定要保证输入的 URL 与将要部署的环境一致)
- 这时会生成 .ipa 和 .plist 为后缀的两个文件,最后搭配相应的 iTunesArtwork 和icon.png 上传到网站特定目录。
参考的文章:
-
创建发布页面
OTA (Over The Air) 方式安装是依赖于 Safari 解析itms-services://
协议实现的,看下面这个例子:Install App
这就是完整的 OTA 发布方式的链接,其中 ota.plist 就是 Xcode 打包时生成的 .plist 文件。
.plist 用于记录应用的一些重要信息,如:应用包 URL、应用 icon URL、名称、版本等,该文件也可自己编写,详细的代码可以查看 Apple 官方文档:Sample app manifest file
三、Demo
最后来看个 Demo,因为我没有苹果的个人证书,所以是直接找一个 .ipa 应用并自己编写了一个配套的 .plist 文件。如此一来,.ipa 中的打包信息就与 .plist 中的信息不符,有可能出现无法下载的情况,也有可能出现能够下载,但安装完后应用的 icon 从 SpringBoard 上消失的问题(需要重载 SpringBoard 才能看到),当然使用 Xcode 打包就不会有这些问题。
越狱用户可以点击体验Demo,安装的软件是一个手电筒软件 Everlight,无害:)
<wbr></wbr>
转自:http://www.iinterest.net/2011/09/22/installing-apps-wirelessly-ipa-ota/