- (void)getNetTime {
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
// do something with the data
}];
[dataTask resume];
}
运行的时候虽然不会崩溃,但是会报如下错误:This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes. This will cause an exception in a future release.
解决方法:
- (void)getNetTime {
NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (error == nil) {// 将要处理的内容放在GCDblock中就可以了
dispatch_async(dispatch_get_main_queue(), ^{
// do something with the data
});
} else {
NSLog(@"hhhhda");
}
}];
[dataTask resume];
}