Life Cycle of a URL Session with System-Provided Delegates
123456789101112131415161718192021222324252627
- (void)startWithURLString:(NSString *)urlString {
/**
* using stringByAddingPercentEscapesUsingEncoding to support Chinese characters in url string
*/
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionConfiguration* configuration = [NSURLSessionConfiguration ephemeralSessionConfiguration];
NSURLSession* session = [NSURLSession sessionWithConfiguration:configuration];
NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (!error) {
if ([request.URL isEqual:url]) {
/**
* currently we are not in the main thread
* call method in main thread to update the UI
*/
dispatch_async(dispatch_get_main_queue(), ^{
});
}
}
}];
[dataTask resume];
}
App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure.
Temporary exceptions can be configured via your app's Info.plist file.
打开Info.plist,加入如下字段:
123456
<key>NSAppTransportSecurity</key>
<dict>
<!--Include to allow all connections (DANGER)-->
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>