| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * libjingle | |
| 3 * Copyright 2014 Google Inc. | |
| 4 * | |
| 5 * Redistribution and use in source and binary forms, with or without | |
| 6 * modification, are permitted provided that the following conditions are met: | |
| 7 * | |
| 8 * 1. Redistributions of source code must retain the above copyright notice, | |
| 9 * this list of conditions and the following disclaimer. | |
| 10 * 2. Redistributions in binary form must reproduce the above copyright notice, | |
| 11 * this list of conditions and the following disclaimer in the documentation | |
| 12 * and/or other materials provided with the distribution. | |
| 13 * 3. The name of the author may not be used to endorse or promote products | |
| 14 * derived from this software without specific prior written permission. | |
| 15 * | |
| 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED | |
| 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF | |
| 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
| 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
| 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
| 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
| 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
| 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 26 */ | |
| 27 | |
| 28 #import "ARDUtilities.h" | |
| 29 | |
| 30 @implementation NSDictionary (ARDUtilites) | |
| 31 | |
| 32 + (NSDictionary *)dictionaryWithJSONString:(NSString *)jsonString { | |
| 33 NSParameterAssert(jsonString.length > 0); | |
| 34 NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding]; | |
| 35 NSError *error = nil; | |
| 36 NSDictionary *dict = | |
| 37 [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; | |
| 38 if (error) { | |
| 39 NSLog(@"Error parsing JSON: %@", error.localizedDescription); | |
| 40 } | |
| 41 return dict; | |
| 42 } | |
| 43 | |
| 44 + (NSDictionary *)dictionaryWithJSONData:(NSData *)jsonData { | |
| 45 NSError *error = nil; | |
| 46 NSDictionary *dict = | |
| 47 [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error]; | |
| 48 if (error) { | |
| 49 NSLog(@"Error parsing JSON: %@", error.localizedDescription); | |
| 50 } | |
| 51 return dict; | |
| 52 } | |
| 53 | |
| 54 @end | |
| 55 | |
| 56 @implementation NSURLConnection (ARDUtilities) | |
| 57 | |
| 58 + (void)sendAsyncRequest:(NSURLRequest *)request | |
| 59 completionHandler:(void (^)(NSURLResponse *response, | |
| 60 NSData *data, | |
| 61 NSError *error))completionHandler { | |
| 62 // Kick off an async request which will call back on main thread. | |
| 63 [NSURLConnection sendAsynchronousRequest:request | |
| 64 queue:[NSOperationQueue mainQueue] | |
| 65 completionHandler:^(NSURLResponse *response, | |
| 66 NSData *data, | |
| 67 NSError *error) { | |
| 68 if (completionHandler) { | |
| 69 completionHandler(response, data, error); | |
| 70 } | |
| 71 }]; | |
| 72 } | |
| 73 | |
| 74 // Posts data to the specified URL. | |
| 75 + (void)sendAsyncPostToURL:(NSURL *)url | |
| 76 withData:(NSData *)data | |
| 77 completionHandler:(void (^)(BOOL succeeded, | |
| 78 NSData *data))completionHandler { | |
| 79 NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
| 80 request.HTTPMethod = @"POST"; | |
| 81 request.HTTPBody = data; | |
| 82 [[self class] sendAsyncRequest:request | |
| 83 completionHandler:^(NSURLResponse *response, | |
| 84 NSData *data, | |
| 85 NSError *error) { | |
| 86 if (error) { | |
| 87 NSLog(@"Error posting data: %@", error.localizedDescription); | |
| 88 if (completionHandler) { | |
| 89 completionHandler(NO, data); | |
| 90 } | |
| 91 return; | |
| 92 } | |
| 93 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; | |
| 94 if (httpResponse.statusCode != 200) { | |
| 95 NSString *serverResponse = data.length > 0 ? | |
| 96 [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] : | |
| 97 nil; | |
| 98 NSLog(@"Received bad response: %@", serverResponse); | |
| 99 if (completionHandler) { | |
| 100 completionHandler(NO, data); | |
| 101 } | |
| 102 return; | |
| 103 } | |
| 104 if (completionHandler) { | |
| 105 completionHandler(YES, data); | |
| 106 } | |
| 107 }]; | |
| 108 } | |
| 109 | |
| 110 @end | |
| OLD | NEW |