No Description

UIWebView+AFNetworking.m 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // UIWebView+AFNetworking.m
  2. // Copyright (c) 2011–2016 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 TARGET_OS_IOS
  24. #import "AFHTTPSessionManager.h"
  25. #import "AFURLResponseSerialization.h"
  26. #import "AFURLRequestSerialization.h"
  27. @interface UIWebView (_AFNetworking)
  28. @property (readwrite, nonatomic, strong, setter = af_setURLSessionTask:) NSURLSessionDataTask *af_URLSessionTask;
  29. @end
  30. @implementation UIWebView (_AFNetworking)
  31. - (NSURLSessionDataTask *)af_URLSessionTask {
  32. return (NSURLSessionDataTask *)objc_getAssociatedObject(self, @selector(af_URLSessionTask));
  33. }
  34. - (void)af_setURLSessionTask:(NSURLSessionDataTask *)af_URLSessionTask {
  35. objc_setAssociatedObject(self, @selector(af_URLSessionTask), af_URLSessionTask, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  36. }
  37. @end
  38. #pragma mark -
  39. @implementation UIWebView (AFNetworking)
  40. - (AFHTTPSessionManager *)sessionManager {
  41. static AFHTTPSessionManager *_af_defaultHTTPSessionManager = nil;
  42. static dispatch_once_t onceToken;
  43. dispatch_once(&onceToken, ^{
  44. _af_defaultHTTPSessionManager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
  45. _af_defaultHTTPSessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
  46. _af_defaultHTTPSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  47. });
  48. #pragma clang diagnostic push
  49. #pragma clang diagnostic ignored "-Wgnu"
  50. return objc_getAssociatedObject(self, @selector(sessionManager)) ?: _af_defaultHTTPSessionManager;
  51. #pragma clang diagnostic pop
  52. }
  53. - (void)setSessionManager:(AFHTTPSessionManager *)sessionManager {
  54. objc_setAssociatedObject(self, @selector(sessionManager), sessionManager, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  55. }
  56. - (AFHTTPResponseSerializer <AFURLResponseSerialization> *)responseSerializer {
  57. static AFHTTPResponseSerializer <AFURLResponseSerialization> *_af_defaultResponseSerializer = nil;
  58. static dispatch_once_t onceToken;
  59. dispatch_once(&onceToken, ^{
  60. _af_defaultResponseSerializer = [AFHTTPResponseSerializer serializer];
  61. });
  62. #pragma clang diagnostic push
  63. #pragma clang diagnostic ignored "-Wgnu"
  64. return objc_getAssociatedObject(self, @selector(responseSerializer)) ?: _af_defaultResponseSerializer;
  65. #pragma clang diagnostic pop
  66. }
  67. - (void)setResponseSerializer:(AFHTTPResponseSerializer<AFURLResponseSerialization> *)responseSerializer {
  68. objc_setAssociatedObject(self, @selector(responseSerializer), responseSerializer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
  69. }
  70. #pragma mark -
  71. - (void)loadRequest:(NSURLRequest *)request
  72. progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
  73. success:(NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success
  74. failure:(void (^)(NSError *error))failure
  75. {
  76. [self loadRequest:request MIMEType:nil textEncodingName:nil progress:progress success:^NSData *(NSHTTPURLResponse *response, NSData *data) {
  77. NSStringEncoding stringEncoding = NSUTF8StringEncoding;
  78. if (response.textEncodingName) {
  79. CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
  80. if (encoding != kCFStringEncodingInvalidId) {
  81. stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding);
  82. }
  83. }
  84. NSString *string = [[NSString alloc] initWithData:data encoding:stringEncoding];
  85. if (success) {
  86. string = success(response, string);
  87. }
  88. return [string dataUsingEncoding:stringEncoding];
  89. } failure:failure];
  90. }
  91. - (void)loadRequest:(NSURLRequest *)request
  92. MIMEType:(NSString *)MIMEType
  93. textEncodingName:(NSString *)textEncodingName
  94. progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
  95. success:(NSData * (^)(NSHTTPURLResponse *response, NSData *data))success
  96. failure:(void (^)(NSError *error))failure
  97. {
  98. NSParameterAssert(request);
  99. if (self.af_URLSessionTask.state == NSURLSessionTaskStateRunning || self.af_URLSessionTask.state == NSURLSessionTaskStateSuspended) {
  100. [self.af_URLSessionTask cancel];
  101. }
  102. self.af_URLSessionTask = nil;
  103. __weak __typeof(self)weakSelf = self;
  104. NSURLSessionDataTask *dataTask;
  105. dataTask = [self.sessionManager
  106. GET:request.URL.absoluteString
  107. parameters:nil
  108. progress:nil
  109. success:^(NSURLSessionDataTask * _Nonnull task, id _Nonnull responseObject) {
  110. __strong __typeof(weakSelf) strongSelf = weakSelf;
  111. if (success) {
  112. success((NSHTTPURLResponse *)task.response, responseObject);
  113. }
  114. [strongSelf loadData:responseObject MIMEType:MIMEType textEncodingName:textEncodingName baseURL:[task.currentRequest URL]];
  115. if ([strongSelf.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
  116. [strongSelf.delegate webViewDidFinishLoad:strongSelf];
  117. }
  118. }
  119. failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
  120. if (failure) {
  121. failure(error);
  122. }
  123. }];
  124. self.af_URLSessionTask = dataTask;
  125. if (progress != nil) {
  126. *progress = [self.sessionManager downloadProgressForTask:dataTask];
  127. }
  128. [self.af_URLSessionTask resume];
  129. if ([self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
  130. [self.delegate webViewDidStartLoad:self];
  131. }
  132. }
  133. @end
  134. #endif