UIWebView+AFNetworking.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // UIWebView+AFNetworking.m
  2. // Copyright (c) 2011–2015 Alamofire Software Foundation (http://alamofire.org/)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. #import "UIWebView+AFNetworking.h"
  22. #import <objc/runtime.h>
  23. #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
  24. #import "AFHTTPRequestOperation.h"
  25. #import "AFURLResponseSerialization.h"
  26. #import "AFURLRequestSerialization.h"
  27. @interface UIWebView (_AFNetworking)
  28. @property (readwrite, nonatomic, strong, setter = af_setHTTPRequestOperation:) AFHTTPRequestOperation *af_HTTPRequestOperation;
  29. @end
  30. @implementation UIWebView (_AFNetworking)
  31. - (AFHTTPRequestOperation *)af_HTTPRequestOperation {
  32. return (AFHTTPRequestOperation *)objc_getAssociatedObject(self, @selector(af_HTTPRequestOperation));
  33. }
  34. - (void)af_setHTTPRequestOperation:(AFHTTPRequestOperation *)operation {
  35. objc_setAssociatedObject(self, @selector(af_HTTPRequestOperation), operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  36. }
  37. @end
  38. #pragma mark -
  39. @implementation UIWebView (AFNetworking)
  40. - (AFHTTPRequestSerializer <AFURLRequestSerialization> *)requestSerializer {
  41. static AFHTTPRequestSerializer <AFURLRequestSerialization> *_af_defaultRequestSerializer = nil;
  42. static dispatch_once_t onceToken;
  43. dispatch_once(&onceToken, ^{
  44. _af_defaultRequestSerializer = [AFHTTPRequestSerializer serializer];
  45. });
  46. #pragma clang diagnostic push
  47. #pragma clang diagnostic ignored "-Wgnu"
  48. return objc_getAssociatedObject(self, @selector(requestSerializer)) ?: _af_defaultRequestSerializer;
  49. #pragma clang diagnostic pop
  50. }
  51. - (void)setRequestSerializer:(AFHTTPRequestSerializer<AFURLRequestSerialization> *)requestSerializer {
  52. objc_setAssociatedObject(self, @selector(requestSerializer), requestSerializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  53. }
  54. - (AFHTTPResponseSerializer <AFURLResponseSerialization> *)responseSerializer {
  55. static AFHTTPResponseSerializer <AFURLResponseSerialization> *_af_defaultResponseSerializer = nil;
  56. static dispatch_once_t onceToken;
  57. dispatch_once(&onceToken, ^{
  58. _af_defaultResponseSerializer = [AFHTTPResponseSerializer serializer];
  59. });
  60. #pragma clang diagnostic push
  61. #pragma clang diagnostic ignored "-Wgnu"
  62. return objc_getAssociatedObject(self, @selector(responseSerializer)) ?: _af_defaultResponseSerializer;
  63. #pragma clang diagnostic pop
  64. }
  65. - (void)setResponseSerializer:(AFHTTPResponseSerializer<AFURLResponseSerialization> *)responseSerializer {
  66. objc_setAssociatedObject(self, @selector(responseSerializer), responseSerializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  67. }
  68. #pragma mark -
  69. - (void)loadRequest:(NSURLRequest *)request
  70. progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
  71. success:(NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success
  72. failure:(void (^)(NSError *error))failure
  73. {
  74. [self loadRequest:request MIMEType:nil textEncodingName:nil progress:progress success:^NSData *(NSHTTPURLResponse *response, NSData *data) {
  75. NSStringEncoding stringEncoding = NSUTF8StringEncoding;
  76. if (response.textEncodingName) {
  77. CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
  78. if (encoding != kCFStringEncodingInvalidId) {
  79. stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding);
  80. }
  81. }
  82. NSString *string = [[NSString alloc] initWithData:data encoding:stringEncoding];
  83. if (success) {
  84. string = success(response, string);
  85. }
  86. return [string dataUsingEncoding:stringEncoding];
  87. } failure:failure];
  88. }
  89. - (void)loadRequest:(NSURLRequest *)request
  90. MIMEType:(NSString *)MIMEType
  91. textEncodingName:(NSString *)textEncodingName
  92. progress:(void (^)(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite))progress
  93. success:(NSData * (^)(NSHTTPURLResponse *response, NSData *data))success
  94. failure:(void (^)(NSError *error))failure
  95. {
  96. NSParameterAssert(request);
  97. if (self.af_HTTPRequestOperation) {
  98. [self.af_HTTPRequestOperation cancel];
  99. }
  100. request = [self.requestSerializer requestBySerializingRequest:request withParameters:nil error:nil];
  101. self.af_HTTPRequestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  102. self.af_HTTPRequestOperation.responseSerializer = self.responseSerializer;
  103. __weak __typeof(self)weakSelf = self;
  104. [self.af_HTTPRequestOperation setDownloadProgressBlock:progress];
  105. [self.af_HTTPRequestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id __unused responseObject) {
  106. NSData *data = success ? success(operation.response, operation.responseData) : operation.responseData;
  107. #pragma clang diagnostic push
  108. #pragma clang diagnostic ignored "-Wgnu"
  109. __strong __typeof(weakSelf) strongSelf = weakSelf;
  110. [strongSelf loadData:data MIMEType:(MIMEType ?: [operation.response MIMEType]) textEncodingName:(textEncodingName ?: [operation.response textEncodingName]) baseURL:[operation.response URL]];
  111. if ([strongSelf.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
  112. [strongSelf.delegate webViewDidFinishLoad:strongSelf];
  113. }
  114. #pragma clang diagnostic pop
  115. } failure:^(AFHTTPRequestOperation * __unused operation, NSError *error) {
  116. if (failure) {
  117. failure(error);
  118. }
  119. }];
  120. [self.af_HTTPRequestOperation start];
  121. if ([self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
  122. [self.delegate webViewDidStartLoad:self];
  123. }
  124. }
  125. @end
  126. #endif