9

I'm trying to open a user's Facebook profile in Swift using their Facebook UID. In the Simulator, it works and opens up the web version of the profile and works fine, however on my actual device, it opens up the Facebook native app and gives me a "Page not Found" error.

guard let facebookUID = self.user?.facebookUID else {
    return
}

print(facebookUID)
let fbURLWeb: NSURL = NSURL(string: "https://www.facebook.com/\(facebookUID)")!
let fbURLID: NSURL = NSURL(string: "fb://profile/\(facebookUID)")!

if(UIApplication.sharedApplication().canOpenURL(fbURLID)){
    // FB installed
    UIApplication.sharedApplication().openURL(fbURLID)
} else {
    // FB is not installed, open in safari
    UIApplication.sharedApplication().openURL(fbURLWeb)
}

enter image description here

What am I doing wrong?

Thanks in advance!

EDIT

I've noticed that if a user deletes or doesn't have the Facebook app installed on their phone, it will open up the profile correctly through Safari, which is why it worked on the simulator, but didn't on my device. However, I've also noticed I can get it to work in the Facebook app if I try a public profile like fb://profile/4 (which is Mark Zuckerburg's profile).

enter image description here

Thomas
  • 2,356
  • 7
  • 23
  • 59
  • hmm weird, I tested your code and it's working fine on my device – Penkey Suresh May 13 '16 at 06:51
  • is your facebook profile that you're visiting private? I'm thinking that because it also requires me to login on the web version to view the persons profile, it might be an issue with the token – Thomas May 17 '16 at 23:19
  • @PenkeySuresh still haven't figured this one out. Did you have the facebook app installed on your phone? I was able to figure out why it works in the simulator and not on my actual device. It seems that when the Facebook App is installed, it will give that error. If it's not installed, it will just open up the user's profile in Safari – Thomas Jul 12 '16 at 23:53
  • I tried on iPhone 5s real device. Have facebook installed on the device. Tried with profiles with/without mutual friends – Penkey Suresh Jul 13 '16 at 08:33
  • Any chance you could push a sample project to GitHub so I could check it out? This doesn't work on my device or any of my friends devices I've tried it on @PenkeySuresh – Thomas Jul 13 '16 at 23:46

5 Answers5

9

Hi based on documentation Facebook ID is the App-scoped User ID so you can use like

fb://profile?app_scoped_user_id=%@

But also check bug report on https://developers.facebook.com/bugs/332195860270199 opening the Facebook profile with

fb://profile?app_scoped_user_id=%@

is not supported.

So you will have to open the Facebook profile within Safari or you can use in app webview to open user profile. I have check with some facebook id 10000****889,10000***041 etc. all have open profile in facebook app.Yes some have same error.Page bot found.

May be there is Privacy setting->Do you want search engine outside link to your profile->No. restrict search.

To get ids of user from facebook Trick :

1)Right click on the person or page's profile photo, select properties and the ID will be included in the page

2)In mobile browswe if you open photo there is id E.g. " fbid = *** & pid = 1000....89 " so 1000....89 will be your user id

Bhoomi Jagani
  • 2,413
  • 18
  • 24
  • Thanks. Getting the image id (from trick 1) and using that instead of the name I thought I had to use worked for me. – Mark Leonard Jan 08 '17 at 20:38
6

Here is another solution that works for iOS 10 & Swift 3.

First of all, you should add LSApplicationQueriesSchemes key to Info.plist file. If you do not add this key, canOpenURL will always return false. Than you need to add what scheme(s) you want to check. Use fb for facebook.

It should looks like this:

enter image description here

The rest is Swift:

if UIApplication.shared.canOpenURL(URL(string: "fb://profile/PROFILE_ID")!) {
    UIApplication.shared.open(URL(string: "fb://profile/PROFILE_ID")!, options: [:])
} else {
    UIApplication.shared.open(URL(string: "https://facebook.com/PROFILE_ID")!, options: [:])
}

Note: Replace PROFILE_ID with your target.

Ogulcan Orhan
  • 5,170
  • 6
  • 33
  • 49
3

Thanks to vien vu. From iOS 9 < above, You must whitelist the url's that your app will call out to using the LSApplicationQueriesSchemes key in your Info.plist. So add this code to your plist file:

 <key>LSApplicationQueriesSchemes</key>
<array>
<string>fb</string>
<string>fbapi</string>
<string>fbauth2</string>
<string>fbshareextension</string>
<string>fb-messenger-api</string>
<string>twitter</string>
<string>whatsapp</string>
<string>wechat</string>
<string>line</string>
<string>instagram</string>
<string>kakaotalk</string>
<string>mqq</string>
<string>vk</string>
<string>comgooglemaps</string>
</array>
Baljeet Singh
  • 453
  • 5
  • 15
3

I able to use this URL scheme fb://profile?id={userid/username/pageid} to open a facebook user's profile / page as of 30 Aug 2020.

eg: fb://profile?id=4 OR fb://profile?id=zuck

Warning: Please use at your own risk as this is not documented in Facebook developers documentation. This URL scheme may be removed by Facebook app in the future without any warning and indication.

luke77
  • 2,255
  • 2
  • 18
  • 30
1

Swift 2.3

func goFacebook() {
    let profileID = "" //Here put profile id Example:'62260589592'
    let facebookURL = NSURL(string: "fb://profile/\(profileID)")!

    if UIApplication.sharedApplication().canOpenURL(facebookURL) {
        UIApplication.sharedApplication().openURL(facebookURL)
    } else {
        UIApplication.sharedApplication().openURL(NSURL(string: "https://www.facebook.com/PageName")!)
    }
}

Note: If you can't find the facebook profile id can you use the follow site to do it (https://findmyfbid.com/)

Set in the info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
 <string>fb</string>
 <string>fbapi</string>
 <string>fbauth2</string>
 <string>fbshareextension</string>
 <string>fb-messenger-api</string>
</array>
Cristian Mora
  • 1,813
  • 2
  • 19
  • 27