如何通过蒲公英实现iOS版本自动提示更新,省去频繁打内测版本发链接给同事老板

一:xcode项目中需要做的操作
1.代码中设置bulid号
这里写图片描述

2.在xcode中添加指定脚本(确保每次打版bulid号会自动+1,这样可以确保每次打包的bulid号>蒲公英上曾经打版的号)

if [ "$CONFIGURATION" != "Debug" ]
then
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")
buildNumber=$(($buildNumber + 1))
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$INFOPLIST_FILE"
fi

//写脚本位置截图
脚本代码截图

如果没有Run Version 选项,按照下图点击“+”创建
这里写图片描述

3.项目必须已经添加到蒲公英上,并把蒲公英上”对应项目”的Appid(kPGYApiKey)Apikey放到xcode 的宏定义文件中
在iOS 项目最先进入的控制器(一般是homeVC或者登陆页)添加提示更新的代码

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
     [self hasUpdateVersion];
}

//如果有最新版本上传到蒲公英,提示更新
- (void)hasUpdateVersion{
    kWeakSelf(self);
    NSDictionary *infoDic=[[NSBundle mainBundle] infoDictionary];
    NSString *currentBulidVersion=infoDic[@"CFBundleVersion"];

    //蒲公英的apikey,appkey
    NSDictionary *paramDic = @{@"_api_key":kPGYApiKey,@"appKey":kPGYAppKey};
    [self.homeLogic loadUpdateWithDic:paramDic success:^(id response) {
        RLog(@"更新信息");
        if ([currentBulidVersion integerValue]<[response[@"data"][@"buildVersionNo"]integerValue]) {
        //如果当前手机安装app的bulid号<蒲公英上最新打包的bulid号,则提示更新
            UIAlertController *ac = [UIAlertController alertControllerWithTitle:@"版本有更新" message:@"检测到新版本,是否更新?"  preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
            [ac addAction:cancelAction];
            UIAlertAction *doneAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                NSURL *url = [NSURL URLWithString:response[@"data"][@"buildShortcutUrl"]];
                [[UIApplication sharedApplication] openURL:url];
            }];
            [ac addAction:doneAction];
            [weakself presentViewController:ac animated:YES completion:nil];

        }
    }];

//蒲公英版本更新,蒲公英检查版本号api:https://www.pgyer.com/apiv2/app/check

- (void)loadUpdateWithDic:(NSDictionary *)dic success:(void(^)(id response))success {
    [PPNetworkHelper POST:@"https://www.pgyer.com/apiv2/app/check" parameters:dic success:^(id responseObject) {
        RLog(@"版本更新%@",responseObject);
        success(responseObject);
    } failure:^(NSError *error) {
        RLog(@"搜版本更新请求失败");
    }];
}

//蒲公英平台上对应的AppKey,ApiKey
蒲公英项目对应Appkey,Apikey

//接口入口:蒲公英-文档-API2.0-检测App是否有更新
网络请求api

//蒲公英中请求蒲公英上版本是都有更新的api详情截图
api详情

//自动更新代码截图
代码截图

二:蒲公英平台上的设置:
4.如果没有勾选bulid号自动+1,那么勾选下,确保当前最新的bulid号

扫描二维码关注公众号,回复: 2516137 查看本文章

猜你喜欢

转载自blog.csdn.net/wei371522/article/details/79885256