【问题标题】:How can I open a View Controller when the UIWebView (by means of Java) recognized a click on an Image?当 UIWebView(通过 Java)识别出点击图像时,如何打开视图控制器?
【发布时间】:2011-06-23 15:19:39
【问题描述】:

我有一个 HTML 内容如下:

<html>
<head>
    <meta name=viewport content=width=320/>
    <script>
        function imageClicked(){
            var clicked=true;
            window.location="/click/"+clicked;
        }
        </script>
</head>
<body>
    <center><h1>This is Page One</h1></center>
    <center><a href="javascript:void(0)" onMouseDown="imageClicked()"><IMG SRC="divers-circle.jpg">
        </a></center>
       </center><br><br>
    <center>Scuba divers</center>
</body>
</html>

HTML 已正确加载到 UIWebView,因为它是本地文件:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:((WebPageObj *)obj).webPath ofType:@"html"]isDirectory:NO]]];


    webView.dataDetectorTypes = UIDataDetectorTypeAll;

现在,当我单击图像时,它会识别单击,但我如何在 Xcode 中实现操作,例如,打开另一个视图或警报?

顺便说一句,我是根据解释一些概念的教程做的,这里是链接: http://www.codeproject.com/KB/iPhone/iPhoneDevReader.aspx

但是,它在 Xcode 中创建了 HTML,在我的例子中,我加载了 HTML,因此,我需要以某种方式读取 HTML 中的脚本以包含以下内容:

if ( [request.mainDocumentURL.relativePath isEqualToString:@"/click/false"] ) {    
        NSLog( @"not clicked" );
        return false;
    }

    if ( [request.mainDocumentURL.relativePath isEqualToString:@"/click/true"] ) {        //the image is clicked, variable click is true
        NSLog( @"image clicked" );

        UIAlertView* alert=[[UIAlertView alloc]initWithTitle:@"JavaScript called" 
               message:@"You've called iPhone provided control from javascript!!" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
        [alert show];
        [alert release];
        return false;
    }

    return true;
}

什么方法可以帮助我做到这一点?以及我应该把它放在哪里,因为我使用的是 ScrollView,然后在调用页面时加载 WebView。

提前致谢。

【问题讨论】:

    标签: java html xcode uiwebview html-parsing


    【解决方案1】:

    你需要为你的 UIWebView 设置一个委托。

    如果你为你的 UIWebView 设置了一个委托,那么每次你的 web 视图发送请求时都会调用以下方法:

    - (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
        NSURL *url = [request URL];
        if (navigationType == UIWebViewNavigationTypeLinkClicked) {
                if ([[url scheme] isEqualToString:@"click"]) {
                   // do whatever you want here to happen after the click
                }
                return NO;
        } else {
           return YES;
        }
    }
    

    为了尽可能简单地将您的 html 更改为以下内容(无需 Javascript):

      <html>
        <head>
            <meta name=viewport content=width=320/>
        </head>
        <body>
            <center><h1>This is Page One</h1></center>
            <center><a href="click://"><IMG SRC="divers-circle.jpg"></a></center>
            <br><br>
            <center>Scuba divers</center>
        </body>
        </html>
    

    点击图片时,会发生以下情况:

    1. WebView 使用 URL“click://”发送请求。
    2. 正在调用 UIWebView 委托的方法 webView:shouldStartLoadWithRequest:navigationType:
    3. 此方法中的 2 个“if”子句评估为“YES”,因为请求发生在单击 (navigationType == UIWebViewNavigationTypeLinkClicked) 之后,并且 URL 的方案是“单击”

    您可以找到有关 UIWebViewDelegate 协议here 的更多信息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-30
      • 2019-10-04
      相关资源
      最近更新 更多