Genadinik
Genadinik

Reputation: 18639

ios web view - after a click, how to open a page in a web browser instead of embedded in an app

I have a UIWebView screen and in it I have a link which I want to go to a different web page.

Right now I have this:

if ([request.URL.scheme isEqualToString:@"something"])
{
    NSURL *url = [NSURL URLWithString:@"stackoverflow.com];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [theWebView loadRequest:request];
}

But it just opens the link within the app itself. But how do I make it load the web browser and open the url inside the web browser?

And similarly, if I want to link to my other apps inside the app store, what is the correct way to do that?

Thanks!

Upvotes: 1

Views: 3968

Answers (1)

apascual
apascual

Reputation: 2980

First you have to implement UIWebViewDelegate in the header file of your Controller, then implement shouldStartLoadWithRequest to capture the NSURLrequest and launch it in the system browser:

- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {

    NSString *url = request.URL.absoluteString;
    NSLog(@"%@",url);

    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
    return NO;
}

EDIT (for your code):

if ([request.URL.scheme isEqualToString:@"something"])
{
    NSURL *url = [NSURL URLWithString:@"stackoverflow.com];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}

Upvotes: 2

Related Questions