跳转至

iOS APP升级跳转到AppStore

  1. 检测当前app版本
  2. 请求后台接口,判断当前版本是否需要升级
  3. 请求App Store上最新的版本

App Store上是最新的话就跳转到AppStore上。

获取当前app的版本号

//此获取的版本号对应bundle,打印出来对应为12345这样的数字
NSNumber *number = [[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString*)kCFBundleVersionKey];

//此获取的版本号对应version,打印出来对应为1.2.3.4.5这样的字符串
NSString *string = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];

获取app在AppStore上的版本号

https://itunes.apple.com/cn/lookup,在中国的需要加cn否则数据为空。

- (void)getVersionForAppStore:(void (^)(BOOL succeed, NSString *version))block {
    AFHTTPSessionManager *manager  = [AFHTTPSessionManager manager];
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes =  [NSSet setWithObjects:@"text/html",@"text/plain",@"application/json",@"text/javascript",nil];
    NSString *urlStr = [NSString stringWithFormat:@"https://itunes.apple.com/cn/lookup?id=%@",@"1205952707"];
    [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //必须得加判断,因为第一个版本上架之前或者app下架之后,获取不到版本信息。results是空数组。
        if ([responseObject isKindOfClass:[NSDictionary class]]
            && responseObject[@"results"]
            && [responseObject[@"results"] isKindOfClass:[NSArray class]]
            && [responseObject[@"results"] count] > 0
            && [[responseObject[@"results"] firstObject] isKindOfClass:[NSDictionary class]]
            && [responseObject[@"results"] firstObject][@"version"])
        {
            NSArray *results = responseObject[@"results"];
            NSDictionary *dic = results[0];
            NSString *version = dic[@"version"];
            block(YES, version);
            NSLog(@"AppStore上当前app的版本号:%@",version);
        } else {
            block(NO, nil);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@", error);
        block(NO, nil);
    }];
}

跳转到AppStore

AppStore的urlSchemes为itms-apps

NSString *urlStr =@"itms-apps://itunes.apple.com/app/id387682726?mt=8";
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];