29

I placed a Facebook button in my iPhone app. How to open the iPhone's default browser when clicking on that button and loads the Facebook login page?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Arun
  • 3,478
  • 8
  • 33
  • 46

7 Answers7

81

iOS 10 has introduced new method to open a URL, see below:

Swift 3:

let url = URL(string:"http://www.booleanbites.com")!
if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    // Fallback on earlier versions
    UIApplication.shared.openURL(url)
}

Objective C:

NSURL *url = [NSURL URLWithString:@"http://www.booleanbites.com"];

if ([[UIApplication sharedApplication] respondsToSelector:@selector(openURL:options:completionHandler:)]) {
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL];
}else{
    // Fallback on earlier versions
    [[UIApplication sharedApplication] openURL:url];
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
Adil Soomro
  • 37,609
  • 9
  • 103
  • 153
  • 8
    Your "some search" link points to "phonegap android home button pressed". The question is about iOS application with button to open facebook homepage on default browser. I don't see a connection? – JOM Jan 06 '12 at 11:21
8
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.facebook.com"]];
Ankit Srivastava
  • 12,347
  • 11
  • 63
  • 115
4

When you open iPhone's default browser, your application gets to background - possibly terminated by iOS. Is that really what you want? User has no way to get back to your app.

Couple options to consider:

  • Integrate Facebook iOS library into your application. Lots of coding, since you need to write some UI code to support the integration. On the other hand it's your application and you have some control over what is going on and how to react to user actions

https://github.com/facebook/facebook-ios-sdk

  • Embed browser inside your application and "open the browser" within your app. Yet again you have some control over what's going on and what to do about it

http://developer.apple.com/library/ios/#documentation/uikit/reference/UIWebView_Class/

The point is: it's YOUR application. Why would you force user to shutdown your app and start using something else without any way to get back?

Well, you know all the details and based on those you want the default browser. That's ok.

JOM
  • 8,139
  • 6
  • 78
  • 111
3

In Swift

UIApplication.sharedApplication().openURL(NSURL(string:"http://www.facebook.com")!)

In objective C

[[UIApplication sharedApplication] openURL:[NSURL          
URLWithString:@"http://www.facebook.com"]];
Nitesh
  • 1,389
  • 1
  • 15
  • 24
2
Swift: iOS

var url:NSURL? = NSURL(string:webLink!)
UIApplication.sharedApplication().openURL(url!)
Vinod Joshi
  • 7,696
  • 1
  • 50
  • 51
1

Swift 3:

let url = URL(string: "http://www.example.com")!           
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

If you want to add a completionHandler:

let url = URL(string: "http://www.example.com")!           
if UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: { (success) in
        print("success!") // do what you want
    })
}
lenooh
  • 10,364
  • 5
  • 58
  • 49
1

OpenUrl is depricated, Use the following code.

NSURL *url = [NSURL URLWithString:@"https://google.com"];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
Morteza Soleimani
  • 2,652
  • 3
  • 25
  • 44