├── LICENSE ├── README.md └── WebKit+AFNetworking ├── WebFrame+AFNetworking.h └── WebFrame+AFNetworking.m /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 AFNetworking 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | WebKit-AFNetworking 2 | =================== 3 | 4 | AFNetworking Extensions for WebKit 5 | -------------------------------------------------------------------------------- /WebKit+AFNetworking/WebFrame+AFNetworking.h: -------------------------------------------------------------------------------- 1 | // WebFrame+AFNetworking.h 2 | // 3 | // Copyright (c) 2013-2014 AFNetworking (http://afnetworking.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import 24 | 25 | #import 26 | 27 | #if defined(__MAC_OS_X_VERSION_MIN_REQUIRED) 28 | 29 | #import 30 | 31 | @class AFHTTPRequestSerializer, AFHTTPResponseSerializer; 32 | @protocol AFURLRequestSerialization, AFURLResponseSerialization; 33 | 34 | /** 35 | This category adds methods to the WebKit framework's `WebFrame` class. The methods in this category provide increased control over the request cycle, including progress monitoring and success / failure handling. 36 | 37 | @discussion When using these category methods, make sure to assign `delegate` for the web view, which implements `–webView:shouldStartLoadWithRequest:navigationType:` appropriately. This allows for tapped links to be loaded through AFNetworking, and can ensure that `canGoBack` & `canGoForward` update their values correctly. 38 | */ 39 | @interface WebFrame (AFNetworking) 40 | 41 | /** 42 | The request serializer used to serialize requests made with the `-loadRequest:...` category methods. By default, this is an instance of `AFHTTPRequestSerializer`. 43 | */ 44 | @property (nonatomic, strong) AFHTTPRequestSerializer * requestSerializer; 45 | 46 | /** 47 | The response serializer used to serialize responses made with the `-loadRequest:...` category methods. By default, this is an instance of `AFHTTPResponseSerializer`. 48 | */ 49 | @property (nonatomic, strong) AFHTTPResponseSerializer * responseSerializer; 50 | 51 | /** 52 | Asynchronously loads the specified request. 53 | 54 | @param request A URL request identifying the location of the content to load. This must not be `nil`. 55 | @param progress A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes three arguments: the number of bytes read since the last time the download progress block was called, the total bytes read, and the total bytes expected to be read during the request, as initially determined by the expected content size of the `NSHTTPURLResponse` object. This block may be called multiple times, and will execute on the main thread. 56 | @param success A block object to be executed when the request finishes loading successfully. This block returns the HTML string to be loaded by the web view, and takes two arguments: the response, and the response string. 57 | @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error that occurred. 58 | */ 59 | - (void)loadRequest:(NSURLRequest *)request 60 | progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress 61 | success:(NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success 62 | failure:(void (^)(NSError *error))failure; 63 | 64 | /** 65 | Asynchronously loads the data associated with a particular request with a specified MIME type and text encoding. 66 | 67 | @param request A URL request identifying the location of the content to load. This must not be `nil`. 68 | @param MIMEType The MIME type of the content. Defaults to the content type of the response if not specified. 69 | @param textEncodingName The IANA encoding name, as in `utf-8` or `utf-16`. Defaults to the response text encoding if not specified. 70 | @param progress A block object to be called when an undetermined number of bytes have been downloaded from the server. This block has no return value and takes three arguments: the number of bytes read since the last time the download progress block was called, the total bytes read, and the total bytes expected to be read during the request, as initially determined by the expected content size of the `NSHTTPURLResponse` object. This block may be called multiple times, and will execute on the main thread. 71 | @param success A block object to be executed when the request finishes loading successfully. This block returns the data to be loaded by the web view and takes two arguments: the response, and the downloaded data. 72 | @param failure A block object to be executed when the request operation finishes unsuccessfully, or that finishes successfully, but encountered an error while parsing the response data. This block has no return value and takes a single argument: the error that occurred. 73 | */ 74 | - (void)loadRequest:(NSURLRequest *)request 75 | MIMEType:(NSString *)MIMEType 76 | textEncodingName:(NSString *)textEncodingName 77 | progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress 78 | success:(NSData * (^)(NSHTTPURLResponse *response, NSData *data))success 79 | failure:(void (^)(NSError *error))failure; 80 | 81 | @end 82 | 83 | #endif 84 | -------------------------------------------------------------------------------- /WebKit+AFNetworking/WebFrame+AFNetworking.m: -------------------------------------------------------------------------------- 1 | // WebFrame+AFNetworking.m 2 | // 3 | // Copyright (c) 2013-2014 AFNetworking (http://afnetworking.com) 4 | // 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy 6 | // of this software and associated documentation files (the "Software"), to deal 7 | // in the Software without restriction, including without limitation the rights 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | // copies of the Software, and to permit persons to whom the Software is 10 | // furnished to do so, subject to the following conditions: 11 | // 12 | // The above copyright notice and this permission notice shall be included in 13 | // all copies or substantial portions of the Software. 14 | // 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | // THE SOFTWARE. 22 | 23 | #import "WebFrame+AFNetworking.h" 24 | #import 25 | 26 | #import "AFHTTPRequestOperation.h" 27 | #import "AFURLResponseSerialization.h" 28 | #import "AFURLRequestSerialization.h" 29 | 30 | @interface WebFrame (_AFNetworking) 31 | @property (readwrite, nonatomic, strong, setter = af_setHTTPRequestOperation:) AFHTTPRequestOperation *af_HTTPRequestOperation; 32 | @end 33 | 34 | @implementation WebFrame (_AFNetworking) 35 | 36 | - (AFHTTPRequestOperation *)af_HTTPRequestOperation { 37 | return (AFHTTPRequestOperation *)objc_getAssociatedObject(self, @selector(af_HTTPRequestOperation)); 38 | } 39 | 40 | - (void)af_setHTTPRequestOperation:(AFHTTPRequestOperation *)operation { 41 | objc_setAssociatedObject(self, @selector(af_HTTPRequestOperation), operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 42 | } 43 | 44 | @end 45 | 46 | #pragma mark - 47 | 48 | @implementation WebFrame (AFNetworking) 49 | 50 | - (AFHTTPRequestSerializer *)requestSerializer { 51 | static AFHTTPRequestSerializer *_af_defaultRequestSerializer = nil; 52 | static dispatch_once_t onceToken; 53 | dispatch_once(&onceToken, ^{ 54 | _af_defaultRequestSerializer = [AFHTTPRequestSerializer serializer]; 55 | }); 56 | 57 | #pragma clang diagnostic push 58 | #pragma clang diagnostic ignored "-Wgnu" 59 | return objc_getAssociatedObject(self, @selector(requestSerializer)) ?: _af_defaultRequestSerializer; 60 | #pragma clang diagnostic pop 61 | } 62 | 63 | - (void)setRequestSerializer:(AFHTTPRequestSerializer *)requestSerializer { 64 | objc_setAssociatedObject(self, @selector(requestSerializer), requestSerializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 65 | } 66 | 67 | - (AFHTTPResponseSerializer *)responseSerializer { 68 | static AFHTTPResponseSerializer *_af_defaultResponseSerializer = nil; 69 | static dispatch_once_t onceToken; 70 | dispatch_once(&onceToken, ^{ 71 | _af_defaultResponseSerializer = [AFHTTPResponseSerializer serializer]; 72 | }); 73 | 74 | #pragma clang diagnostic push 75 | #pragma clang diagnostic ignored "-Wgnu" 76 | return objc_getAssociatedObject(self, @selector(responseSerializer)) ?: _af_defaultResponseSerializer; 77 | #pragma clang diagnostic pop 78 | } 79 | 80 | - (void)setResponseSerializer:(AFHTTPResponseSerializer *)responseSerializer { 81 | objc_setAssociatedObject(self, @selector(responseSerializer), responseSerializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 82 | } 83 | 84 | #pragma mark - 85 | 86 | - (void)loadRequest:(NSURLRequest *)request 87 | progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress 88 | success:(NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success 89 | failure:(void (^)(NSError *error))failure 90 | { 91 | [self loadRequest:request MIMEType:nil textEncodingName:nil progress:progress success:^NSData *(NSHTTPURLResponse *response, NSData *data) { 92 | NSStringEncoding stringEncoding = NSUTF8StringEncoding; 93 | if (response.textEncodingName) { 94 | CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName); 95 | if (encoding != kCFStringEncodingInvalidId) { 96 | stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding); 97 | } 98 | } 99 | 100 | NSString *string = [[NSString alloc] initWithData:data encoding:stringEncoding]; 101 | if (success) { 102 | string = success(response, string); 103 | } 104 | 105 | return [string dataUsingEncoding:stringEncoding]; 106 | } failure:failure]; 107 | } 108 | 109 | - (void)loadRequest:(NSURLRequest *)request 110 | MIMEType:(NSString *)MIMEType 111 | textEncodingName:(NSString *)textEncodingName 112 | progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress 113 | success:(NSData * (^)(NSHTTPURLResponse *response, NSData *data))success 114 | failure:(void (^)(NSError *error))failure 115 | { 116 | NSParameterAssert(request); 117 | 118 | if (self.af_HTTPRequestOperation) { 119 | [self.af_HTTPRequestOperation cancel]; 120 | } 121 | 122 | request = [self.requestSerializer requestBySerializingRequest:request withParameters:nil error:nil]; 123 | 124 | self.af_HTTPRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; 125 | self.af_HTTPRequestOperation.responseSerializer = self.responseSerializer; 126 | 127 | __weak __typeof(self)weakSelf = self; 128 | [self.af_HTTPRequestOperation setDownloadProgressBlock:progress]; 129 | [self.af_HTTPRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id __unused responseObject) { 130 | NSData *data = success ? success(operation.response, operation.responseData) : operation.responseData; 131 | 132 | #pragma clang diagnostic push 133 | #pragma clang diagnostic ignored "-Wgnu" 134 | __strong __typeof(weakSelf) strongSelf = weakSelf; 135 | [strongSelf loadData:data MIMEType:(MIMEType ?: [operation.response MIMEType]) textEncodingName:(textEncodingName ?: [operation.response textEncodingName]) baseURL:[operation.response URL]]; 136 | #pragma clang diagnostic pop 137 | } failure:^(AFHTTPRequestOperation * __unused operation, NSError *error) { 138 | if (failure) { 139 | failure(error); 140 | } 141 | }]; 142 | 143 | [self.af_HTTPRequestOperation start]; 144 | } 145 | 146 | @end 147 | --------------------------------------------------------------------------------