Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIWebView webViewDidStartLoad is called with request which properties are null

I am currently debugging a UIWebView in order to get some information to improve performance (on server and iPhone). I noticed that after calling loadRequest: the callback

- (void)webViewDidStartLoad:(UIWebView *)webView_ 

is called, however each parameter of the request is null.

I am using the following statement:

    - (void)webViewDidStartLoad:(UIWebView *)webView_{
    NSLog(@"%@ \t Start Request: %@ \n absolute: %@ \n Method: %@ \n Parameters: %@ \n Port: %@ \n Query: %@ \n Header Fields: %@ \n HTTPBody: %@ \n HTTPBodyStream: %@", [NSDate date], [[webView_ request] mainDocumentURL], [[[webView_ request] mainDocumentURL] absoluteString], [[webView_ request] HTTPMethod], [[[webView_ request] mainDocumentURL] parameterString], [[[webView_ request] mainDocumentURL] port], [[[webView_ request] mainDocumentURL] query], [[webView_ request] allHTTPHeaderFields], [[webView_ request] HTTPBody], [[webView_ request] HTTPBodyStream]);
}

The output is:

 2011-05-11 17:15:34 +0200    Start Request: (null) 
 absolute: (null) 
 Method: GET 
 Parameters: (null) 
 Port: (null) 
 Query: (null) 
 Header Fields: {
} 
 HTTPBody: (null) 
 HTTPBodyStream: (null)

Is there any explanation for this behavior or anything to fix this?

The page loads fine, however the request loading nothing seems to take about 30 seconds which I try to avoid.

edit: some additional information about loading the webview. I am calling a method which adds the webview to the UIView and loads the URL

    UIWebView * web = [[UIWebView alloc] initWithFrame:CGRectMake(indent, topindent+indent, screenSize.width-2*indent, screenSize.height-2*indent-topindent)];
     web.delegate = self;
    [view addSubview:web];
    NSURLRequest * someUrl = [NSURLRequest requestWithURL:[NSURL URLWithString:
@"some_URL"]];
    [web loadRequest: someUrl];
like image 335
st-h Avatar asked May 11 '11 15:05

st-h


2 Answers

The reason you're not seeing a request is that the request property of the webview isn't assigned until the request has been loaded. This is likely to only show the actual displayed request, after all redirects. If you want the initial request object, before redirects etc, use ToddH's answer. For the final request, you will have to check it in webViewDidFinishLoad.

like image 113
Smeedge Avatar answered Oct 08 '22 12:10

Smeedge


Well, after struggling with this same issue I discovered that the solution is to use this UIWebViewDelegate method:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType

It gets called right before viewDidStartLoad and is passed the request object that the view will load with.

like image 36
ToddH Avatar answered Oct 08 '22 11:10

ToddH