App版本检查更新

2020-02-26  本文已影响0人  追梦小怪兽

1.需要准备你的App版本更新的url,通过url去获取你需要的数据:http://itunes.apple.com/lookup?id=你的AppID
由于发布了新版本,但是接口没有拿到最新的版本号。这个url 需要注意下:如果你的应用是只发布到中国区那么接口需要添加 /cn:
http://itunes.apple.com/cn/lookup?id=你的AppID
如果没有拿到数据,或者没有拿到最新数据。需要用https,所以最后用下面的接口(modify 2020年05月12日09:57:00)
https://itunes.apple.com/lookup?id=你的AppID

    NSString *url = url // 这里的url是你上面准备的那个url;
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/javascript", nil];

    [manager POST:url parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSString *newVersion = responseObject[@"results"][0][@"version"];//获取版本号
//        NSLog(@"%@",responseObject);
        BOOL result = [self compareVersionWithNewVersion:newVersion];
        if (result) {
            // 需要更新
            // 进行弹窗
        }else{
            NSLog(@" 已经是最新版本");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
  1. 这里会有一个版本号的对比
- (BOOL) compareVersionWithNewVersion:(NSString *) newVersionStr{
    //从info.plist文件获取Version
    NSString *version = [[[NSBundle mainBundle] infoDictionary]objectForKey:@"CFBundleShortVersionString"];
    NSInteger localVersion = [[version stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
    //最新版本号
    NSInteger newVersion = [[newVersionStr stringByReplacingOccurrencesOfString:@"." withString:@""] integerValue];
    //这里的话,版本号可能类似:1.2,1.2.1,(如果是1.2.22 和 1.3 这样的比较需要注意补齐位数)
    newVersion = newVersion < 100 ? newVersion *= 10 : newVersion;
    localVersion = localVersion < 100 ? localVersion *= 10 : localVersion;
    
    return localVersion < newVersion ? 1 : 0;
}

---来自涛胖子的工作笔记

上一篇 下一篇

猜你喜欢

热点阅读