NSURLProtocol isn't asked to load after YES response to canInitWithRequest

后端 未结 2 834
故里飘歌
故里飘歌 2020-12-11 12:48

I\'m registering an implementation of NSURLProtocol to do some custom handling of certain URL requests (I tag these requests by setting properties on them). These URL reques

相关标签:
2条回答
  • 2020-12-11 13:35
    *** Way to clear the cache of webview **********
    Follow this code:
    
    NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        for (NSHTTPCookie *cookie in [storage cookies]) {
            [storage deleteCookie:cookie];
        }
        [[NSUserDefaults standardUserDefaults] synchronize];
    
    0 讨论(0)
  • 2020-12-11 13:39

    I was able to workaround this issue by cache-busting the UIWebView cache, while not busting the NSURLCache.

    1. Add a unique param to the query params of the original request. I chose 'key=000000' where the value is zero-led six digit random number.
    2. In the protocol, strip the key in canonicalRequestForRequest: and in initWithRequest:cachedResponse:client

    My stripping code looks like this (there might be a cleaner way to strip the param, but this works):

    + (NSURLRequest *)canonicalRequestForRequest:(NSURLRequest *)request
    {
        NSURLRequest *canonicalRequest = request;
        BOOL myProtocolRequest = [[NSURLProtocol propertyForKey:kMYProtocolRequest inRequest:request] boolValue];
        if (myProtocolRequest)
        {
            NSMutableURLRequest *mutableRequest = [request mutableCopyWorkaround];
            NSString *originalURLString = mutableRequest.URL.absoluteString;
            NSString *regexString = [NSString stringWithFormat:@"(?:[?&])(key=[[:digit:]]{%d}&*)", kMYKeyLength];
    
            NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:regexString options:0 error:0];
            NSTextCheckingResult *result = [regex firstMatchInString:originalURLString options:0 range:NSMakeRange(0, originalURLString.length)];
            if (result.numberOfRanges > 1)
            {
                NSRange keyRange = [result rangeAtIndex:1];
                NSLog(@"Removing '%@' from request", [originalURLString substringWithRange:keyRange]);
                NSString *replacementURLString = [originalURLString stringByReplacingCharactersInRange:keyRange withString:@""];
                mutableRequest.URL = [NSURL URLWithString:replacementURLString];
                canonicalRequest = mutableRequest;
            }
        }
    
        return canonicalRequest;
    }
    

    My init code looks like this:

    - (id)initWithRequest:(NSURLRequest *)request cachedResponse:(NSCachedURLResponse *)cachedResponse client:(id<NSURLProtocolClient>)client
    {
        self = [super initWithRequest:[MYURLProtocol canonicalRequestForRequest:request] cachedResponse:cachedResponse client:client];
        return self;
    }
    

    I don't like that I have to do this, but I'm finally getting exactly the behavior I want. Hopefully it helps someone out there.

    0 讨论(0)
提交回复
热议问题