我编写了一个自定义的NSURLProtocol(“UrlProtocol”),以在导航到特定网站时拦截来自UIWebView的请求,并在发送之前应用额外的HTTP header 。我跟随https://www.raywenderlich.com/59982/nsurlprotocol-tutorial参加 worker 类(Class)的培训。我的问题来自从已弃用的NSURLConnection切换到NSURLSession:我测试了一个非常简单的单文件html页面,该页面成功加载。但是,具有js文件,图像等资源的稍微复杂的网站将超时,而使用NSURLConnection则整个网站将在几秒钟内加载。

我将使用UrlProtocol粘贴原始的NSURLConnection,然后使用NSURLSession粘贴新的类。原本的:

#import "UrlProtocol.h"
#import "Globals.h"

@implementation UrlProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
    if (![request.URL.absoluteString hasPrefix:@"http"]) return NO; //No need to intercept non-http requests

    if ([NSURLProtocol propertyForKey:@"handled" inRequest:request]) {
        return NO;
    }

    return YES;
}

+ (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request {
    NSString* key = @"custom-auth-header";
    Globals* globals = [Globals getInstance];
    NSString* token = [globals token];

    NSMutableURLRequest *newRequest = [request mutableCopy]; //Create a mutable copy that can be modified
    [newRequest setValue:token forHTTPHeaderField:key];

    return [newRequest copy]; //return a non-mutable copy
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest *)a toRequest:(NSURLRequest *)b {
    return [super requestIsCacheEquivalent:a toRequest:b];
}

- (void)startLoading {
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:@"handled" inRequest:newRequest];

    self.connection = [NSURLConnection connectionWithRequest:newRequest delegate:self];
}

- (void)stopLoading {
    NSLog(@"stopLoading");
    [self.connection cancel];
    self.connection = nil;
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [self.client URLProtocol:self didLoadData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [self.client URLProtocolDidFinishLoading:self];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    [self.client URLProtocol:self didFailWithError:error];
}

@end

针对每个请求使用UrlProtocol的新NSURLSessionDataTasks:
#import "UrlProtocol.h"
#import "Globals.h"

@implementation UrlProtocol

+ (BOOL)canInitWithRequest:(NSURLRequest * _Nonnull) request {
    if (![request.URL.absoluteString hasPrefix:@"http"]) return NO; //No need to intercept non-http requests

    if ([NSURLProtocol propertyForKey:@"handled" inRequest:request]) {
        return NO;
    }

    return YES;
}

+ (NSURLRequest * _Nonnull)canonicalRequestForRequest:(NSURLRequest * _Nonnull)request {
    NSString* key = @"custom-auth-header";
    Globals* globals = [Globals getInstance];
    NSString* token = [globals token];

    NSMutableURLRequest *newRequest = [request mutableCopy]; //Create a mutable copy that can be modified
    [newRequest setValue:token forHTTPHeaderField:key];
    return [newRequest copy]; //return a non-mutable copy
}

+ (BOOL)requestIsCacheEquivalent:(NSURLRequest * _Nonnull)a toRequest:(NSURLRequest * _Nonnull)b {
    return [super requestIsCacheEquivalent:a toRequest:b];
}

- (void)startLoading {
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:@"handled" inRequest:newRequest];
    [Globals setUrlSessionDelegate:self];
    Globals* globals = [Globals getInstance];
    self.dataTask = [globals.session dataTaskWithRequest:newRequest];
    [self.dataTask resume];
}

- (void)URLSession:(NSURLSession * _Nonnull)session dataTask:(NSURLSessionDataTask * _Nullable)dataTask didReceiveData:(NSData * _Nullable)data{
    [self.client URLProtocol:self didLoadData:data];
}

- (void)URLSession:(NSURLSession * _Nonnull)session dataTask:(NSURLSessionDataTask * _Nullable)dataTask didReceiveResponse:(NSURLResponse * _Nullable)response
                 completionHandler:(void (^ _Nullable)(NSURLSessionResponseDisposition))completionHandler{
    [self.client URLProtocol:self didReceiveResponse:response cacheStoragePolicy:NSURLCacheStorageNotAllowed];
    completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession * _Nonnull)session task:(NSURLSessionTask * _Nonnull)task didCompleteWithError:(NSError * _Nullable)error{
    if (error){
        [self.client URLProtocol:self didFailWithError:error];
    } else {
        [self.client URLProtocolDidFinishLoading:self];
    }
}

- (void)URLSession:(NSURLSession * _Nonnull)session task:(NSURLSessionTask * _Nonnull)task willPerformHTTPRedirection:(NSHTTPURLResponse * _Nonnull)response
                        newRequest:(NSURLRequest * _Nonnull)request completionHandler:(void (^ _Nonnull)(NSURLRequest * _Nullable))completionHandler {
    completionHandler(request);
}

- (void)stopLoading {
    [self.dataTask cancel];
    self.dataTask = nil;
}

@end

“全局”是一个单例,我在其中初始化了一个NSURLSession,打算在应用程序的整个运行时使用它。它还包含我设置为所有请求的自定义HTTP header 的 token :
#import "Globals.h"
#import "UrlProtocol.h"

@implementation Globals
@synthesize token;
@synthesize session;

static Globals *instance = nil;

+(Globals*) getInstance
{
    @synchronized(self)
    {
        if (instance == nil)
        {
            instance = [Globals new];
        }
    }
    return instance;
}

//UrlProtocol class has no init method, so the NSURLSession delegate is being set on url load. We will ensure only one NSURLSession is created.
+(void) setUrlSessionDelegate:(UrlProtocol*) urlProtocol{
    Globals* globals = [Globals getInstance];
    if (!globals.session){
        globals.session = [NSURLSession sessionWithConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration  delegate:urlProtocol delegateQueue:nil];
    }
}

@end

最佳答案

通过为每个NSURLSessionDataTask创建一个新的默认NSURLSession解决了我的问题。我尝试为所有任务共享一个NSURLSession的方式出了点问题。 URLProtocol的startLoading方法现在如下:

- (void)startLoading {
    NSMutableURLRequest *newRequest = [self.request mutableCopy];
    [NSURLProtocol setProperty:@YES forKey:@"handled" inRequest:newRequest];
    NSURLSessionConfiguration* config = NSURLSessionConfiguration.defaultSessionConfiguration;
    NSURLSession* session = [NSURLSession sessionWithConfiguration:config  delegate:self delegateQueue:nil];
    self.dataTask = [session dataTaskWithRequest:newRequest];
    [self.dataTask resume];
}

之前的简单HTML页面测试必须已经完成,因为仅需执行一项任务即可加载页面

关于ios - 将NSURLConnection切换为NSURLSession后,自定义NSURLProtocol变慢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41477434/

10-12 14:36