The goal is simple, I want to allow a user to connect their Facebook page to the iOS app i've been developing so that when they press a button in my app, it will navigate them to the appropriate Facebook page. If the user has the Facebook app installed, it will launch and use the Facebook app and if not, it will use Safari.
I was able to implement this exact functionality for another app with the following code:
if let facebookPageId = facebookPageIdTextField.text {
let facebookHook = "fb://profile/" + facebookPageId
if let url = URL(string: "\(facebookHook)") {
if UIApplication.shared.canOpenURL(url) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.open(URL(string: "https://www.facebook.com/placeholder")!)
}
}
}
However, I had to browse the Facebook HTML source code in order to find the value to use for facebookPageId and asking a user to dive into the source code seems pretty impractical.
Does Facebook not want developers to have access to this kind of functionality? Has anyone found an easy fix for this?
Related
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"
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")
})
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.
I believe you want to use "fb://" and not "facebook://"
Im using FBSDKShareKit (4.22.1), i'm trying to share photo with the following code:
let photo = FBSDKSharePhoto()
photo1.imageURL = URL(string: "sample url")
photo.isUserGenerated = false
let photoContent = FBSDKSharePhotoContent()
photoContent.photos = [photo]
FBSDKShareDialog.show(from: self, with: photoContent, delegate: nil)
but it only works when the native Facebook app is installed on the phone.
I also tried to show the content with Share Button instead of Share Dialog like:
var shareButton = FBSDKShareButton()
shareButton.shareContent = photoContent
shareButton.center = self.view.center
self.view.addSubview(shareButton)
it didn't work, if app was not installed either.
i want the FBSDK i used in my app, opens Safari when Facebook app is not installed, it opens safari by default for FBSDKShareLinkContent, but not working for FBSDKSharePhotoContent.
does anyone know what is the problem?
I have implemented another way to share photos using Graph API. It was very helpful. Here is a link.
Just what you need is to take the permissions from the user to publish_actions
before posting.
Also there is no native facebook app installed. Just got logged in through Browser.
Well, I found the answer in Facebook developer website, which is:
Photos
People can share photos from your app to Facebook with the Share
Dialog or with a custom interface:
Photos must be less than 12MB in size
People need the native Facebook for iOS app installed, version 7.0 or higher
you can also ask for "publish_actions", you need to login twice, first with readPermission for email, then when user wants to share a post loginWithPublishPermission for "publish_actions". the point is that you have to add "publish_actions" first of all in your Facebook developer account.
I am wondering if it is possible to share/send a url link and other media from iOS app via Facebook Messenger app without having to open UIActivityViewController's activity sheet first, which in addition shows a number of other apps which can handle the intended data and does not allow to exclude apps only UIActivityTypes. I would like to present a FB Messenger dialog with pre-filled url link immediately after a button tap, so no dialog with multiple other apps.
Facebook documentation suggests we should use FB SDK in such cases: https://developers.facebook.com/docs/sharing/ios#message. But what is beyond me is that iOS seems to have a "native access" to the Messenger app (and other apps), but there is no good API which could facilitate some UI customisation.
Any idea if there is a way to directly open FB Messenger with pre-filled message without needing to present UIActivityViewController's activity sheet or installing FB SDK?
EDIT: Let me clarify, I would like to share a url link or other media, it is not necessary that the message is pre-filled with text.
You can share a link via messenger in this way
let urlStr = String(format: "fb-messenger://share/?link=%#", sharelink!)
let url = NSURL(string: urlStr)
if UIApplication.shared.canOpenURL(url! as URL) {
UIApplication.shared.open(url! as URL, options: [:]) { (success) in
if success {
print("Messenger accessed successfully")
} else {
print("Error accessing Messenger")
}
}
} else {
// show Install Messenger
}
After lots of searching I can confirm that it is not possible to share text or other media via Facebook Messenger without using their FB SDK as they do not provide URL schemes for that.
I want to connect users' Instagram accounts to my app, and be able to make them follow other users (that s/he chooses) on Instagram; or at least triggering the Instagram app and go to users' profile. I am currently using Swift and the documentations out there are not so clear. Is it actually possible to do what I want to do? If so, how can I do it? Thanks in advance for your helps.
This function will do the job:
if instagram app is not installed, then it will open the webpage of instagram in safari.
If the app is installed, it opens the page you want in instagram app:)
func openInstagram() {
var instURL: NSURL = NSURL (string: "instagram://user?username=Instagram")! // Replace = Instagram by the your instagram user name
var instWB: NSURL = NSURL (string: "https://instagram.com/instagram/")! // Replace the link by your instagram weblink
if (UIApplication.sharedApplication().canOpenURL(instURL)) {
// Open Instagram application
UIApplication.sharedApplication().openURL(instURL)
} else {
// Open in Safari
UIApplication.sharedApplication().openURL(instWB)
}
}
Here is document page from Instagram for ios developers (you can find how to open user Instagram profile): https://instagram.com/developer/mobile-sharing/iphone-hooks/
Library that may help you (connect user accounts, follow): https://github.com/shyambhat/InstagramKit
I want to give the user a link to my app's Facebook page so the user can like it. I don't want there to be any login, permissions or any other social functionality. I thought I could use a simple link like this:
UIApplication.sharedApplication().openURL(NSURL(string: "https://www.facebook.com/MyAppPage")!)
I thought this would take the user to the Facebook app and open my app's page, or if the user doesn't have the Facebook app installed, take them to Safari. It did go to the Facebook app, but didn't go to my page. Is there a simple way to do this? Do I need to implement the Facebook SDK or Social Framework?
Here's the solution I use. Your app will have two links, one for the FB app and one for the browser. This will check to see if the device supports the FB app link first and if not show it in the browser (if available).
let urls = ["fb://profile/xxxxxxxx",
"https://m.facebook.com/xxxxxxx"];
let application = UIApplication.sharedApplication()
for urlString in urls {
let url = NSURL(string: urlString)
if(application.canOpenURL(url!))
{
application.openURL(url!)
return
}
}