开发者

How can I open a View Controller when the UIWebView (by means of Java) recognized a click on an Image?

I have an HTML that says the following:

<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>
    <c开发者_JAVA技巧enter>Scuba divers</center>
</body>
</html>

The HTML is properly loaded to a UIWebView, since it is a local file:

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


    webView.dataDetectorTypes = UIDataDetectorTypeAll;

Now, when I click the image, it recognized the click, but how can I implement an action within Xcode, for example, open another view or an alert?

BTW, I did this based on a tutorial that explains some concept, here is the link: http://www.codeproject.com/KB/iPhone/iPhoneDevReader.aspx

However, it created the HTML within Xcode, and in my case, I loaded the HTML, therefore, I need to somehow read the script within the HTML to include the following:

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;
}

What method will help me to do that? and where Shall I put it since I am using a ScrollView, and then loading the WebView when the page is called.

Thanks in advance.


You need to set a delegate for your UIWebView.

If you set a delegate for your UIWebView the following method will be called everytime your web view sends a request:

- (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;
    }
}

To make it as easy as possible change your html to the following (no Javascript needed):

  <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>

When the image is clicked, the following is happening:

  1. The WebView sends a request with the URL "click://".
  2. The UIWebView delegate's method webView:shouldStartLoadWithRequest:navigationType: is being called
  3. the 2 "if" clauses in this method evaluate to "YES" because the request happened after a click (navigationType == UIWebViewNavigationTypeLinkClicked) and the URL's scheme is "click"

You find more about the UIWebViewDelegate protocol here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜