Cocoa WebKit/WebView 委托用于位置更改? (用户点击链接、javascript 操作等)

发布于 2024-12-09 18:23:37 字数 519 浏览 0 评论 0原文

我已经向 Cocoa 应用程序添加了一个 WebView,并且我正在尝试找出可以使用哪个委托来检测导航何时发生变化(用户单击链接,或触发 javascript 来更改位置等)。

WebView 类参考 包含四个委托(WebFrameLoadDelegate 协议参考、WebPolicyDelegate 协议参考、WebResourceLoadDelegate 协议参考和 WebUIDelegate 协议参考),我已经查看了每个委托这些,但我似乎无法找到如何检测到这一点。我是否遗漏了一些明显明显的东西,或者没有办法得到它?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

猛虎独行 2024-12-16 18:23:37

使用 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:, although webView: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.

我最亲爱的 2024-12-16 18:23:37

我建议您实现这两个 WebPolicyDelegate 方法:

-webView:decidePolicyForNavigationAction:request:frame:decisionListener:
-webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:

这将告诉您导航到的新 URL,同时也让您有机会更改默认行为。例如,您可能无法通过 WebKit 尝试打开新窗口。

I recommend you implement these two WebPolicyDelegate methods:

-webView:decidePolicyForNavigationAction:request:frame:decisionListener:
-webView:decidePolicyForNewWindowAction:request:newFrameName:decisionListener:

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.

半岛未凉 2024-12-16 18:23:37

实现

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

在正文中 并测试导航类型。您的类需要设置为 Web 视图的委托,并实现 UIWebViewDelegate 协议。可以从请求中获取URL:

[request URL]

Implement

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    return YES;
}

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:

[request URL]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文