Ios进行网络请求

ios网络请求设计的一些类,可以参考;IOS网络编程:HTTP

下面主要说说NSURLSession。

NSURLSession

参考官方文档:Life Cycle of a URL Session。根据官方文档的Sample code如下:

  • Life Cycle of a URL Session with System-Provided Delegates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

- (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];

}
  • Life Cycle of a URL Session with Custom Delegates
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

- (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 delegate:self delegateQueue:[NSOperationQueue mainQueue]];
    
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request];
    
    self.responseData = [[NSMutableData alloc] init];
    
    [dataTask resume];
}

以及NSURLSessionDataDelegate中的:

1
2
3
4
5
6
7
8
9
10
11
12
13

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
    // self.responseData is NSMutableData type
    [self.responseData appendData:data];
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
    if (!error) {

    } else {

    }
}

NSURLSessionDataTask不能携带completionHandler,否则上面的delegate不会调到。

App Transport Security

如果遇到如下错误信息:

1
2
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,加入如下字段:

1
2
3
4
5
6
<key>NSAppTransportSecurity</key>
    <dict>
        <!--Include to allow all connections (DANGER)-->
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>