tomidelucca
tomidelucca

Reputation: 2543

Open html links in the app

Is it possible to handle the call of an URL Scheme inside the same app?

Here's an example: There is an html link in my app that opening would trigger safari. What I want to do is to tap on that link and that no matter where i am in the app a viewcontroller gets pushed with a webview on it.

If this is possible, where I have to do it? If I handle URL Scheme's calls i need to do it on the app's delegate. Where should i do it in this case? If the viewcontroller needs to be pushed, who is the responsible of pushing it? The appdelegate or the view controller thats currently on screen?

Thanks :)

Upvotes: 1

Views: 253

Answers (4)

Melbourne
Melbourne

Reputation: 541

Try this. Use this code in the current controller

WebViewController *objWebViewController     =   [[WebViewController alloc] initWithNibName:@"WebViewController" bundle:nil];
                objWebViewController.m_strHtml=[NSString stringWithString:@"www.google.com"];//m_strHtml is a NSString variable in WebViewController class
                [self.navigationController pushViewController:objWebViewController animated:YES];
                [objContentWebViewController release];

Use this in the WebViewController

NSURL *url = [[NSURL alloc] initWithString:m_strHtml];
        NSURLRequest * req = [[NSURLRequest alloc] initWithURL:url];
        [url release];
        [Webview loadRequest:req];
        [req release];

ELse you can load the the html inside a web view and the web view opens the links inside it
as default . Use the following code.

m_strHtml=[[NSString alloc]initWithFormat:@"<html><body>%@</body></html>",m_strHtmlContent];//m_strHtmlContent is string variable to store the html content

        [Webview loadHTMLString:m_strHtml baseURL:Nil]; 
        Webview.backgroundColor=[UIColor clearColor];

Upvotes: 0

Mike
Mike

Reputation: 132

Well its kind of annoying to involve view controller. I found it easier to use javascript to load new links/content within the page. That way one webview is involved and the data/html files get shuffled. Let me know if you need more information.

Upvotes: 0

Mitesh Khatri
Mitesh Khatri

Reputation: 3955

Please check my Question

I faced the same problem. Hope it will helps you.

Thanks

Upvotes: 1

rishi
rishi

Reputation: 11839

  1. UITapGestureRecognizer, if your link is in a label:

    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openLink:)];
    [yourLinkLabel addGestureRecognizer:gestureRecognizer];
    gestureRecognizer.numberOfTapsRequired = 1;
    gestureRecognizer.cancelsTouchesInView = NO;
    
  2. If it is a link inside a web page loaded in UIWebView, then you can use:webView:shouldStartLoadWithRequest:navigationType. It will be called every time the user taps a link in the UIWebView.

Upvotes: 2

Related Questions