在前一段時間,蘋果的審覈標準有所變化:在程序中不能出現檢測版本更新的入口。看到這個信息的時候以爲在app中不能有檢測更新或強制更新的功能,最後發現自己還是犯了望文生義的大錯誤。蘋果建議採用系統的自動更新,但在系統的設置裏可以把自動更新關掉。這就導致app有新版本,不能及時被用戶更新,所以檢測版本和版本更新的功能還是必不可少的。但是一定不要有檢測更新的入口呦,如button之類的。否則被蘋果給拒了就自能怪自己了。
開發人員都知道,apple的App有兩種類型:企業版和store版。所以採用的更新方式自然也就有所不同。
1、appStore更新
(1)先介紹一下接口
檢查app應用信息的接口:https://itunes.apple.com/lookup?id=1234567890(id爲自己應用的Apple ID)
返回數據:其中version是最新的版本號,releaseNotes爲新版本的更新內容;
跳轉進入appStore的接口:此接口可以進入iTunes在iTunes store中獲得,步驟如下圖:
<1>搜索應用
<2>右鍵單擊應用
<3>拷貝鏈接,粘貼就得到了 https://itunes.apple.com/cn/app/xin-dong-hui-yi-qing-se-zhi/id655913723?mt=8
(2)代碼實現
在- (void)applicationDidBecomeActive:(UIApplication *)application方法裏寫更新,便於每次程序運行都提示,當然這個也是根據需求來的;
- (void)applicationDidBecomeActive:(UIApplication *)application {
//每次啓動應用的時候檢查更新
[selfchectVersionFromAppStore];
}
具體方法:
- (void)chectVersionFromAppStore
{
NSString *url = [[NSStringalloc]initWithFormat:@"https://itunes.apple.com/lookup?id=16666666"];
[selfrequestURL:url];
}
- (void)requestURL:(NSString *)URL
{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManagermanager];
manager.requestSerializer = [AFHTTPRequestSerializerserializer];
[managerGET:URLparameters:nilsuccess:^(AFHTTPRequestOperation *operation,id responseObject) {
NSDictionary *infoDic = [[NSBundlemainBundle]infoDictionary];
NSString *localVersion = [infoDicobjectForKey:@"CFBundleShortVersionString"];
NSDictionary *versionInfo = [[responseObjectobjectForKey:@"results"]firstObject];
NSString *serverVersion = [versionInfoobjectForKey:@"version"];
NSString *message = [versionInfoobjectForKey:@"releaseNotes"];
if ([selfcompareServerVersion:serverVersion withLocalVersion:localVersion])
{
/* UIAlertController實現
UIAlertController *AlertController = [UIAlertController alertControllerWithTitle:@"發現新版本" message:message preferredStyle:UIAlertControllerStyleAlert];
AppDelegate *delegate = [UIApplication sharedApplication].delegate;
[delegate.window.rootViewController presentViewController:AlertController animated:YES completion:nil];
*/
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"發現新版本"message:nildelegate:selfcancelButtonTitle:@"下載"otherButtonTitles:@"取消",nil];
NSDictionary *attrs =@{NSFontAttributeName : [UIFontsystemFontOfSize:17]};
CGSize size = [messageboundingRectWithSize:CGSizeMake(230,400)options:NSStringDrawingUsesLineFragmentOriginattributes:attrscontext:nil].size;
UITextView *textView = [[UITextViewalloc]initWithFrame:CGRectMake(0,0,200, size.height +10)];
textView.editable =NO;
textView.selectable =NO;
textView.font = [UIFontsystemFontOfSize:15];
textView.backgroundColor = [UIColorclearColor];
textView.text = message;
// iOS8以上系統,充滿整個AlertView,需要調整邊距
if ([selfIOS8])
{
UIEdgeInsets e =UIEdgeInsetsMake(0,20,0, 0);
[textViewsetTextContainerInset:e];
}
[alertViewsetValue:textViewforKey:@"accessoryView"];
alertView.message =@"";
[alertViewshow];
}
}failure:^(AFHTTPRequestOperation *operation,NSError *error) {
}];
}
- (BOOL)IOS8
{
BOOL b =false;
NSString *ver = [[UIDevicecurrentDevice]systemVersion];
if (NSOrderedAscending != [vercompare:@"8.0"]) {
b =true;
}
return b;
}
/**
* 比較服務器版本和本地程序版本。
*
* @param sv 服務器版本
* @param lv 本地版本
*
* @return YES:更新(server version > local version), NO:不更新(數據異常或者server version <= local version)
*/
- (BOOL)compareServerVersion:(NSString*)serverVersion withLocalVersion:(NSString*)localVersion
{
BOOL ret =NO;
if (serverVersion ==nil || [serverVersion isEqualToString:@""]) {
NSLog(@"Server version is empty.");
return ret;
}
if (localVersion ==nil || [localVersion isEqualToString:@""]) {
NSLog(@"Local version is empty.");
return ret;
}
NSArray *serComps = [serverVersioncomponentsSeparatedByString:@"."];
NSArray *locComps = [localVersioncomponentsSeparatedByString:@"."];
for (int index =0; index < serComps.count; index++) {
int numServer = [[serCompsobjectAtIndex:index] intValue];
int numLocal = -1;
if (locComps.count > index) {
numLocal = [[locCompsobjectAtIndex:index]intValue];
}
if (numServer > numLocal) {
ret =YES;
break;
}elseif (numServer < numLocal) {
ret =NO;
break;
}
}
return ret;
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex == alertView.cancelButtonIndex) {
[[UIApplicationsharedApplication]openURL:[NSURLURLWithString:@"https://itunes.apple.com/cn/app/zhi-dong-sheng-huo/id=853522946?mt=8"]];
}
}
2、服務器版本下載和更新
用此功能必須具備以下條件:
<1>蘋果企業開發者賬號和企業證書
<2>支持https的服務器,當然沒有也可以(利用第三方的)
<3>下載頁面(負責找到plist文件,並提供給用戶良好的頁面展示)
<4>plist文件(負責下載ipa包)
如果沒有支持https的服務器,七牛雲存儲是個很好的代理,下來看看它的真面目吧。
進入七牛官網:http://www.qiniu.com,然後註冊成爲標準用戶,就可以上傳plist文件了
註冊成功後,新建空間
點擊存在的空間,進入空間,再點擊內容管理,然後上傳plist文件
查看自己空間的URL,配置在自己的html頁面裏:
到此,路已經走通,下面來看一下plist文件的結構
注意: bundle-identifier的Value中在bundleId後增加.fixios8是爲了解決ios8剛出現的時候會安裝成兩個app的問題,後來問題沒有了,所以以後直接寫bundleId就ok了。
html的具體配置
代碼的具體實現和appStore的實現類似,只不過版本信息是通過後臺接口傳過來。so,這裏就不寫了,參照上面的代碼。