蒲公英自动检测更新

我们所知在产品上架之前,肯定会经过N+1次的更新测试。一般我们所熟知的测试平台如蒲公英,今天我在这里主要分享一个蒲公英植入APP内部自动检测更新的方法,这个使我们可很方便的及时更新最新版本的测试包。如下图

蒲公英自动检测更新_第1张图片
自动检测

实现 鉴于CDDMall是我的github开源项目,所以以下展示代码和截图上就不打码了。

自定义宏

/*****************  蒲公英更新  ******************/
//版本控制
#define VERSION_HTTPS_SERVER @"http://www.pgyer.com/apiv1/app/getAppKeyByShortcut"
//版本更新内容
#define VERSION_HTTPS_INFO @"http://www.pgyer.com/apiv1/app/view"
//蒲公英应用api_Key
#define VERSION_API_KEY @"e08ad9d9e8fb4dee72c44cb486be09c0"
//蒲公英应用User_Key
#define VERSION_User_Key @"eb79e5278779732ec8634c0c21d304ed"
//蒲公英应用页面地址后缀
#define VERSION_Shortcut @"Mall"
//蒲公英应用itms-services下载
#define VERSION_Itms_Services @"itms-services://?action=download-manifest&url=https%3A%2F%2Fwww.pgyer.com%2Fapiv1%2Fapp%2Fplist%3FaId%3Db15ba9b695d00c14e1361c24c1869a17%26_api_key%3De08ad9d9e8fb4dee72c44cb486be09c0"
//获取当前版本号
#define BUNDLE_VERSION [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]
//获取当前版本的biuld
#define BIULD_VERSION [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];

解释

蒲公英自动检测更新_第2张图片
定义宏1
蒲公英自动检测更新_第3张图片
定义宏2

请求代码在AppDelegate的didFinishLaunchingWithOptions调用

#pragma mark - 蒲公英版本更新检测
- (void)CDDMallVersionInformationFromPGY
{
    NSDictionary *dict = @{
                          @"shortcut" : VERSION_Shortcut,  //应用页面地址后缀
                          @"_api_key" : [NSString stringWithFormat:@"%@",VERSION_API_KEY]
                          };
    
    [RequestTool requestWithType:POST URL:VERSION_HTTPS_SERVER parameter:dict successComplete:^(id responseObject) {
        
        if ([[responseObject valueForKey:@"code"] intValue] == 0) {
            NSLog(@"CDDMall请求成功 appVersion%@,appVersionNo%@",[[responseObject valueForKey:@"data"] valueForKey:@"appVersion"],[[responseObject valueForKey:@"data"] valueForKey:@"appVersionNo"]);
            
            NSString *newVersion = [[responseObject valueForKey:@"data"] valueForKey:@"appVersion"];
            NSString *newBiuld = [[responseObject valueForKey:@"data"] valueForKey:@"appVersionNo"]; //为@""之前未上传过版本
            NSString *beforeVersion = BIULD_VERSION;
            NSString *beforeBiuld = BUNDLE_VERSION;
            
            if ((![newVersion isEqualToString:beforeVersion] || ![newBiuld isEqualToString:beforeBiuld] ) && ![newBiuld isEqualToString:@""]){
                
                NSDictionary *dict = @{
                                       @"uKey" : VERSION_User_Key,
                                       @"_api_key" : [NSString stringWithFormat:@"%@",VERSION_API_KEY],
                                       @"aKey" : [[responseObject valueForKey:@"data"] valueForKey:@"appKey"]
                                       };
                
                [RequestTool requestWithType:POST URL:VERSION_HTTPS_INFO parameter:dict successComplete:^(id responseObject) {
                    
                    UIWindow *alertWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
                    alertWindow.rootViewController = [[UIViewController alloc] init];
                    alertWindow.windowLevel = UIWindowLevelAlert + 1;
                    [alertWindow makeKeyAndVisible];
                    
                    [DCSpeedy dc_SetUpAlterWithView:alertWindow.rootViewController Message:[NSString stringWithFormat:@"CDDMall有新版本,请前往更新\n更新内容:%@",[[responseObject valueForKey:@"data"] valueForKey:@"appUpdateDescription"]] Sure:^{
                        
                        //现在绑定
                        NSURL *url = [NSURL URLWithString:VERSION_Itms_Services];
                        [[UIApplication sharedApplication] openURL:url];
                        
                    } Cancel:nil];
                    
                } failureComplete:^(NSError *error) {
                    
                }];
            }
        }

    } failureComplete:^(NSError *error) {
        
        NSLog(@"蒲公英请求失败~");
        
    }];
}

gif操作演示

真机测试
  • 一个很简单且非常实用的内测功能,蒲公英检测更新就植入成功了。

你可能感兴趣的:(蒲公英自动检测更新)