- Upgrade libraries
This commit is contained in:
1 parent
ee53b3f736
commit
100e2c98f4
47 files changed
+874
-895
No files matched your search
@@ -28,7 +28,7 @@
|
||||
@interface AFCachedImage : NSObject
|
||||
|
||||
@property (nonatomic, strong) UIImage *image;
|
||||
@property (nonatomic, strong) NSString *identifier;
|
||||
@property (nonatomic, copy) NSString *identifier;
|
||||
@property (nonatomic, assign) UInt64 totalBytes;
|
||||
@property (nonatomic, strong) NSDate *lastAccessDate;
|
||||
@property (nonatomic, assign) UInt64 currentMemoryUsage;
|
||||
@@ -37,7 +37,7 @@
|
||||
|
||||
@implementation AFCachedImage
|
||||
|
||||
-(instancetype)initWithImage:(UIImage *)image identifier:(NSString *)identifier {
|
||||
- (instancetype)initWithImage:(UIImage *)image identifier:(NSString *)identifier {
|
||||
if (self = [self init]) {
|
||||
self.image = image;
|
||||
self.identifier = identifier;
|
||||
@@ -51,7 +51,7 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (UIImage*)accessImage {
|
||||
- (UIImage *)accessImage {
|
||||
self.lastAccessDate = [NSDate date];
|
||||
return self.image;
|
||||
}
|
||||
@@ -134,7 +134,7 @@
|
||||
[self.cachedImages removeObjectForKey:cachedImage.identifier];
|
||||
bytesPurged += cachedImage.totalBytes;
|
||||
if (bytesPurged >= bytesToPurge) {
|
||||
break ;
|
||||
break;
|
||||
}
|
||||
}
|
||||
self.currentMemoryUsage -= bytesPurged;
|
||||
|
||||
@@ -67,7 +67,7 @@ typedef NS_ENUM(NSInteger, AFImageDownloadPrioritization) {
|
||||
/**
|
||||
Defines the order prioritization of incoming download requests being inserted into the queue. `AFImageDownloadPrioritizationFIFO` by default.
|
||||
*/
|
||||
@property (nonatomic, assign) AFImageDownloadPrioritization downloadPrioritizaton;
|
||||
@property (nonatomic, assign) AFImageDownloadPrioritization downloadPrioritization;
|
||||
|
||||
/**
|
||||
The shared default instance of `AFImageDownloader` initialized with default values.
|
||||
|
||||
+36
-24
@@ -28,8 +28,8 @@
|
||||
|
||||
@interface AFImageDownloaderResponseHandler : NSObject
|
||||
@property (nonatomic, strong) NSUUID *uuid;
|
||||
@property (nonatomic, copy) void (^successBlock)(NSURLRequest*, NSHTTPURLResponse*, UIImage*);
|
||||
@property (nonatomic, copy) void (^failureBlock)(NSURLRequest*, NSHTTPURLResponse*, NSError*);
|
||||
@property (nonatomic, copy) void (^successBlock)(NSURLRequest *, NSHTTPURLResponse *, UIImage *);
|
||||
@property (nonatomic, copy) void (^failureBlock)(NSURLRequest *, NSHTTPURLResponse *, NSError *);
|
||||
@end
|
||||
|
||||
@implementation AFImageDownloaderResponseHandler
|
||||
@@ -71,11 +71,11 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)addResponseHandler:(AFImageDownloaderResponseHandler*)handler {
|
||||
- (void)addResponseHandler:(AFImageDownloaderResponseHandler *)handler {
|
||||
[self.responseHandlers addObject:handler];
|
||||
}
|
||||
|
||||
- (void)removeResponseHandler:(AFImageDownloaderResponseHandler*)handler {
|
||||
- (void)removeResponseHandler:(AFImageDownloaderResponseHandler *)handler {
|
||||
[self.responseHandlers removeObject:handler];
|
||||
}
|
||||
|
||||
@@ -109,20 +109,24 @@
|
||||
@implementation AFImageDownloader
|
||||
|
||||
+ (NSURLCache *)defaultURLCache {
|
||||
NSUInteger memoryCapacity = 20 * 1024 * 1024; // 20MB
|
||||
NSUInteger diskCapacity = 150 * 1024 * 1024; // 150MB
|
||||
NSURL *cacheURL = [[[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory
|
||||
inDomain:NSUserDomainMask
|
||||
appropriateForURL:nil
|
||||
create:YES
|
||||
error:nil]
|
||||
URLByAppendingPathComponent:@"com.alamofire.imagedownloader"];
|
||||
|
||||
// It's been discovered that a crash will occur on certain versions
|
||||
// of iOS if you customize the cache.
|
||||
//
|
||||
// More info can be found here: https://devforums.apple.com/message/1102182#1102182
|
||||
//
|
||||
// When iOS 7 support is dropped, this should be modified to use
|
||||
// NSProcessInfo methods instead.
|
||||
if ([[[UIDevice currentDevice] systemVersion] compare:@"8.2" options:NSNumericSearch] == NSOrderedAscending) {
|
||||
return [NSURLCache sharedURLCache];
|
||||
}
|
||||
return [[NSURLCache alloc] initWithMemoryCapacity:20 * 1024 * 1024
|
||||
diskCapacity:150 * 1024 * 1024
|
||||
diskPath:@"com.alamofire.imagedownloader"];
|
||||
#if TARGET_OS_MACCATALYST
|
||||
return [[NSURLCache alloc] initWithMemoryCapacity:memoryCapacity
|
||||
diskCapacity:diskCapacity
|
||||
directoryURL:cacheURL];
|
||||
#else
|
||||
return [[NSURLCache alloc] initWithMemoryCapacity:memoryCapacity
|
||||
diskCapacity:diskCapacity
|
||||
diskPath:[cacheURL path]];
|
||||
#endif
|
||||
}
|
||||
|
||||
+ (NSURLSessionConfiguration *)defaultURLSessionConfiguration {
|
||||
@@ -163,7 +167,7 @@
|
||||
if (self = [super init]) {
|
||||
self.sessionManager = sessionManager;
|
||||
|
||||
self.downloadPrioritizaton = downloadPrioritization;
|
||||
self.downloadPrioritization = downloadPrioritization;
|
||||
self.maximumActiveDownloads = maximumActiveDownloads;
|
||||
self.imageCache = imageCache;
|
||||
|
||||
@@ -254,14 +258,14 @@
|
||||
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
|
||||
dispatch_async(self.responseQueue, ^{
|
||||
__strong __typeof__(weakSelf) strongSelf = weakSelf;
|
||||
AFImageDownloaderMergedTask *mergedTask = strongSelf.mergedTasks[URLIdentifier];
|
||||
AFImageDownloaderMergedTask *mergedTask = [strongSelf safelyGetMergedTask:URLIdentifier];
|
||||
if ([mergedTask.identifier isEqual:mergedTaskIdentifier]) {
|
||||
mergedTask = [strongSelf safelyRemoveMergedTaskWithURLIdentifier:URLIdentifier];
|
||||
if (error) {
|
||||
for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) {
|
||||
if (handler.failureBlock) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
handler.failureBlock(request, (NSHTTPURLResponse*)response, error);
|
||||
handler.failureBlock(request, (NSHTTPURLResponse *)response, error);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -273,7 +277,7 @@
|
||||
for (AFImageDownloaderResponseHandler *handler in mergedTask.responseHandlers) {
|
||||
if (handler.successBlock) {
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
handler.successBlock(request, (NSHTTPURLResponse*)response, responseObject);
|
||||
handler.successBlock(request, (NSHTTPURLResponse *)response, responseObject);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -333,14 +337,14 @@
|
||||
}
|
||||
}
|
||||
|
||||
if (mergedTask.responseHandlers.count == 0 && mergedTask.task.state == NSURLSessionTaskStateSuspended) {
|
||||
if (mergedTask.responseHandlers.count == 0) {
|
||||
[mergedTask.task cancel];
|
||||
[self removeMergedTaskWithURLIdentifier:URLIdentifier];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
- (AFImageDownloaderMergedTask*)safelyRemoveMergedTaskWithURLIdentifier:(NSString *)URLIdentifier {
|
||||
- (AFImageDownloaderMergedTask *)safelyRemoveMergedTaskWithURLIdentifier:(NSString *)URLIdentifier {
|
||||
__block AFImageDownloaderMergedTask *mergedTask = nil;
|
||||
dispatch_sync(self.synchronizationQueue, ^{
|
||||
mergedTask = [self removeMergedTaskWithURLIdentifier:URLIdentifier];
|
||||
@@ -383,7 +387,7 @@
|
||||
}
|
||||
|
||||
- (void)enqueueMergedTask:(AFImageDownloaderMergedTask *)mergedTask {
|
||||
switch (self.downloadPrioritizaton) {
|
||||
switch (self.downloadPrioritization) {
|
||||
case AFImageDownloadPrioritizationFIFO:
|
||||
[self.queuedMergedTasks addObject:mergedTask];
|
||||
break;
|
||||
@@ -404,6 +408,14 @@
|
||||
return self.activeRequestCount < self.maximumActiveDownloads;
|
||||
}
|
||||
|
||||
- (AFImageDownloaderMergedTask *)safelyGetMergedTask:(NSString *)URLIdentifier {
|
||||
__block AFImageDownloaderMergedTask *mergedTask;
|
||||
dispatch_sync(self.synchronizationQueue, ^(){
|
||||
mergedTask = self.mergedTasks[URLIdentifier];
|
||||
});
|
||||
return mergedTask;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
+7
-27
@@ -109,11 +109,9 @@ typedef void (^AFNetworkActivityActionBlock)(BOOL networkActivityIndicatorVisibl
|
||||
|
||||
- (void)setNetworkActivityIndicatorVisible:(BOOL)networkActivityIndicatorVisible {
|
||||
if (_networkActivityIndicatorVisible != networkActivityIndicatorVisible) {
|
||||
[self willChangeValueForKey:@"networkActivityIndicatorVisible"];
|
||||
@synchronized(self) {
|
||||
_networkActivityIndicatorVisible = networkActivityIndicatorVisible;
|
||||
_networkActivityIndicatorVisible = networkActivityIndicatorVisible;
|
||||
}
|
||||
[self didChangeValueForKey:@"networkActivityIndicatorVisible"];
|
||||
if (self.networkActivityActionBlock) {
|
||||
self.networkActivityActionBlock(networkActivityIndicatorVisible);
|
||||
} else {
|
||||
@@ -122,35 +120,20 @@ typedef void (^AFNetworkActivityActionBlock)(BOOL networkActivityIndicatorVisibl
|
||||
}
|
||||
}
|
||||
|
||||
- (void)setActivityCount:(NSInteger)activityCount {
|
||||
@synchronized(self) {
|
||||
_activityCount = activityCount;
|
||||
}
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self updateCurrentStateForNetworkActivityChange];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)incrementActivityCount {
|
||||
[self willChangeValueForKey:@"activityCount"];
|
||||
@synchronized(self) {
|
||||
_activityCount++;
|
||||
}
|
||||
[self didChangeValueForKey:@"activityCount"];
|
||||
|
||||
@synchronized(self) {
|
||||
self.activityCount++;
|
||||
}
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self updateCurrentStateForNetworkActivityChange];
|
||||
});
|
||||
}
|
||||
|
||||
- (void)decrementActivityCount {
|
||||
[self willChangeValueForKey:@"activityCount"];
|
||||
@synchronized(self) {
|
||||
_activityCount = MAX(_activityCount - 1, 0);
|
||||
}
|
||||
[self didChangeValueForKey:@"activityCount"];
|
||||
|
||||
@synchronized(self) {
|
||||
self.activityCount = MAX(_activityCount - 1, 0);
|
||||
}
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[self updateCurrentStateForNetworkActivityChange];
|
||||
});
|
||||
@@ -172,7 +155,6 @@ typedef void (^AFNetworkActivityActionBlock)(BOOL networkActivityIndicatorVisibl
|
||||
- (void)setCurrentState:(AFNetworkActivityManagerState)currentState {
|
||||
@synchronized(self) {
|
||||
if (_currentState != currentState) {
|
||||
[self willChangeValueForKey:@"currentState"];
|
||||
_currentState = currentState;
|
||||
switch (currentState) {
|
||||
case AFNetworkActivityManagerStateNotActive:
|
||||
@@ -191,9 +173,7 @@ typedef void (^AFNetworkActivityActionBlock)(BOOL networkActivityIndicatorVisibl
|
||||
[self startCompletionDelayTimer];
|
||||
break;
|
||||
}
|
||||
[self didChangeValueForKey:@"currentState"];
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -103,11 +103,11 @@ static const char * af_backgroundImageDownloadReceiptKeyForState(UIControlState
|
||||
|
||||
+ (AFImageDownloader *)sharedImageDownloader {
|
||||
|
||||
return objc_getAssociatedObject(self, @selector(sharedImageDownloader)) ?: [AFImageDownloader defaultInstance];
|
||||
return objc_getAssociatedObject([UIButton class], @selector(sharedImageDownloader)) ?: [AFImageDownloader defaultInstance];
|
||||
}
|
||||
|
||||
+ (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader {
|
||||
objc_setAssociatedObject(self, @selector(sharedImageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
objc_setAssociatedObject([UIButton class], @selector(sharedImageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
@@ -168,7 +168,7 @@ static const char * af_backgroundImageDownloadReceiptKeyForState(UIControlState
|
||||
if ([[strongSelf af_imageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
|
||||
if (success) {
|
||||
success(request, response, responseObject);
|
||||
} else if(responseObject) {
|
||||
} else if (responseObject) {
|
||||
[strongSelf setImage:responseObject forState:state];
|
||||
}
|
||||
[strongSelf af_setImageDownloadReceipt:nil forState:state];
|
||||
@@ -247,7 +247,7 @@ static const char * af_backgroundImageDownloadReceiptKeyForState(UIControlState
|
||||
if ([[strongSelf af_backgroundImageDownloadReceiptForState:state].receiptID isEqual:downloadID]) {
|
||||
if (success) {
|
||||
success(request, response, responseObject);
|
||||
} else if(responseObject) {
|
||||
} else if (responseObject) {
|
||||
[strongSelf setBackgroundImage:responseObject forState:state];
|
||||
}
|
||||
[strongSelf af_setBackgroundImageDownloadReceipt:nil forState:state];
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
//
|
||||
// UIImage+AFNetworking.h
|
||||
//
|
||||
//
|
||||
// Created by Paulo Ferreira on 08/07/15.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#if TARGET_OS_IOS || TARGET_OS_TV
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
|
||||
@interface UIImage (AFNetworking)
|
||||
|
||||
+ (UIImage*) safeImageWithData:(NSData*)data;
|
||||
|
||||
@end
|
||||
|
||||
#endif
|
||||
@@ -48,11 +48,11 @@
|
||||
@implementation UIImageView (AFNetworking)
|
||||
|
||||
+ (AFImageDownloader *)sharedImageDownloader {
|
||||
return objc_getAssociatedObject(self, @selector(sharedImageDownloader)) ?: [AFImageDownloader defaultInstance];
|
||||
return objc_getAssociatedObject([UIImageView class], @selector(sharedImageDownloader)) ?: [AFImageDownloader defaultInstance];
|
||||
}
|
||||
|
||||
+ (void)setSharedImageDownloader:(AFImageDownloader *)imageDownloader {
|
||||
objc_setAssociatedObject(self, @selector(sharedImageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
objc_setAssociatedObject([UIImageView class], @selector(sharedImageDownloader), imageDownloader, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
|
||||
}
|
||||
|
||||
#pragma mark -
|
||||
@@ -75,7 +75,6 @@
|
||||
success:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, UIImage *image))success
|
||||
failure:(void (^)(NSURLRequest *request, NSHTTPURLResponse * _Nullable response, NSError *error))failure
|
||||
{
|
||||
|
||||
if ([urlRequest URL] == nil) {
|
||||
self.image = placeholderImage;
|
||||
if (failure) {
|
||||
@@ -85,7 +84,7 @@
|
||||
return;
|
||||
}
|
||||
|
||||
if ([self isActiveTaskURLEqualToURLRequest:urlRequest]){
|
||||
if ([self isActiveTaskURLEqualToURLRequest:urlRequest]) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -119,7 +118,7 @@
|
||||
if ([strongSelf.af_activeImageDownloadReceipt.receiptID isEqual:downloadID]) {
|
||||
if (success) {
|
||||
success(request, response, responseObject);
|
||||
} else if(responseObject) {
|
||||
} else if (responseObject) {
|
||||
strongSelf.image = responseObject;
|
||||
}
|
||||
[strongSelf clearActiveDownloadInformation];
|
||||
|
||||
@@ -20,23 +20,24 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#if TARGET_OS_IOS || TARGET_OS_TV
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <TargetConditionals.h>
|
||||
|
||||
#ifndef _UIKIT_AFNETWORKING_
|
||||
#define _UIKIT_AFNETWORKING_
|
||||
|
||||
#if TARGET_OS_IOS
|
||||
#if TARGET_OS_IOS || TARGET_OS_TV
|
||||
#import "AFAutoPurgingImageCache.h"
|
||||
#import "AFImageDownloader.h"
|
||||
#import "AFNetworkActivityIndicatorManager.h"
|
||||
#import "UIRefreshControl+AFNetworking.h"
|
||||
#import "UIWebView+AFNetworking.h"
|
||||
#endif
|
||||
|
||||
#import "UIActivityIndicatorView+AFNetworking.h"
|
||||
#import "UIButton+AFNetworking.h"
|
||||
#import "UIImageView+AFNetworking.h"
|
||||
#import "UIProgressView+AFNetworking.h"
|
||||
#endif /* _UIKIT_AFNETWORKING_ */
|
||||
#endif
|
||||
|
||||
#if TARGET_OS_IOS
|
||||
#import "AFNetworkActivityIndicatorManager.h"
|
||||
#import "UIRefreshControl+AFNetworking.h"
|
||||
#import "WKWebView+AFNetworking.h"
|
||||
#endif
|
||||
|
||||
#endif /* _UIKIT_AFNETWORKING_ */
|
||||
+11
-11
@@ -1,4 +1,4 @@
|
||||
// UIWebView+AFNetworking.h
|
||||
// WkWebView+AFNetworking.h
|
||||
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -26,47 +26,47 @@
|
||||
#if TARGET_OS_IOS
|
||||
|
||||
#import <UIKit/UIKit.h>
|
||||
#import <WebKit/WebKit.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@class AFHTTPSessionManager;
|
||||
|
||||
/**
|
||||
This category adds methods to the UIKit framework's `UIWebView` class. The methods in this category provide increased control over the request cycle, including progress monitoring and success / failure handling.
|
||||
|
||||
@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.
|
||||
*/
|
||||
@interface UIWebView (AFNetworking)
|
||||
@interface WKWebView (AFNetworking)
|
||||
|
||||
/**
|
||||
The session manager used to download all requests.
|
||||
The session manager used to download all request
|
||||
*/
|
||||
@property (nonatomic, strong) AFHTTPSessionManager *sessionManager;
|
||||
|
||||
/**
|
||||
Asynchronously loads the specified request.
|
||||
|
||||
|
||||
@param request A URL request identifying the location of the content to load. This must not be `nil`.
|
||||
@param navigation The WKNavigation object that containts information for tracking the loading progress of a webpage. This must not be `nil`.
|
||||
@param progress A progress object monitoring the current download progress.
|
||||
@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.
|
||||
@param failure A block object to be executed when the data task 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.
|
||||
*/
|
||||
- (void)loadRequest:(NSURLRequest *)request
|
||||
navigation:(WKNavigation * _Nonnull)navigation
|
||||
progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
|
||||
success:(nullable NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success
|
||||
failure:(nullable void (^)(NSError *error))failure;
|
||||
|
||||
/**
|
||||
Asynchronously loads the data associated with a particular request with a specified MIME type and text encoding.
|
||||
|
||||
|
||||
@param request A URL request identifying the location of the content to load. This must not be `nil`.
|
||||
@param navigation The WKNavigation object that containts information for tracking the loading progress of a webpage. This must not be `nil`.
|
||||
@param MIMEType The MIME type of the content. Defaults to the content type of the response if not specified.
|
||||
@param textEncodingName The IANA encoding name, as in `utf-8` or `utf-16`. Defaults to the response text encoding if not specified.
|
||||
@param progress A progress object monitoring the current download progress.
|
||||
@param progress A progress object monitoring the current download progress.
|
||||
@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.
|
||||
@param failure A block object to be executed when the data task 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.
|
||||
*/
|
||||
- (void)loadRequest:(NSURLRequest *)request
|
||||
navigation:(WKNavigation * _Nonnull)navigation
|
||||
MIMEType:(nullable NSString *)MIMEType
|
||||
textEncodingName:(nullable NSString *)textEncodingName
|
||||
progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
|
||||
+42
-45
@@ -1,4 +1,4 @@
|
||||
// UIWebView+AFNetworking.m
|
||||
// WkWebView+AFNetworking.m
|
||||
// Copyright (c) 2011–2016 Alamofire Software Foundation ( http://alamofire.org/ )
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -19,7 +19,7 @@
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#import "UIWebView+AFNetworking.h"
|
||||
#import "WKWebView+AFNetworking.h"
|
||||
|
||||
#import <objc/runtime.h>
|
||||
|
||||
@@ -29,11 +29,11 @@
|
||||
#import "AFURLResponseSerialization.h"
|
||||
#import "AFURLRequestSerialization.h"
|
||||
|
||||
@interface UIWebView (_AFNetworking)
|
||||
@interface WKWebView (_AFNetworking)
|
||||
@property (readwrite, nonatomic, strong, setter = af_setURLSessionTask:) NSURLSessionDataTask *af_URLSessionTask;
|
||||
@end
|
||||
|
||||
@implementation UIWebView (_AFNetworking)
|
||||
@implementation WKWebView (_AFNetworking)
|
||||
|
||||
- (NSURLSessionDataTask *)af_URLSessionTask {
|
||||
return (NSURLSessionDataTask *)objc_getAssociatedObject(self, @selector(af_URLSessionTask));
|
||||
@@ -47,9 +47,9 @@
|
||||
|
||||
#pragma mark -
|
||||
|
||||
@implementation UIWebView (AFNetworking)
|
||||
@implementation WKWebView (AFNetworking)
|
||||
|
||||
- (AFHTTPSessionManager *)sessionManager {
|
||||
- (AFHTTPSessionManager *)sessionManager {
|
||||
static AFHTTPSessionManager *_af_defaultHTTPSessionManager = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
@@ -57,7 +57,7 @@
|
||||
_af_defaultHTTPSessionManager.requestSerializer = [AFHTTPRequestSerializer serializer];
|
||||
_af_defaultHTTPSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
|
||||
});
|
||||
|
||||
|
||||
return objc_getAssociatedObject(self, @selector(sessionManager)) ?: _af_defaultHTTPSessionManager;
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@
|
||||
dispatch_once(&onceToken, ^{
|
||||
_af_defaultResponseSerializer = [AFHTTPResponseSerializer serializer];
|
||||
});
|
||||
|
||||
|
||||
return objc_getAssociatedObject(self, @selector(responseSerializer)) ?: _af_defaultResponseSerializer;
|
||||
}
|
||||
|
||||
@@ -82,11 +82,11 @@
|
||||
#pragma mark -
|
||||
|
||||
- (void)loadRequest:(NSURLRequest *)request
|
||||
navigation:(WKNavigation * _Nonnull)navigation
|
||||
progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
|
||||
success:(NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success
|
||||
failure:(void (^)(NSError *error))failure
|
||||
{
|
||||
[self loadRequest:request MIMEType:nil textEncodingName:nil progress:progress success:^NSData *(NSHTTPURLResponse *response, NSData *data) {
|
||||
success:(nullable NSString * (^)(NSHTTPURLResponse *response, NSString *HTML))success
|
||||
failure:(nullable void (^)(NSError *error))failure {
|
||||
[self loadRequest:request navigation:navigation MIMEType:nil textEncodingName:nil progress:progress success:^NSData * _Nonnull(NSHTTPURLResponse * _Nonnull response, NSData * _Nonnull data) {
|
||||
NSStringEncoding stringEncoding = NSUTF8StringEncoding;
|
||||
if (response.textEncodingName) {
|
||||
CFStringEncoding encoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)response.textEncodingName);
|
||||
@@ -94,61 +94,58 @@
|
||||
stringEncoding = CFStringConvertEncodingToNSStringEncoding(encoding);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
NSString *string = [[NSString alloc] initWithData:data encoding:stringEncoding];
|
||||
if (success) {
|
||||
string = success(response, string);
|
||||
}
|
||||
|
||||
|
||||
return [string dataUsingEncoding:stringEncoding];
|
||||
} failure:failure];
|
||||
}
|
||||
|
||||
- (void)loadRequest:(NSURLRequest *)request
|
||||
MIMEType:(NSString *)MIMEType
|
||||
textEncodingName:(NSString *)textEncodingName
|
||||
navigation:(WKNavigation * _Nonnull)navigation
|
||||
MIMEType:(nullable NSString *)MIMEType
|
||||
textEncodingName:(nullable NSString *)textEncodingName
|
||||
progress:(NSProgress * _Nullable __autoreleasing * _Nullable)progress
|
||||
success:(NSData * (^)(NSHTTPURLResponse *response, NSData *data))success
|
||||
failure:(void (^)(NSError *error))failure
|
||||
{
|
||||
success:(nullable NSData * (^)(NSHTTPURLResponse *response, NSData *data))success
|
||||
failure:(nullable void (^)(NSError *error))failure {
|
||||
NSParameterAssert(request);
|
||||
|
||||
|
||||
if (self.af_URLSessionTask.state == NSURLSessionTaskStateRunning || self.af_URLSessionTask.state == NSURLSessionTaskStateSuspended) {
|
||||
[self.af_URLSessionTask cancel];
|
||||
}
|
||||
self.af_URLSessionTask = nil;
|
||||
|
||||
|
||||
__weak __typeof(self)weakSelf = self;
|
||||
__block NSURLSessionDataTask *dataTask;
|
||||
dataTask = [self.sessionManager
|
||||
dataTaskWithRequest:request
|
||||
uploadProgress:nil
|
||||
downloadProgress:nil
|
||||
completionHandler:^(NSURLResponse * _Nonnull response, id _Nonnull responseObject, NSError * _Nullable error) {
|
||||
__strong __typeof(weakSelf) strongSelf = weakSelf;
|
||||
if (error) {
|
||||
if (failure) {
|
||||
failure(error);
|
||||
}
|
||||
} else {
|
||||
if (success) {
|
||||
success((NSHTTPURLResponse *)response, responseObject);
|
||||
}
|
||||
[strongSelf loadData:responseObject MIMEType:MIMEType textEncodingName:textEncodingName baseURL:[dataTask.currentRequest URL]];
|
||||
|
||||
if ([strongSelf.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
|
||||
[strongSelf.delegate webViewDidFinishLoad:strongSelf];
|
||||
}
|
||||
}
|
||||
}];
|
||||
__strong __typeof(weakSelf) strongSelf = weakSelf;
|
||||
__strong __typeof(weakSelf.navigationDelegate) strongSelfDelegate = strongSelf.navigationDelegate;
|
||||
dataTask = [self.sessionManager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
|
||||
if (error) {
|
||||
if (failure) {
|
||||
failure(error);
|
||||
}
|
||||
} else {
|
||||
if (success) {
|
||||
success((NSHTTPURLResponse *)response, responseObject);
|
||||
}
|
||||
[strongSelf loadData:responseObject MIMEType:MIMEType characterEncodingName:textEncodingName baseURL:[dataTask.currentRequest URL]];
|
||||
|
||||
if ([strongSelfDelegate respondsToSelector:@selector(webView:didFinishNavigation:)]) {
|
||||
[strongSelfDelegate webView:strongSelf didFinishNavigation:navigation];
|
||||
}
|
||||
}
|
||||
}];
|
||||
self.af_URLSessionTask = dataTask;
|
||||
if (progress != nil) {
|
||||
*progress = [self.sessionManager downloadProgressForTask:dataTask];
|
||||
}
|
||||
[self.af_URLSessionTask resume];
|
||||
|
||||
if ([self.delegate respondsToSelector:@selector(webViewDidStartLoad:)]) {
|
||||
[self.delegate webViewDidStartLoad:self];
|
||||
|
||||
if ([strongSelfDelegate respondsToSelector:@selector(webView:didStartProvisionalNavigation:)]) {
|
||||
[strongSelfDelegate webView:self didStartProvisionalNavigation:navigation];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user