一.判断是不是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];
/////////////////////////////////////////////////////////////////版本更新结速
二.把更新的代码重构到一个方法里
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];
//申明返回的结果是json类型
manager.responseSerializer = [AFJSONResponseSerializer serializer];
//申明请求的数据是json类型
manager.requestSerializer = [AFJSONRequestSerializer serializer];
//如果报接受类型不一致请替换一致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是你的应用在app store的链接:打开iTunes,搜索到你的应用后右单击应用的logo,如下图,选择拷贝链接.上面的appID就是链接中id后面的数字.
这里写图片描述
网友评论