【问题标题】:How to authorize or not a WebView to load a page url如何授权或不授权 WebView 加载页面 url
【发布时间】:2012-02-25 17:43:25
【问题描述】:

我研究如何控制 WebView(用于 Mac 应用程序)以允许特定的 url,如带有

的 iO

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;

提前谢谢你。

【问题讨论】:

  • 运气好了吗?我正在寻找完全相同问题的答案。

标签: macos delegates webview controls boolean


【解决方案1】:

我终于想通了。

为您的 WebView 实例设置WebPolicyDelegate delegate

self.webview.policyDelegate = self;

然后在您的委托中实现– webView:decidePolicyForNavigationAction:request:frame:decisionListener: 方法:

-(void)webView:(WebView *)webView
decidePolicyForNavigationAction:(NSDictionary *)actionInformation
        request:(NSURLRequest *)request
          frame:(WebFrame *)frame
decisionListener:(id < WebPolicyDecisionListener >)listener
{
    int actionKey = [[actionInformation objectForKey: WebActionNavigationTypeKey] intValue];
    if (actionKey == WebNavigationTypeOther)
    {
        [listener use];
    }
    else
    {
        //
        // Here is where you would intercept the user navigating away 
        // from the current page, and use `[listener ignore];`
        //

        NSLog(@"\n\nuser navigating from: \n\t%@\nto:\n\t%@",
              [webView mainFrameURL],
              [[request URL] absoluteString]);

        [listener use];
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-07
    • 2011-08-05
    • 2016-07-12
    • 2021-01-14
    • 2018-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多