How to open fb and instagram application by clicking on a button in Swift

How to open facebook and instagram app by clicking on a button in Swift? In some applications, by clicking on something, it redirects to the facebook application and opens some page. How can I do the same in Swift?

I found him,

var url = NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4") if UIApplication.sharedApplication().canOpenURL(url!) { UIApplication.sharedApplication().openURL(url!) } 

but here I have to know the url of the application. And other examples were in ObjC, which I don’t know at all = /

+7
ios facebook swift instagram
source share
5 answers

Take a look at these links, this may help you:

https://instagram.com/developer/mobile-sharing/iphone-hooks/

http://wiki.akosma.com/IPhone_URL_Schemes

Open facebook link using Facebook’s native iOS app

Otherwise, there is a quick Instagram example to open a specific profile (alias: johndoe) here:

 var instagramHooks = "instagram://user?username=johndoe" var instagramUrl = NSURL(string: instagramHooks) if UIApplication.sharedApplication().canOpenURL(instagramUrl!) { UIApplication.sharedApplication().openURL(instagramUrl!) } else { //redirect to safari because the user doesn't have Instagram UIApplication.sharedApplication().openURL(NSURL(string: "http://instagram.com/")!) } 
+16
source share

Update for Swift 3 and iOS 10+

OK, there are two simple steps in Swift 3:

First you need to change Info.plist to instagram list and facebook to LSApplicationQueriesSchemes . Just open Info.plist as source code and paste this:

 <key>LSApplicationQueriesSchemes</key> <array> <string>instagram</string> <string>fb</string> </array> 

After that, you can open instagram and facebook applications using instagram:// and fb:// . Here is the full code for instagram, and you can do the same for facebook, you can associate this code with any button that you use as an action:

 @IBAction func InstagramAction() { let Username = "instagram" // Your Instagram Username here let appURL = NSURL(string: "instagram://user?username=\(Username)")! let webURL = NSURL(string: "https://instagram.com/\(Username)")! let application = UIApplication.shared if application.canOpenURL(appURL as URL) { application.open(appURL as URL) } else { // if Instagram app is not installed, open URL inside Safari application.open(webURL as URL) } } 

For facebook you can use this code:

 let appURL = NSURL(string: "fb://profile/\(Username)")! 
+9
source share

In fast 3;

You must add this to your Info.plist first.

enter image description here

How can you use this code;

  let instagramUrl = URL(string: "instagram://app") UIApplication.shared.canOpenURL(instagramUrl!) UIApplication.shared.open(instagramUrl!, options: [:], completionHandler: nil) 
+8
source share

Based on accepted answer, here is a way to make it more elegant with Swift 4

 UIApplication.tryURL([ "instagram://user?username=johndoe", // App "https://www.instagram.com/johndoe/" // Website if app fails ]) 

And really remember to add a schema so that the application opens. However, even if you forget that instagram will open in Safari.

+1
source share

In fast 4:

Just change appURL and webURL :

 twitter://user?screen_name=\(screenName) instagram://user?screen_name=\(screenName) facebook://user?screen_name=\(screenName) 
  • 'openURL' deprecated in iOS 10.0:
 let screenName = "imrankst1221" let appURL = NSURL(string: "instagram://user?screen_name=\(screenName)")! let webURL = NSURL(string: "https://twitter.com/\(screenName)")! if UIApplication.shared.canOpenURL(appURL as URL) { if #available(iOS 10.0, *) { UIApplication.shared.open(appURL as URL, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(appURL as URL) } } else { //redirect to safari because the user doesn't have Instagram if #available(iOS 10.0, *) { UIApplication.shared.open(webURL as URL, options: [:], completionHandler: nil) } else { UIApplication.shared.openURL(webURL as URL) } } 
0
source share

All Articles