0

我在这里找到了几篇文章,解释了如何在 Safari(或默认浏览器)中打开嵌入式 WebView 中的所有链接,但我找不到任何关于如何在 Safari 中仅打开您选择的链接的信息,而默认情况下,它们将加载到 WebView 中。有任何想法吗?

4

1 回答 1

5

您实际上可以在默认浏览器中加载您单击的链接。

为此,您必须实施WebPolicyDecisionListener协议并policyDelegate设置WebView

[webView setPolicyDelegate:self];

然后实现以下方法:

-(void)webView:(WebView *)webView decidePolicyForNavigationAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request frame:(WebFrame *)frame decisionListener:(id<WebPolicyDecisionListener>)listener
    {

        if (WebNavigationTypeLinkClicked == [[actionInformation objectForKey:WebActionNavigationTypeKey] intValue])
        {
            [listener ignore];
            NSLog(@"Opening URL in browser:%@",[request URL]);
            [[NSWorkspace sharedWorkspace] openURL:[request URL]];

        }    
        [listener use];
    }

-(void)webView:(WebView *)webView decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request newFrameName:(NSString *)frameName decisionListener:(id <WebPolicyDecisionListener>)listener
    {
        if (WebNavigationTypeLinkClicked == [[actionInformation objectForKey:WebActionNavigationTypeKey] intValue])
        {
            [listener ignore];
            NSLog(@"Opening URL new window:%@",[request URL]);
            [[NSWorkspace sharedWorkspace] openURL:[request URL]];
        }
        [listener ignore];
    }

您单击的此链接将在 [request URL] 处接收到此委托方法

于 2013-03-04T11:25:42.323 回答