UIWebView webViewDidStartLoad 被调用并带有属性为 null 的请求
我目前正在调试 UIWebView,以获得一些信息来提高性能(在服务器和 iPhone 上)。 我注意到在调用 loadRequest: 后
- (void)webViewDidStartLoad:(UIWebView *)webView_
调用了回调,但是请求的每个参数都是空的。
我使用以下语句:
- (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]);
}
输出是:
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)
对此行为有任何解释或可以解决此问题吗?
页面加载正常,但是加载任何内容的请求似乎需要大约 30 秒,我试图避免这种情况。
编辑:有关加载网络视图的一些附加信息。我正在调用一个方法,将 webview 添加到 UIView 并加载 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];
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];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您看不到请求的原因是,在加载请求之前,不会分配 Web 视图的 request 属性。在所有重定向之后,这可能只显示实际显示的请求。如果您想要在重定向等之前的初始请求对象,请使用 ToddH 的答案。对于最终请求,您必须在 webViewDidFinishLoad 中检查它。
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.
好吧,在解决同样的问题之后,我发现解决方案是使用这个 UIWebViewDelegate 方法:
它在 viewDidStartLoad 之前被调用,并传递视图将加载的请求对象。
Well, after struggling with this same issue I discovered that the solution is to use this UIWebViewDelegate method:
It gets called right before viewDidStartLoad and is passed the request object that the view will load with.
您没有检查的一件事是请求 URL 属性。检查一下,看看你得到什么:
编辑:
看起来你可能在自动释放对象方面遇到问题。尝试创建与 NSURLRequest 分开的 NSURL:
The one thing you didn't check is the request URL property. Check that, and see what you get:
Edit:
It looks like you might be having problems with autoreleased objects. Try creating the NSURL separate fromt he NSURLRequest: