iosswiftfacebook-graph-apifacebook-ios-sdkfacebook-sdk-4.0

How to launch iOS Native Facebook App from my app with a public profile URL?


I am developing an iOS app that needs to open a Facebook user or page profile on the Facebook App (if installed) from my app. I have the public profile URL of the user or page, such as https://www.facebook.com/nirav.parmar.7169709. How can I use a custom URL scheme to launch the Facebook App and show the profile? If the Facebook App is not installed, I want to open the profile in Safari instead.

I already tried this way but it's not work for me:


Solution

  • You can use canOpenURL to check if app can be opened.

    let appURL = URL(string: "fb://profile/100004929307564")!
    let browserURL = URL(string: "https://www.facebook.com/nirav.parmar.7169709")!
    if UIApplication.shared.canOpenURL(appURL) {
        UIApplication.shared.open(appURL)
    } else {
        UIApplication.shared.open(browserURL)
    }