蒲公英App开发之检测新版本

https://www.jianshu.com/p/2d3f048511d7

2017.04.17 16:22* 字数 62 阅读 422评论 0喜欢 1

可以在App内部实现检测版本更新并实现安装。

pastedGraphic.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]];

                                        }

                                    }];

                                });

                            }

                            // 当前是最新版本

                            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

应用上传时间

参考文档

蒲公英API

原文地址:https://www.cnblogs.com/sundaysgarden/p/10318563.html