Swift Open Facebook Profile by FacebookUID

前端 未结 5 1220
别跟我提以往
别跟我提以往 2020-12-20 16:29

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, howe

5条回答
  •  没有蜡笔的小新
    2020-12-20 17:08

    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:

    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.

提交回复
热议问题