OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #import <Foundation/Foundation.h> | |
6 #include "NetworkCommunication.h" | |
7 | |
8 @implementation NetworkCommunication : NSObject | |
9 | |
10 // The following variables each have getters and setters, so when this class | |
11 // is in use, please don't forget to set the response handler! | |
12 @synthesize session; | |
Mark Mentovai
2016/07/07 17:53:48
Please name the variables that back your propertie
| |
13 @synthesize request; | |
14 @synthesize dataResponseHandler; | |
15 @synthesize downloadResponseHandler; | |
16 | |
17 - (id)init { | |
18 return [self initWithDelegate:nil]; | |
19 } | |
20 | |
21 - (id)initWithDelegate:(id) delegate { | |
22 if ((self = [super init])) { | |
23 NSURLSessionConfiguration* sessionConfig = [NSURLSessionConfiguration | |
Mark Mentovai
2016/07/07 17:53:48
Both sessionConfig and session are autoreleased, s
| |
24 defaultSessionConfiguration]; | |
25 session = [NSURLSession sessionWithConfiguration:sessionConfig | |
26 delegate:delegate | |
27 delegateQueue:nil]; | |
28 } | |
29 return self; | |
30 } | |
31 | |
32 - (NSMutableURLRequest*)createRequestWithURLasString:(NSString*) urlString | |
33 andXMLBody:(NSXMLDocument*) body { | |
34 if (request) { | |
Mark Mentovai
2016/07/07 17:53:48
When would this ever happen?
| |
35 [request autorelease]; | |
36 } | |
37 NSURL* requestURL = [NSURL URLWithString:urlString]; | |
38 request = [NSMutableURLRequest requestWithURL:requestURL]; | |
39 if (body) { | |
40 [request addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; | |
41 NSData* requestBody = [[body XMLString] | |
42 dataUsingEncoding:NSUTF8StringEncoding]; | |
43 request.HTTPBody = requestBody; | |
44 } | |
45 return request; | |
46 } | |
47 | |
48 - (void)sendDataRequestWithCompletionHandler: | |
49 (DataTaskCompletionHandler) completionHandler { | |
50 dataResponseHandler = completionHandler; | |
51 NSURLSessionDataTask* dataTask = [session dataTaskWithRequest:request | |
52 completionHandler:dataResponseHandler]; | |
53 | |
54 [dataTask resume]; | |
55 return; | |
56 } | |
57 | |
58 - (void)sendDownloadRequest { | |
59 NSURLSessionDownloadTask* downloadTask; | |
60 if (downloadResponseHandler) { | |
61 downloadTask = | |
62 [session downloadTaskWithRequest:request | |
63 completionHandler:downloadResponseHandler]; | |
64 } else { | |
65 downloadTask = | |
66 [session downloadTaskWithRequest:request]; | |
67 } | |
68 [downloadTask resume]; | |
69 | |
70 return; | |
71 } | |
72 | |
73 @end | |
OLD | NEW |