2

Facebook app wont open up, facebook in safari opens instead. Using xCode 10.2.1

Deployment target running iOS 12.2

Edit: this code does achieve the desired objective for Instagram

Edit2: So on @bbrodsky's recommendation i changed "facebook" to "fb" in the URL link. The problem now is that it opens to my personal feed and not the business page i want it to open to.

Edit3: So ive gotten it to work. Opens up the facebook app, and goes to desired page. Had to change the appURL to "fb://profile/NUMERICFBID". I was able to get the numeric facebook ID for the page at this site https://findmyfbid.com/

Tried solutions from this post and others. Cant seem to find any recent answers to this question, as everything seems to be from earlier versions of iOS and swift/xCode. Unsure if things have changed. Have added the recommended code to the plist source file with no results.

How to open fb and instagram app by tapping on button in Swift

@IBAction func facebookButtonTapped(_ sender: UIButton) {
   let screenName =  "MYSCREENNAME"
   let appURL = NSURL(string: "facebook://user?screen_name=\ 
     (screenName)")!
   let webURL = NSURL(string: "https://facebook.com/\ 
     (screenName)")!

   if UIApplication.shared.canOpenURL(appURL as URL) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(appURL as URL, options: 
            [:], completionHandler: nil)
            } 
            else { UIApplication.shared.openURL(appURL as URL)
            }
        } 
   else {
            //redirect to safari because the user doesn't have 
              Instagram
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(webURL as URL, options: 
                [:], completionHandler: nil)
            } 
            else {UIApplication.shared.openURL(webURL as URL)
            }
        }

expected result is that the facebook app opens to the particular page. keep getting this error code.

2019-06-08 11:28:39.561051-0500 SocialMediaLinkPractice[9888:2173963] -canOpenURL: failed for URL: "facebook://user?screen_name=MYSCREENNAME" - error: "This app is not allowed to query for scheme facebook"

futureappguru
  • 21
  • 1
  • 3
  • Why do you create the URLs as `NSURL` and then cast them to `URL` everywhere you use them? Why not just create them as `URL` in the first place? – Fogmeister Jun 08 '19 at 17:24
  • 1
    Im just a beginner and working on a simple project and just copied the code. I dont even know what the difference between "NSURL" and "URL" is to be completely honest. Appreciate the input though. Ill mess around with changing it. @Fogmeister – futureappguru Jun 08 '19 at 17:30
  • No worries. Was just curious. For most cases when you see something with the `NS` prefix it is a legacy type from Objective-C. A lot of them now have their Swift equivalents that drop the `NS`. In your case just drop the NS and then you can remove the “as URL” bits. – Fogmeister Jun 08 '19 at 17:35
  • Ahh i see. Yeah i had looked up the NS prefix before but didnt quite find anything that explained it like that. Thanks for the info. @Fogmeister – futureappguru Jun 08 '19 at 17:40

3 Answers3

1

Add in Info.plist

First, you have to modify Info.plist to list instagram and facebook with LSApplicationQueriesSchemes. Simply open Info.plist as a Source Code, and paste this:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>instagram</string>
    <string>fb</string>
</array>

When you want to open the Facebook App and direct to a Facebook-Page, use the Page-ID. Here is a Link, where you could find them: https://www.facebook.com/help/1503421039731588

Schemes

fb://profile – Open Facebook app to the user’s profile OR pages

fb://friends – Open Facebook app to the friends list

fb://notifications – Open Facebook app to the notifications list (NOTE: there appears to be a bug with this URL. The Notifications page opens. However, it’s not possible to navigate to anywhere else in the Facebook app)

fb://feed – Open Facebook app to the News Feed

fb://events – Open Facebook app to the Events page

fb://requests – Open Facebook app to the Requests list

fb://notes – Open Facebook app to the Notes page

fb://albums – Open Facebook app to Photo Albums list Source: https://stackoverflow.com/a/10416399/8642838

SwiftUI-Code Version

    Button(action: {
        let url = URL(string: "fb://profile/<PAGE_ID>")!
        let application = UIApplication.shared
        // Check if the facebook App is installed
        if application.canOpenURL(url) {
            application.open(url)
        } else {
            // If Facebook App is not installed, open Safari with Facebook Link
            application.open(URL(string: "https://de-de.facebook.com/apple")!)
        }
    }, label: {
        Text("Facebook")
    })
Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
1

if you want to open a specific profile on Facebook app, you have to use

let username = "your Facebook name"
let appURL = URL(string: "fb://profile/\(username)/?id=\(username)")!

otherwise it will not work.

0

I believe you want to use "fb://" and not "facebook://"

bbrodsky
  • 762
  • 1
  • 5
  • 18
  • Awesome. that worked to open the app. But now its just opening to my feed and not the page i want it to open to. @bbrodsky – futureappguru Jun 08 '19 at 19:29
  • 2
    That worked. Had to find the facebook pages numeric ID which i did using this site. https://findmyfbid.com/ Really appreciate the help. Only small bug i have now is that if the facebook app was already on the intended page, clicking the link from my app just opens up the facebook app to a blank page with a search bar. Not too concerned about it really, but figured id write it here incase someone comes along with an easy fix. – futureappguru Jun 08 '19 at 20:07
  • @futureappguru glad i could help! – bbrodsky Jun 09 '19 at 15:33