2

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!!!!

VishalPethani
  • 854
  • 1
  • 9
  • 17

2 Answers2

0

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!)
        }
a.masri
  • 2,439
  • 1
  • 14
  • 32
0

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.

Guilherme Matuella
  • 2,193
  • 1
  • 17
  • 32
  • 1
    I am checking that if fb app is available in device, open page in fb app. if not, open page in, UIWebView..... but the problem is that It's not open a page in facebook app. I already added the "fb"MyID in URL Types. – VishalPethani Oct 08 '18 at 05:49
  • Oh, now I've fully understood. I will update my answer, your problem is probably that you're trying to open in an URL on the fb app, and that's not the way you do it. – Guilherme Matuella Oct 08 '18 at 13:05