【问题标题】:UIWebView, tying an action to a href linkUIWebView,将操作绑定到 href 链接
【发布时间】:2012-02-08 17:51:32
【问题描述】:

我们有一个“关于我们的应用程序”弹出框,它是 html 格式的,基本上是弹出框中的 UIWebView。还有一个指向我们网站的 href 链接。问题是如果您单击链接,它只会在弹出窗口中打开我们的网站。有没有办法改变该链接发生的情况,比如打开 Safari 使其在完整的浏览器中,或者至少增加弹出框的大小,因为我们的关于我们的应用程序窗口很小。

【问题讨论】:

    标签: iphone uiwebview


    【解决方案1】:

    是的,this post 为在 Safari 中打开的每个 http、https 和 mailto 调用提供以下代码:

    -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request    navigationType:(UIWebViewNavigationType)navigationType; 
    {
        NSURL *requestURL =[ [ request URL ] retain ]; 
        if ( ( [ [ requestURL scheme ] isEqualToString: @"http" ] || [ [ requestURL scheme ] isEqualToString: @"https" ] || [ [ requestURL scheme ] isEqualToString: @"mailto" ]) 
                 && ( navigationType == UIWebViewNavigationTypeLinkClicked ) ) { 
           return ![ [ UIApplication sharedApplication ] openURL: [ requestURL autorelease ] ]; 
        } 
        [ requestURL release ]; 
        return YES; 
    }
    

    您只能通过从请求中获取 URL 来修改某些 URL。

    【讨论】:

      【解决方案2】:

      我使用此代码通过 UIWebView 打开 URL:

      - (BOOL)webView:(UIWebView *)webView_ shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
      {
          self.curURL = request.URL;
      
          if ([request.URL.scheme isEqualToString:@"file"])
          {
              return YES;
          }
      
          UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil
                                                                    delegate:self
                                                           cancelButtonTitle:nil
                                                      destructiveButtonTitle:@"Cancel"
                                                           otherButtonTitles:@"Open in Safari", nil];
          [actionSheet showInView:self.view];
          [actionSheet release];
      
          return NO;
      }
      
      
      #pragma mark -
      #pragma mark Action sheet delegate methods
      
      
      - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
      {
          if(buttonIndex == actionSheet.firstOtherButtonIndex + eSafariButtonIndex)
          {
              [[UIApplication sharedApplication] openURL:self.curURL];    
          }
      }
      

      【讨论】:

        【解决方案3】:

        你测试过 UIWebView 委托的- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 方法吗?

        webview 应该在每次加载另一个框架/页面时询问它的委托,所以如果你点击链接,方法就会被调用。

        然后您只需返回NO 并按照您想要的方式处理请求!

        【讨论】:

          猜你喜欢
          • 2020-02-16
          • 2013-01-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-02-28
          • 2020-11-23
          • 1970-01-01
          相关资源
          最近更新 更多