Search code examples
iosfacebookswiftinstagram

How to open fb and instagram app by tapping on button in Swift


How can I open Facebook and Instagram app by tapping on a button in swift? Some apps redirect to the Facebook app and open a specific page. How can I do the same thing?

I found it:

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 I have to know the app URL. Other examples were in ObjectiveC, which I don't know =/


Solution

  • Take a look at these links, it can help you:

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

    http://wiki.akosma.com/IPhone_URL_Schemes

    Open a facebook link by native Facebook app on iOS

    Otherwise, there is a quick example with Instagram for opening a specific profile (nickname: 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/")!)
    }