iOS Facebook URL schemes doesn't open page in facebook app

2.6k Views Asked by At

I need to open facebook page in facebook app.

This is my code

 let url : URL!
 if (UIApplication.shared.canOpenURL(URL(string:"fb://")!)) {
    //FIXME: Url is not working ✋
    let pageName = "google"
      // tried with fb://page?name=%@ not worked
    let urlString =  String.init(format: "fb://page?id=%@",pageName!) 
    url = URL.init(string: urlString)
 } else {
    url = URL.init(string: "https://www.facebook.com/google")
 }

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url!, completionHandler: nil)
} else {
    UIApplication.shared.openURL(url!)
}

it perfectly works in safari browser but not in facebook app.

I have added the url schem in inof.plist

<key>LSApplicationQueriesSchemes</key>
    <array>
        <string>fb</string>
        <string>fbapi</string>
        <string>fb-messenger-api</string>
        <string>fbauth2</string>
        <string>fbshareextension</string>
    </array>

Can anyone help me to get out this problem?

Thanks In Advance!!!!

2

There are 2 best solutions below

2
On

And is it opening in a browser when you call UIApplication.shared.openURL(:)?

I didn't exactly understand your problem, but if I'm not mistaken, you've to put the Facebook App ID (it's the first thing that appears in your app in developers.facebook) in your URL Types.

This image should clarify what I'm trying to say:

enter image description here

Do not forget the "fb" String before your App ID.


EDIT: Now that I've understood your problem, I can't be sure that you will be able to solve this - as you can see all this answers-, they apparently don't seem to work anymore, as the URL scheme changed a couple of times, and the one that you're using is actually the last "answered/used" on the referenced answer. I can almost confirm that there is no supported way to do what you're trying because of this answer in fb-sdk github issues.

0
On

try this code , make sure use facebook id from page not page name

check this How to find page Id

 let url : URL!
        if (UIApplication.shared.canOpenURL(URL(string:"fb://")!)) {
            //FIXME: Url is not working ✋
            let pageName = "104958162837"
            // tried with fb://page?name=%@ not worked
            let urlString =  String.init(format: "fb://profile/%@",pageName) 
            url = URL.init(string: urlString)
        } else {
            url = URL.init(string: "https://www.facebook.com/104958162837")
        }

        if #available(iOS 10.0, *) {
            UIApplication.shared.open(url!, completionHandler: nil)
        } else {
            UIApplication.shared.openURL(url!)
        }