蒲公英提示更新版本

一般公司用蒲公英来托管测试版app,而我们公司就是不一般,要把企业app放到蒲公英,活活浪费了企业账号的优势啊。

然后就开始了我的蒲公英的自动更新之路。

首先,我们要先到蒲公英的官网下载sdk。
1,通过cocopods安装framework

pod 'Pgyer'
pod 'PgyUpdate'

2,在项目的AppDelegate.m文件中引入头文件:

import

import

在application:didFinishLaunchingWithOptions 中调用 SDK:

//启动基本SDK
[[PgyManager sharedPgyManager] startManagerWithAppId:@"PGY_APP_ID"];
//启动更新检查SDK
[[PgyUpdateManager sharedPgyManager] startManagerWithAppId:@"PGY_APP_ID"];
其中PGY_APP_ID即在蒲公英上获取的App Key。

3,检查更新

在需要检查更新的文件中引入头文件:

import

如果是使用蒲公英自动检测
然后调用

[[PgyUpdateManager sharedPgyManager] startManagerWithAppId:@"PGY_APP_ID"]; // 请将 PGY_APP_ID 换成应用的 App Key
[[PgyUpdateManager sharedPgyManager] checkUpdate];

这里我用的是自定义方法
[[PgyUpdateManager sharedPgyManager] checkUpdateWithDelegete:self selector:@selector(updateMethod)];
其中updateMethod为检查更新的回调方法。如果有新版本,则包含新版本信息的字典会被回传,否则字典为nil。如果想更新新版本,可通过调用

//蒲公英应用api_Key
#define VERSION_API_KEY @"db78fe9e7698d9bb13c1df9a39b24e7b"
//蒲公英应用User_Key
#define VERSION_User_Key @"ab5b16eca517abcbdb170e1dd87f4504"
//蒲公英应用App_Key
#define PGY_APP_ID @"ffab7b92ad25019d02eda36556640540"
//蒲公英应用页面地址后缀
#define VERSION_Shortcut @"sylibt"
//蒲公英应用itms-services下载
#define VERSION_Itms_Services @"itms-services://?action=download-manifest&url=https%3A%2F%2Fwww.pgyer.com%2Fapiv2%2Fapp%2Fplist%3FappKey%3Dffab7b92ad25019d02eda36556640540%26_api_key%3Ddb78fe9e7698d9bb13c1df9a39b24e7b"



dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSDictionary *dict = @{
                               @"_api_key" : VERSION_API_KEY,
                               @"buildShortcutUrl" : VERSION_Shortcut //应用页面地址后缀
                               };
        
        
        NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:VERSION_HTTPS_SERVER parameters:dict 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;
            }
            SYLog(@"%@ %@", response, responseObject);
            if ([[responseObject valueForKey:@"code"] intValue] == 0) {
                SYLog(@"CDDMall请求成功 appVersion%@,appVersionNo%@",[[responseObject valueForKey:@"data"] valueForKey:@"appVersion"],[[responseObject valueForKey:@"data"] valueForKey:@"appVersionNo"]);
                
                //是否是最新版
//                NSInteger buildIsLastest = [[responseObject valueForKey:@"data"]
//                                            valueForKey:@"buildIsLastest"];
                NSString *newVersion = [[responseObject valueForKey:@"data"] valueForKey:@"buildVersion"];
//                NSString *newBiuld = [[responseObject valueForKey:@"data"] valueForKey:@"buildBuildVersion"];//为@""之前未上传过版本
                //获取本地的版本信息
                NSDictionary *bundleInfo = [[NSBundle mainBundle] infoDictionary];
                NSString *currentVersion = bundleInfo[@"CFBundleShortVersionString"];
//                NSString *currentBulid = bundleInfo[@"CFBundleVersion"];
                //                NSString *beforeVersion = BIULD_VERSION;
                //                NSString *beforeBiuld = BUNDLE_VERSION;
//                SYLog(@"%@----%@--%ld" , newVersion ,newBiuld ,buildIsLastest);
                if([newVersion compare:currentVersion options:NSCaseInsensitiveSearch] == NSOrderedDescending) {
                    SYProjectUpDate *update = [SYProjectUpDate sharedUpdate];
                    update.message =[[responseObject valueForKey:@"data"] valueForKey:@"buildUpdateDescription"];
                    [update alertUpdateForVersion:newVersion withForce:YES isMandatory:YES isPGY:YES];
                }
            }
            
        }];
        [dataTask resume];
    });

在要点更新的地方调用
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:response[@"downloadURL"]]];

你可能感兴趣的:(蒲公英提示更新版本)