Cocoa WebKit/WebView 委托用于位置更改? (用户点击链接、javascript 操作等)
我已经向 Cocoa 应用程序添加了一个 WebView,并且我正在尝试找出可以使用哪个委托来检测导航何时发生变化(用户单击链接,或触发 javascript 来更改位置等)。
tldr - 试图弄清楚如何设置委托以在 WebView 更改 URL 时获取事件。 (具体来说,我需要知道新的 URL 是什么)。
I have added a WebView to a Cocoa application and I'm trying to figure out which delegate I can use to detect when navigation changes (user clicks a link, or javascript fires to change the location, etc).
The WebView class reference contains four delegates (WebFrameLoadDelegate Protocol Reference, WebPolicyDelegate Protocol Reference, WebResourceLoadDelegate Protocol Reference and WebUIDelegate Protocol Reference) and I have looked at each of these, but I cannot seem to find how to detect this. Am I missing something blatantly obvious or is there no way to get this?
tldr - Trying to figure out how I can set a delegate to get an event when the WebView is changing URL. (Specifically I need to know what the new URL is).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 WebKit(即 MacOS X),那么跟踪请求的最佳位置可能是在
webView:identifierForInitialRequest:fromDataSource:
中,尽管webView:resource:willSendRequest:redirectResponse:fromDataSource:
是也不错,优点是可以让你修改请求。与 iOS 一样,您可以使用
[请求 URL]
。通过
[webView setResourceLoadDelegate:self];
进行设置。在我看来,资源加载委托(如上所述)比策略委托方法提供了更好的选项来处理请求。
Using WebKit (i.e. MacOS X) then probably the best place to track requests is in
webView:identifierForInitialRequest:fromDataSource:
, althoughwebView:resource:willSendRequest:redirectResponse:fromDataSource:
is also good, and has the advantage of allowing you to modify the request.As with iOS, you can use
[request URL]
.Set this via
[webView setResourceLoadDelegate:self];
.The resource load delegate (as above) gives better options to handle the request, in my opinion, than the policy delegate methods.
我建议您实现这两个
WebPolicyDelegate
方法:这将告诉您导航到的新 URL,同时也让您有机会更改默认行为。例如,您可能无法通过 WebKit 尝试打开新窗口。
I recommend you implement these two
WebPolicyDelegate
methods:That will tell you the new URL being navigated to, but also give you a chance to change the default behaviour. e.g. you may not WebKit attempting to open new windows.
实现
在正文中 并测试导航类型。您的类需要设置为 Web 视图的委托,并实现
UIWebViewDelegate
协议。可以从请求中获取URL:Implement
and test the navigation type within the body. Your class needs to be set as the delegate of the web view, and implement the
UIWebViewDelegate
protocol. The URL can be obtained from the request: