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 |
| 7 #import "NetworkCommunication.h" |
| 8 |
| 9 @implementation NetworkCommunication : NSObject |
| 10 |
| 11 @synthesize session = session_; |
| 12 @synthesize request = request_; |
| 13 @synthesize dataResponseHandler = dataResponseHandler_; |
| 14 @synthesize downloadResponseHandler = downloadResponseHandler_; |
| 15 |
| 16 - (id)init { |
| 17 return [self initWithDelegate:nil]; |
| 18 } |
| 19 |
| 20 - (id)initWithDelegate:(id)delegate { |
| 21 if ((self = [super init])) { |
| 22 NSURLSessionConfiguration* sessionConfig = |
| 23 [NSURLSessionConfiguration defaultSessionConfiguration]; |
| 24 session_ = [NSURLSession sessionWithConfiguration:sessionConfig |
| 25 delegate:delegate |
| 26 delegateQueue:nil]; |
| 27 } |
| 28 return self; |
| 29 } |
| 30 |
| 31 - (NSMutableURLRequest*)createRequestWithUrlAsString:(NSString*)urlString |
| 32 andXMLBody:(NSXMLDocument*)body { |
| 33 NSURL* requestURL = [NSURL URLWithString:urlString]; |
| 34 request_ = [NSMutableURLRequest requestWithURL:requestURL]; |
| 35 if (body) { |
| 36 [request_ addValue:@"text/xml" forHTTPHeaderField:@"Content-Type"]; |
| 37 NSData* requestBody = |
| 38 [[body XMLString] dataUsingEncoding:NSUTF8StringEncoding]; |
| 39 request_.HTTPBody = requestBody; |
| 40 } |
| 41 return request_; |
| 42 } |
| 43 |
| 44 - (void)sendDataRequestWithCompletionHandler: |
| 45 (DataTaskCompletionHandler)completionHandler { |
| 46 dataResponseHandler_ = completionHandler; |
| 47 NSURLSessionDataTask* dataTask = |
| 48 [session_ dataTaskWithRequest:request_ |
| 49 completionHandler:dataResponseHandler_]; |
| 50 |
| 51 [dataTask resume]; |
| 52 } |
| 53 |
| 54 - (void)sendDownloadRequest { |
| 55 NSURLSessionDownloadTask* downloadTask; |
| 56 if (downloadResponseHandler_) { |
| 57 downloadTask = [session_ downloadTaskWithRequest:request_ |
| 58 completionHandler:downloadResponseHandler_]; |
| 59 } else { |
| 60 downloadTask = [session_ downloadTaskWithRequest:request_]; |
| 61 } |
| 62 [downloadTask resume]; |
| 63 } |
| 64 |
| 65 @end |
OLD | NEW |