可以在App內(nèi)部實(shí)現(xiàn)檢測(cè)版本更新并實(shí)現(xiàn)安裝。
![](https://static.pgyer.com/static-20170411/assets/img/banner_macbook.png)
核心代碼
#define API_PGYER_UPDATE_URL @"https://www.pgyer.com/apiv1/app/builds"
#define PGY_API_KEY @""
#define PGY_APP_ID @""
- (void)checkForUpdates {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSDictionary *parameters = @{@"aId":PGY_APP_ID,@"page":@1,@"_api_key":PGY_API_KEY};
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:API_PGYER_UPDATE_URL parameters:parameters error:nil];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
MJWeakSelf
NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (error) {
NSLog(@"Error: %@", error);
return;
}
NSLog(@"%@ %@", response, responseObject);
if (responseObject && [responseObject isKindOfClass:[NSDictionary class]]) {
NSDictionary *responseDictionary = (NSDictionary *)responseObject;
if (responseDictionary && [responseDictionary objectForKey:@"data"]) {
NSDictionary *data = [responseDictionary objectForKey:@"data"];
if (!data || data.count == 0) {
return;
}
NSArray *array = [data objectForKey:@"list"];
if (array) {
BFLarkAppDeatilModel *appDeatil = [BFLarkAppDeatilModel mj_objectArrayWithKeyValuesArray:array].firstObject;
if (appDeatil) {
// 有新版本
if (appDeatil.appVersionNo.integerValue > [[[UIApplication sharedApplication] appBuildVersion] integerValue]) {
dispatch_async(dispatch_get_main_queue(), ^{
[BFAlertView showAlertInViewController:weakSelf.view withTitle:[NSString stringWithFormat:@"Current BuildVersion %@, New BuildVersion %@",[[UIApplication sharedApplication] appBuildVersion],appDeatil.appVersionNo] message:@"舊版就像初戀源请,很美但再也回不去恍风,快去收獲新歡" cancelButtonTitle:@"取消" destructiveButtonTitle:@"殘忍拒絕" otherButtonTitles:@[@"欣然前往"] tapBlock:^(RMUniversalAlert * _Nonnull alert, NSInteger buttonIndex) {
// 去更新
if (buttonIndex == 2) {
NSString *urlString = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=https://www.pgyer.com/app/plist/%@",appDeatil.appKey];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
}];
});
}
// 當(dāng)前是最新版本
else {
dispatch_async(dispatch_get_main_queue(), ^{
// 更新界面
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"您的版本是最新的" delegate:nil cancelButtonTitle:@"確定" otherButtonTitles:nil, nil];
[alertView show];
});
}
}
}
}
}
}];
[dataTask resume];
});
}
BFLarkAppDeatilModel
#import <Foundation/Foundation.h>
@interface BFLarkAppDeatilModel : NSObject
@property (nonatomic, copy) NSString *appKey;
@property (nonatomic, copy) NSString *appVersion;
@property (nonatomic, copy) NSString *appBuildVersion;
@property (nonatomic, copy) NSString *appName;
@property (nonatomic, copy) NSString *appIcon;
@property (nonatomic, copy) NSString *appCreated;
@property (nonatomic, copy) NSString *appFileSize;
@property (nonatomic, copy) NSString *appIdentifier;
@property (nonatomic, copy) NSString *appType;
@property (nonatomic, copy) NSString *appVersionNo;
@end
API詳解
獲取App所有版本
API地址
POST http://www.pgyer.com/apiv1/app/builds
POST參數(shù)
參數(shù) | 類型 | 說(shuō)明 |
---|---|---|
aId | String | App Id |
page | Integer | 歷史版本分頁(yè)頁(yè)數(shù) |
_api_key | String | API Key |
返回?cái)?shù)據(jù)
返回參數(shù) | 類型 | 說(shuō)明 |
---|---|---|
appKey | String | 返回應(yīng)用最新build的App Key |
appType | Integer | 應(yīng)用類型(1:iOS; 2:Android |
appFileSize | Integer | App 文件大小 |
appName | String | 應(yīng)用名稱 |
appVersion | String | 版本號(hào) |
appVersionNo | Integer | 適用于Android的版本編號(hào)挤悉,iOS始終為0 |
appBuildVersion | Integer | 蒲公英生成的用于區(qū)分歷史版本的build號(hào) |
appIdentifier | String | 應(yīng)用程序包名,iOS為BundleId,Android為包名 |
appIcon | String | 應(yīng)用的Icon圖標(biāo)key,訪問(wèn)地址為 http://o1wh05aeh.qnssl.com/image/view/app_icons/[應(yīng)用的Icon圖標(biāo)key] |
appCreated | String | 應(yīng)用上傳時(shí)間 |