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)")!
Ibrahim
source share