How do I get a button to link to a facebook account using the facebook application and if it isn't installed then use Safari? I have used the code in the following and I believe it should work but it only uses Safari.

How do I get a button to link to a facebook account using the facebook application and if it isn't installed then use Safari? I have used the code in the following and I believe it should work but it only uses Safari.


@IBAction func btnFacebook(_ sender: UIButton) {
        
        let facebook_URL_ID: NSURL? = NSURL(string: "fb://profile/##########")
        
        let facebook_URL_Web: NSURL? = NSURL(string: "https://www.facebook.com/##########")
        
        if UIApplication.shared.canOpenURL(facebook_URL_ID! as URL) {
            UIApplication.shared.open(facebook_URL_ID! as URL, completionHandler: nil)
        
        }
        else {
            UIApplication.shared.open(facebook_URL_Web! as URL)
       }
        
    }

Have you put enough info into your app's Info.plist ?


func canOpenURL(URL) -> Bool (See the part Important in the Discussion.)


--

One more, you should better use `URL` instead of `NSURL` in your Swift code.

How do I get a button to link to a facebook account using the facebook application and if it isn't installed then use Safari? I have used the code in the following and I believe it should work but it only uses Safari.
 
 
Q