从Cocoa(OSX,而​​不是iPhone)WebView中仅加载Safari中指定的链接?

时间:2012-04-26 22:10:46

标签: objective-c macos webview

我在这里发现了一些帖子,解释了如何在Safari(或默认浏览器)中的嵌入式WebView中打开所有链接,但我找不到任何关于你如何去做的事情在Safari中只打开您选择的链接,而默认情况下是在WebView中加载它们。有什么想法吗?

1 个答案:

答案 0 :(得分:4)

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

为此,您必须实施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];
    }

您点击此链接将在[请求网址]

收到此委托方法