1

In my case, I need to add some custom headers for some special requests. so I add following code in UIWebViewDelegate:

- (BOOL)webView:(UIWebView *)webView
shouldStartLoadWithRequest:(NSURLRequest *)request
 navigationType:(UIWebViewNavigationType)navigationType
{
NSString *host = [[request URL] host];
static NSString *testTag = @"test_tag";

if ([host hasSuffix:@"yixin.im"]) // add special tag for special url(base on host)
{
    if ([[request allHTTPHeaderFields] objectForKey:testTag] == nil)
    {
        NSMutableURLRequest *aRequest = [request mutableCopy];
        [aRequest addValue:@"1" forHTTPHeaderField:testTag];
        [webView loadRequest:aRequest];            
        return NO;
    }
}
else //remove special tag for other url
{
    if ([[request allHTTPHeaderFields] objectForKey:testTag])
    {
        NSMutableURLRequest *aRequest = [request mutableCopy];
        [aRequest addValue:nil forHTTPHeaderField:testTag];
        [webView loadRequest:aRequest];
        return NO;
    }
}
return YES;

}

However, it crashed while loading the same url second times. The crash stack is quite strange:

crash stack

I found that the crash is related to UIWebView cache. After removing ApplicationCache.db in Cache directory, it won't crash. However, I couldn't find the way to fix this crash.

My demo project is showed following: crash project. Follow the next two step, you will see the crash.

  1. launch the app,you will see a webpage.
  2. then relaunch the app,it crashes.

Some methods I have tried:

1.replace addValue:forHTTPHeaderField: by setValue:forHTTPHeaderField, it didn't work.

2.reset http headers by calling setAllHTTPHeaderFields:,it didn't work too.

3.replace nil by @"",it didn't work to.

4.replace return NO by return YES,it works...However,it doesn't make sense....

Cœur
  • 37,241
  • 25
  • 195
  • 267
amao
  • 41
  • 4

0 Answers0