22

I want to open an url in safari, outisde the app and not in webview.

I implemented the UIWebViewDelegate but I am still not able to open the url. Basically I am not able to click the url.

Below is the code:

-(void)newView:(NSString *)title Description:(NSString *)desc URL:(NSString *)url{
    webView =[[UIWebView alloc]initWithFrame:CGRectMake(15, 17, 190, 190)];
    webView.backgroundColor=[UIColor clearColor];
    webView.delegate=self;
    webView.opaque = NO;
    [webView loadHTMLString:[NSString stringWithFormat:@"<html><body p style='color:white' text=\"#FFFFFF\" face=\"Bookman Old Style, Book Antiqua, Garamond\" size=\"5\">%@ %@</body></html>", desc,url] baseURL:nil];

    v = [[HUDView alloc] initWithFrame:CGRectMake(60, 70, 220, 220)];

    cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    cancelButton.frame = CGRectMake(0, 0, 30, 30);
    [cancelButton setBackgroundImage:[UIImage imageNamed:@"closebox.png"] forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButtonPressed) forControlEvents:UIControlEventTouchUpInside];
    [v addSubview:cancelButton];
    [v addSubview:webView];
    [self.view addSubview:v];  
}

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
    if ( inType == UIWebViewNavigationTypeLinkClicked ) {
        [[UIApplication sharedApplication] openURL:[inRequest URL]];
        return NO;
    }

    return YES;
}
Pierre Prinetti
  • 9,092
  • 6
  • 33
  • 49
user578386
  • 1,061
  • 3
  • 14
  • 37
  • I think this link should help you. http://stackoverflow.com/questions/822599/launch-safari-from-iphone-app – tikhop Jul 17 '12 at 14:36

10 Answers10

44

This answer was readily available via Google:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.apple.com"]];

Just put that in your button press or whatever event you're wanting to call it on, and then pass it a URL (replace the @"http:/www.apple.com").

Jtaylorapps
  • 5,680
  • 8
  • 40
  • 56
  • the thing is when i click the url in the webview it sould launch..i m able to get the url in webview but not able to click the link in webview – user578386 Jul 17 '12 at 14:38
  • See, you can't simply open any URL you click in a WebView in Safari. Your wording is pretty bad in the original answer, so I had no clue what you needed. You'll have to find out how to add an event listener for specific URLs in the WebView, similar to how Safari does it when it brings an Options Dialog when you Long Press a URL in the WebView. Maybe look up the online Docs – Jtaylorapps Jul 17 '12 at 14:42
  • Note that the string you pass this function needs to be correctly URL encoded, with spaces converted to %20 etc. – Duncan Babbage Aug 06 '14 at 21:30
43

After reading the comments I think this is what you're looking for:

Implement this method:

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

from UIWebViewDelegate and depending on that request argument you should return TRUE or FALSE. If you don't want the web view to open it, you should call:

[[UIApplication sharedApplication] openURL:request.URL];

as others mentioned and return FALSE.

Hope this helps. Cheers!

EDIT: If the links are not recognized in your web view, try this:

[webView setDataDetectorTypes:UIDataDetectorTypeLink]
Tamás Sengel
  • 55,884
  • 29
  • 169
  • 223
George
  • 4,029
  • 1
  • 23
  • 31
  • Nice, I couldn't figure out what he wanted, but this looks like what he was looking for – Jtaylorapps Jul 17 '12 at 15:14
  • 1
    @George:thanks that totally worked...i was able to navigate to safari outside my app to open. is it anyway possible to change the color of the link for navy blue – user578386 Jul 17 '12 at 15:22
  • Yes , it is possible , but you have to change that from the html. Glad to help. Cheers! – George Jul 17 '12 at 15:58
  • updated call for people in 2020 : ```[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.google.com"] options:@{} completionHandler:nil];``` – Hibbem May 26 '20 at 19:20
8
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
Obaid Maroof
  • 1,523
  • 2
  • 19
  • 40
  • the thing is when i click the url in the webview it sould launch..i m able to get the url in webview but not able to click the link in webview – user578386 Jul 17 '12 at 14:39
6

For iOS 10+

// Objective-C
UIApplication *application = [UIApplication sharedApplication];
[application openURL:URL options:@{} completionHandler:nil];

// Swift
UIApplication.shared.open(url, options: [:], completionHandler: nil)

For more info refer THIS.

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
3

This works for me:

[[NSWorkspace sharedWorkspace] openURL: [NSURL URLWithString: @"http://www.apple.com"]];
Bijan
  • 25,559
  • 8
  • 79
  • 71
2
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{

    if (![[NSString stringWithFormat:@"%@",[request URL]] containsString:@"file"] ) {
        [[UIApplication sharedApplication] openURL:[request URL]];
        return NO;
    }
    return YES;
}

I used an html file in local. In this html there is some links. If you set delegate UIWebViewDelegate and use this local html will open in your webView and the other links will open in safari
I wrote "file" because this link is "file:///Users/~/x.app/about.html" in local.

kordiseps
  • 391
  • 3
  • 12
1

******************** Swift **********************

//MARK: Button Click on open with SafariViewController

private var urlString:String = "https://google.com"

@IBAction func openWithSafariVC(sender: AnyObject)
{

    let svc = SFSafariViewController(URL: NSURL(string: self.urlString)!)
    svc.delegate = self
    self.presentViewController(svc, animated: true, completion: nil)
}

//MARK: SafatriViewConroller Dismiss

func safariViewControllerDidFinish(controller: SFSafariViewController)
{

    controller.dismissViewControllerAnimated(true, completion: nil)
}
Hiren
  • 12,720
  • 7
  • 52
  • 72
Bhavin Chauhan
  • 611
  • 4
  • 17
1

I found this answer in google but I need to after opening the browser terminate my application and continue the browser.

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://google.com"] options: @{} completionHandler: nil];

in Android can do It easy to calling finish() method how can I do that

I don't know
  • 701
  • 2
  • 8
  • 15
0

Swift and no webview way

    if let url = NSURL(string: "http://www.apple.com") {
        UIApplication.sharedApplication().openURL(url)
    }
dirtsniffer
  • 139
  • 1
  • 6
0

Swift 3 (iOS 10+):

if let url = URL(string: "http://www.seligmanventures.com") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Bentaye
  • 9,403
  • 5
  • 32
  • 45
Charlie S
  • 4,366
  • 6
  • 59
  • 97