寻找用于将iOS应用程序移植到OSX的UIWebViewDelegate的等效项

时间:2011-02-12 17:50:30

标签: cocoa macos ios uiwebview webkit

我正在尝试将iOS应用程序移植到OSX,有一件事我没有得到。 iOS应用程序使用UIWebView,更精确的是实现UIWebViewDelegate的UIView:

@interface Dialog : UIView <UIWebViewDelegate> {

并实现这三个委托方法:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request

  navigationType:(UIWebViewNavigationType)navigationType {

- (void)webViewDidFinishLoad:(UIWebView *)webView {

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {

有人能给我一个提示,告诉我如何将其移植到普通的OSX框架中吗?我知道有WebView但它有4个代表,据我所知,并且这些代理都没有委托方法听起来像那些。

谢谢

3 个答案:

答案 0 :(得分:7)

对于第一个,您可能想要使用WebPolicyDelegate。而对于其他两个,WebFrameLoadDelegate中有相应的方法:

- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
- (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame

答案 1 :(得分:7)

关于iOS UIWebView webView:shouldStartLoadWithRequest:navigationType:,要使用OSX WebView完成此操作:

为您的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];
    }

}

答案 2 :(得分:1)

- (void)webView:(WebView *)webView decisionPolicyForNavigationAction:(NSDictionary *)actionInformation        请求:(NSURLRequest *)请求          frame:(WebFrame *)框架 decisionListener:(id&lt; WebPolicyDecisionListener&gt;)侦听器

我正在使用它而不是shouldStartLoadWithRequest,这很好用