https://www.jianshu.com/p/2d3f048511d7
2017.04.17 16:22* 字數 62 閱讀 422評論 0喜歡 1
可以在App內部實現檢測版本更新並實現安裝。
核心代碼
#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]];
}
}];
});
}
// 當前是最新版本
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參數
參數 |
類型 |
說明 |
aId |
String |
App Id |
page |
Integer |
歷史版本分頁頁數 |
_api_key |
String |
API Key |
返回數據
返回參數 |
類型 |
說明 |
appKey |
String |
返回應用最新build的App Key |
appType |
Integer |
應用類型(1:iOS; 2:Android |
appFileSize |
Integer |
App 文件大小 |
appName |
String |
應用名稱 |
appVersion |
String |
版本號 |
appVersionNo |
Integer |
適用於Android的版本編號,iOS始終為0 |
appBuildVersion |
Integer |
蒲公英生成的用於區分歷史版本的build號 |
appIdentifier |
String |
應用程序包名,iOS為BundleId,Android為包名 |
appIcon |
String |
應用的Icon圖標key,訪問地址為 http://o1wh05aeh.qnssl.com/image/view/app_icons/[應用的Icon圖標key] |
appCreated |
String |
應用上傳時間 |
參考文檔