iOS程序自動(dòng)提示更新的實(shí)現(xiàn)方案大致分為兩種:
第一種,
自己服務(wù)器提供一個(gè)接口
趟薄,告知相關(guān)app的當(dāng)前版本,是否需要更新典徊,以及更新的地址等信息 杭煎。
第二種,就是利用蘋(píng)果的appstore 提供的相關(guān)api進(jìn)行查詢更新卒落。
注:第一種自己提供服務(wù)器羡铲,需要維護(hù),程序提交更新后儡毕,由于蘋(píng)果需要審核也切,中間會(huì)有時(shí)間差,這個(gè)時(shí)間留意一下腰湾。
請(qǐng)求網(wǎng)絡(luò)數(shù)據(jù),返回的大致數(shù)據(jù)如下雷恃,其他還有好多數(shù)據(jù),這里只把關(guān)鍵的給截取出來(lái)
{
resultCount = 1;
results = (
{
artistId = 開(kāi)發(fā)者ID;
artistName = 開(kāi)發(fā)者名稱;
price = 0;
isGameCenterEnabled = 0;
kind = software;
languageCodesISO2A = ( EN );
trackCensoredName = 審查名稱;
trackContentRating = 評(píng)級(jí);
trackId = 應(yīng)用程序 ID;
trackName = 應(yīng)用程序名稱;
trackViewUrl = 應(yīng)用程序介紹網(wǎng)址;
userRatingCount = 用戶評(píng)級(jí);
userRatingCountForCurrentVersion = 1;
version = 版本號(hào);
wrapperType = software;
}
);
}
區(qū)別 CFBundleShortVersionString & CFBundleVersion
- CFBundleShortVersionString
表重大修改的版本费坊,如實(shí)現(xiàn)新的功能或重大變化的修訂倒槐。第二個(gè)整數(shù)表示的修訂,實(shí)現(xiàn)較突出的特點(diǎn)附井。第三個(gè)整數(shù)代表維護(hù)版本讨越。
- CFBundleVersion
內(nèi)部自己團(tuán)隊(duì)使用的一個(gè)版本號(hào)两残,一般不需要公開(kāi)。
CFBundleShortVersionString
對(duì)應(yīng)Xcode里項(xiàng)目的Version
CFBundleVersion
對(duì)應(yīng)Xcode里項(xiàng)目的Build
注意: 每發(fā)布一個(gè)新應(yīng)用或新版本把跨,蘋(píng)果都要求你輸入一個(gè)版本號(hào)人弓,這個(gè)版本號(hào)對(duì)應(yīng)的是 CFBundleShortVersionString ,不要寫(xiě)錯(cuò)哦节猿。并且票从,如果你上傳成功后(未審核,或未通過(guò))滨嘱,然后又修復(fù)了bug峰鄙,或改了功能,那么在打包發(fā)布時(shí)太雨,CFBundleVersion 必須比上一版本更大吟榴。
打個(gè)比方,我第一次上傳的Version:1.5.1囊扳、Build:3.4.2 ,那我這個(gè)應(yīng)用被拒絕吩翻,修復(fù)好后,我又打包上傳時(shí)锥咸,Version還是1.5.1狭瞎,但Build必須大于3.4.2,可以是3.4.3 搏予、3.4.5熊锭、3.8.5等。 如果Version 1.5.1通過(guò)審核后雪侥,又發(fā)新版本碗殷,那個(gè)下次上傳時(shí),Version要大于1.5.1速缨,但Build可以從新開(kāi)始锌妻,比如1.1.0 。
跳轉(zhuǎn)的兩種方式 (可運(yùn)行的demo)
#import "ViewController.h"
#import <StoreKit/StoreKit.h>
@interface ViewController ()<SKStoreProductViewControllerDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//第一種方法 直接跳轉(zhuǎn)
UIButton *btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)];
btn.backgroundColor = [UIColor redColor];
[btn setTitle:@"直接跳轉(zhuǎn)" forState:UIControlStateNormal];
btn.tag = 1;
[btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
//第二中方法 應(yīng)用內(nèi)跳轉(zhuǎn)
UIButton *btnT = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 50)];
btnT.backgroundColor = [UIColor purpleColor];
btnT.tag = 2;
[btnT setTitle:@"應(yīng)用內(nèi)跳轉(zhuǎn)" forState:UIControlStateNormal];
[btnT addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[self.view addSubview:btnT];
}
- (void)btn:(UIButton *)btn{
if (btn.tag == 1) {
//第一種方法 直接跳轉(zhuǎn)
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"xxxxxxxx"]];
}else{
//第二中方法 應(yīng)用內(nèi)跳轉(zhuǎn)
//1:導(dǎo)入StoreKit.framework,控制器里面添加框架#import <StoreKit/StoreKit.h>
//2:實(shí)現(xiàn)代理SKStoreProductViewControllerDelegate
SKStoreProductViewController *storeProductViewContorller = [[SKStoreProductViewController alloc] init];
storeProductViewContorller.delegate = self;
// ViewController *viewc = [[ViewController alloc]init];
// __weak typeof(viewc) weakViewController = viewc;
//加載一個(gè)新的視圖展示
[storeProductViewContorller loadProductWithParameters:
//appId
@{SKStoreProductParameterITunesItemIdentifier : @"1018221712"} completionBlock:^(BOOL result, NSError *error) {
//回調(diào)
if(error){
NSLog(@"錯(cuò)誤%@",error);
}else{
//AS應(yīng)用界面
[self presentViewController:storeProductViewContorller animated:YES completion:nil];
}
}];
}
}
#pragma mark - 評(píng)分取消按鈕監(jiān)聽(tīng)
//取消按鈕監(jiān)聽(tīng)
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{
[self dismissViewControllerAnimated:YES completion:nil];
}
在蘋(píng)果的 app store 條文中旬牲,是不允許新版本提示的仿粹, 但是只要不讓那些審核員知道有這個(gè)功能就可以了。啟動(dòng)的時(shí)候原茅,在后臺(tái)檢測(cè)一下更新即可牍陌,app store的獲取某個(gè)在線產(chǎn)品的信息是:
https://itunes.apple.com/lookup?id={productID}
上邊的 {productID} 是app 的商店ID。返回的是一串 json 對(duì)象员咽,解析到版本號(hào)即可毒涧。