一.判斷是不是WiFi沥潭,如果是WiFi則提醒用戶更新
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'PingFang SC'; color: #008400}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #31595d}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}span.s1 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s2 {font-variant-ligatures: no-common-ligatures}span.s3 {font: 11.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s4 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s5 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s6 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s7 {font-variant-ligatures: no-common-ligatures; color: #ba2da2}span.s8 {font-variant-ligatures: no-common-ligatures; color: #3e1e81}span.s9 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}
////////////////////////////////////////////////////////////////版本更新開始
//先判斷是不是WiFi,如果是WiFi則提醒用戶更新
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
if ([AFStringFromNetworkReachabilityStatus(status) isEqualToString:@"Reachable via WiFi"]) [self update];
}];
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
/////////////////////////////////////////////////////////////////版本更新結(jié)速
二.把更新的代碼重構(gòu)到一個方法里
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; min-height: 13.0px}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #1337ff}p.p4 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #d12f1b}p.p5 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #4f8187}p.p6 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008400}p.p7 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px 'PingFang SC'; color: #008400}p.p8 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3e1e81}span.s1 {font-variant-ligatures: no-common-ligatures}span.s2 {font-variant-ligatures: no-common-ligatures; color: #ba2da2}span.s3 {font-variant-ligatures: no-common-ligatures; color: #703daa}span.s4 {font-variant-ligatures: no-common-ligatures; color: #3e1e81}span.s5 {font-variant-ligatures: no-common-ligatures; color: #d12f1b}span.s6 {font-variant-ligatures: no-common-ligatures; color: #000000}span.s7 {font-variant-ligatures: no-common-ligatures; color: #008400}span.s8 {font-variant-ligatures: no-common-ligatures; color: #1337ff}span.s9 {font-variant-ligatures: no-common-ligatures; color: #31595d}span.s10 {font: 11.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures}span.s11 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures; color: #000000}span.s12 {font: 11.0px Menlo; font-variant-ligatures: no-common-ligatures}span.s13 {font-variant-ligatures: no-common-ligatures; color: #272ad8}span.s14 {font: 11.0px 'PingFang SC'; font-variant-ligatures: no-common-ligatures; color: #d12f1b}
- (void)update {
NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
NSString *oldVersion = infoDict[@"CFBundleShortVersionString"];
////https://itunes.apple.com/cn/app/xing-tu/id1126715226?mt=8
NSString *url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@", @"1126715226"];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
//申明返回的結(jié)果是json類型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明請求的數(shù)據(jù)是json類型
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//如果報(bào)接受類型不一致請?zhí)鎿Q一致text/html或別的
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/javascript", nil nil];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/javascript"];
[manager POST:url parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSNumber *number = responseObject[@"resultCount"];
if (number.intValue == 1) {
NSString *newVersion = responseObject[@"results"][0][@"version"];
if (![newVersion isEqualToString:oldVersion]) {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"有新版本可供更新" message:nil preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"更新" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://itunes.apple.com/cn/app/xing-tu/id1126715226?mt=8"]];
}];
UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
[alertController addAction:cancleAction];
[alertController addAction:okAction];
[self presentViewController:alertController animated:YES completion:nil];
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"%@", error);
}];
}
上面的URLString是你的應(yīng)用在app store的鏈接:打開iTunes,搜索到你的應(yīng)用后右單擊應(yīng)用的logo,如下圖,選擇拷貝鏈接.上面的appID就是鏈接中id后面的數(shù)字.