【问题标题】:Looking for an equivalent for UIWebViewDelegate for porting an iOS app to OSX寻找 UIWebViewDelegate 的等效项以将 iOS 应用程序移植到 OSX
【发布时间】:2011-02-12 17:50:30
【问题描述】:

我正在尝试将 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 个。

谢谢

【问题讨论】:

    标签: cocoa macos ios uiwebview webkit


    【解决方案1】:

    对于第一个,您可能希望使用WebPolicyDelegate。而另外两个在WebFrameLoadDelegate中有对应的方法:

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

    【讨论】:

      【解决方案2】:

      关于 iOS UIWebViewwebView: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];
          }
      
      }
      

      【讨论】:

        【解决方案3】:

        -(void)webView:(WebView *)webView 决定PolicyForNavigationAction:(NSDictionary *)actionInformation 请求:(NSURLRequest *)请求 框架:(WebFrame *)框架 决策监听器:(id )监听器

        我正在使用它而不是 shouldStartLoadWithRequest,它运行良好

        【讨论】:

          猜你喜欢
          • 2012-05-01
          • 1970-01-01
          • 2014-05-28
          • 2014-10-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-15
          相关资源
          最近更新 更多