How to implement iOS app link Facebook functionality in swift 3? - ios

I have tried many ways to achieve this but failed.
I have to share info on facebook with a URL and when clicked on the url, it will redirect to my app's specific page.
step# 1
let content: FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: linkUrl) as URL!
content.contentTitle = "Test"
content.quote = "game"
content.contentDescription = "This is a test game to test Fb share functionality"
content.imageURL = NSURL(string: imgUrl) as URL!
let dialog: FBSDKShareDialog = FBSDKShareDialog()
dialog.mode = FBSDKShareDialogMode.native
dialog.fromViewController = vc
dialog.shareContent = content
// if you don't set this before canShow call, canShow would always return YES
if !dialog.canShow() {
// fallback presentation when there is no FB app
dialog.mode = FBSDKShareDialogMode.feedBrowser
}
dialog.show()
It is working and successfully sharing the post. When I clicked on the link, it is redirecting to the app, not much info to redirect to a particular ViewController.
Step# 2
let properties = [
"fb:app_id": "Fb_id",
"og:type": "article",
"og:title": "Test",
"og:description": "This is a test game to test Fb share functionality",
"og:image" : urlImg,
"og:url" : linkUrl,
]
let object : FBSDKShareOpenGraphObject = FBSDKShareOpenGraphObject.init(properties: properties)
// Create an action
let action : FBSDKShareOpenGraphAction = FBSDKShareOpenGraphAction()
action.actionType = "news.publishes"
action.setObject(object, forKey: "article")
// Create the content
let content : FBSDKShareOpenGraphContent = FBSDKShareOpenGraphContent()
content.action = action
content.previewPropertyName = "article"
FBSDKShareDialog.show(from: vc, with: content, delegate: nil)
Here I am using Open Graph to post and successfully posted the Info. But con't redirecting to my app when clicked the link.
NB:
I don't have web Application.
My goal is to share a post with a app link. When clicked on that link it will open the app and redirect to a specific page if app is installed, otherwise redirect to the AppStore. So what should be the link format or how can I build the link to achieve this functionality?
Please help.
Thanks in advance

Use Universal Links. If you want to achieve something like this using universal link is a good idea. You can redirect to a specific page in your app if the app is installed otherwise it will navigate to appstore.
Here is how to implement universal links if you are not aware of:
Set up universal links - Raywenderlich
Universal links - Appsflyer
Note: Please ignore this, if this is not what you are looking for

I managed to achieve the same result using Branch.io. It's a very powerful app routing SDK and their console practically guides you down the routing scheme in a very visual and understandable manner.
The way it works is by dynamically creating a webpage with certain redirecting features according to what you set in the dashboard. You can then use that generated webpage to share it on Facebook or anywhere else.
Here's how you can integrate it:
Create an account here
Install and link the SDK (pod 'Branch')
Set up the routing mechanism using Branch's dashboard (it's guided and pretty straightforward, you just fill in the branches below). You can always change how the diagram looks like (if you want to always end up on a website or so, for instance)
Initialize the share data in your app
let branchUniversalObject: BranchUniversalObject = BranchUniversalObject(canonicalIdentifier: "any ID here") // just for tracking count and analytics
branchUniversalObject.title = "some title" // This will appear as the shared content's title
branchUniversalObject.contentDescription = "some description" // This will appear as the shared content's description
branchUniversalObject.imageUrl = "www.example.com/test.png"
branchUniversalObject.addMetadataKey("uid", value: self.userId)
branchUniversalObject.addMetadataKey("otherInfo", value: self.otherInfo)
Generate link and share the link on Facebook
let linkProperties = BranchLinkProperties()
linkProperties.channel = "Facebook"
linkProperties.feature = "Share"
// generate link
branchUniversalObject.getShortUrl(with: linkProperties, andCallback: { (shareURL, error) in
if error != nil {
// handle error
}
// init Facebook share data
let content = FBSDKShareLinkContent()
content.contentTitle = "Some title"
content.contentURL = URL(string: shareURL) // this URL handles the redirecting according to your dashboard's settings
content.contentDescription = "Some description"
content.imageURL = URL(string: "www.example.com/test.png")
// create Facebook share dialog
let dialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self
dialog.shareContent = content
dialog.mode = .automatic
if dialog.canShow() {
dialog.show()
}
})

Yes, I achieved this functionality by setting up the metadata in the server end.
References: https://developers.facebook.com/docs/applinks
https://developers.facebook.com/docs/applinks/ios
Thanks...

Related

How to enable camera in MFMessageComposeViewController?

I am developing an iOS application with a button to report an issue using SMS/iMessage. I am using MFMessageComposeViewController to present the message composition interface using the following code (Swift 3):
if(MFMessageComposeViewController.canSendText()){
let controller = MFMessageComposeViewController()
controller.messageComposeDelegate = self
controller.body = "Example Message"
controller.recipients = ["2345678901"]
self.present(controller, animated: true, completion: nil)
}
I have also implemented the MFMessageComposeViewControllerDelegate function to dismiss properly. A standard text message / iMessage sends successfully, but the user does not have the option to attach an image. The buttons for camera, iMessage Apps, etc. are there, but they are disabled and cannot be pressed. How can I enable these buttons (camera, specifically) to allow my users to attach images to messages composed with the app?
The Buttons in Question:
EDIT:
Thanks Abdelahad for the suggestion. I've modified his response to allow multiple recipients and to include a message body. I also updated it to remove the deprecated addingPercentEscapes(using: ) method.
Here is a solution using a url to open the Messages app. NOTE: This takes users out of the app.
let recipients = "2345678901,3456789012" //Phone Numbers
let messageBody = "This is a test"
let sms: String = "sms://open?addresses=\(recipients)&body=\(messageBody)"
let smsEncoded = sms.addingPercentEncoding(withAllowedCharacters: .urlFragmentAllowed)
let url = URL(string: smsEncoded!)
UIApplication.shared.openURL(url!)
But still I would like a solution that does not take the user out of the app. Is this possible? Why would the MFMessageComposeViewController show the buttons without enabling them?
Don't use MFMessageComposeViewController use UIApplication.shared.openURL(url!) but this will takes the user out of the app
var phoneToCall: String = "sms: +201016588557"
var phoneToCallEncoded = phoneToCall.addingPercentEscapes(using: String.Encoding.ascii)
var url = URL(string: phoneToCallEncoded)
UIApplication.shared.openURL(url!)

Using Branch.io generated link with Facebook share dialog

I went through Branch.io's documentation a lot over the past few days and I can't seem to pinpoint the problem. I got the generated link to work properly by setting up the BranchUniversalObject as follows:
let branchUniversalObject: BranchUniversalObject = BranchUniversalObject(canonicalIdentifier: "ios_share_user")
branchUniversalObject.title = "My Title"
branchUniversalObject.contentDescription = "My Description"
branchUniversalObject.imageUrl = "www.example.com/image.jpg"
branchUniversalObject.addMetadataKey("id", value: self.userId)
branchUniversalObject.addMetadataKey("type", value: self.userType)
let linkProperties = BranchLinkProperties()
linkProperties.channel = "Facebook"
linkProperties.feature = "Share"
branchUniversalObject.getShortUrl(with: linkProperties, andCallback: { (shareURL, error) in
if error != nil {
print(error!)
return
}
// using shareURL here
})
The generated URL works perfectly fine, but only when launched from Notes or Messages apps. Safari always redirects me to the AppStore and so does the Facebook app when I try sharing the URL like this:
let content = FBSDKShareLinkContent()
content.contentTitle = "My Title"
content.contentURL = shareURL // generated above
content.contentDescription = "My Description"
content.imageURL = "www.example.com/image.jpg"
let dialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.delegate = self
dialog.shareContent = content
if dialog.canShow() {
dialog.show()
}
That shows up the dialog and shares the content as intended, but the link always redirects me to the AppStore just like in Safari.
Here's a screenshot of the Dashboard:
I also have applinks:myLinkDomain.app.link and applinks:myLinkDomain-alternate.app.link in the Associated Domains, branch_app_domain = myLinkDomain.app.link along with branch_key = ["live": "key_live_myKey"] in Info.plist, and AppDelegate is properly set.
What am I missing to get the app to open from Safari and Facebook instead of getting redirected to the AppStore?
I'm deploying the app for iOS 9.3+
Thanks!
I came across this link and it shows that the problem isn't with Branch.io SDK's setup by any means, but a limitation imposed by Apple. The only workaround is using Branch's Deepviews, which unfortunately I can't use in my case.
Hope Branch.io figure out a way around that! ...And possibly update their documentation to clearly state that some app browsers are not supported

Error when Facebook Open Graph Action shared from iOS (unsafe link)

I tried to share a Facebook Open Graph Action via official iOS SDK (last version) but for a few days now, this action returns an error and it has now stopped working.
The app passed the Facebook approval, including actions and related objects and it all seems correct.
Object creation and share action
// ############## OpenGraph - Arrive At a Marina
// Photo
var photoURL = ""
if let image = firstMarina.images.first {
photoURL = image.width1440
} else {
photoURL = "https://fbstatic-a.akamaihd.net/images/devsite/attachment_blank.png"
}
let photo = FBSDKSharePhoto(imageURL: NSURL(string: photoURL)!, userGenerated: false)
// Properties
let properties = [
"fb:app_id": "xxxxxxxxxxxxxxxxxxxxx",
"og:locale": NSLocale.preferredLanguages()[0].stringByReplacingOccurrencesOfString("-", withString: "_"),
"og:type": "smartsea:marina",
"og:title": firstMarina.name!.text,
"og:description": firstMarina.desc!.text,
"og:image": [photo],
"place:location:latitude": firstMarina.location!.lat,
"place:location:longitude": firstMarina.location!.lng
]
// Object
let object = FBSDKShareOpenGraphObject(properties: properties as [NSObject : AnyObject])
// Action
let action = FBSDKShareOpenGraphAction(type: "smartsea:arrive_at", object: object, key: "marina")
// Content
let content = FBSDKShareOpenGraphContent()
content.action = action
content.previewPropertyName = "marina"
// Share
FBSDKShareDialog.showFromViewController(self, withContent: content, delegate: self)
and the error returned
Error Domain=com.facebook.Facebook.platform Code=102 "(null)" UserInfo={error_reason=The content you're trying to share includes a link that our security systems detected to be unsafe:
https://m.facebook.com/appcenter/smartsea?fbs=9909&fb_object_id=1684374595135519
Please remove this link to continue., error_description=An error occurred during publishing., app_id=xxxxxxxxxxxxxxxxxxxxx, error_code=102}
The strange thing is that the error URL is a Facebook domain and I haven't share it this URL directly. It seems to be generated with every share action.
Any idea?
Thanks!
Unfortunately I can't post a simple comment, so I have to post it as an 'answer'. I have found this article. It's about blocked URLs, but unfortunately not about blocked 'Facebook' URLs. I hope it can help.
http://www.technerves.com/2015/07/unblock-your-website-url-from-facebook.html
It connection might be getting blocked due to App transport security. App transport security is a new thing introduced in iOS9.
It blocks connections to servers that don't meet certain security requirements, such as minimum TLS version etc
Please try again after turning ATS OFF from info.plist. See this link which shows how to turn ATS OFF.

How to share Facebook page using Xamarin.iOS Facebook SDK?

I use this code in my apps to share Facebook pages:
class func shareWithDialog(viewController : UIViewController, delegate : FBSDKSharingDelegate, contentTitle: String, contentUrl: NSURL = url, contentDescription: String) {
let content = FBSDKShareLinkContent()
content.contentTitle = contentTitle
content.contentURL = contentUrl
content.contentDescription = contentDescription
let dialog = FBSDKShareDialog()
dialog.fromViewController = viewController
dialog.shareContent = content
dialog.mode = FBSDKShareDialogMode.FeedBrowser
dialog.delegate = delegate
dialog.show()
}
In the native code (above) FBSDKShareLinkContent.contentURL is set to the facebook page I want to share. I'm using Xamarin.Facebook.iOS.4.5.1.0 nuget package to add this functionality into a Xamarin app. The thing is that this SDK is similar to the native SDK but class ShareLinkContent doesn't have the property contentURL (it does have a ImageURL property).
I might be using the wrong class, but I could'n find the right class (other classes in the SDK are intended for sharing images and videos).
Thanks to Jason Steele I looked closer and see that contentUrl property was replaced by setContentURL() method. I used this method to point to the Facebook page I wanted to share and it worked.
It was a silly question after all ;)

Facebook share success callback?

I would like to know if I can check if my content was actually shared in Facebook?
I'm using Facebook SDK 4.1.x and FBSDKShareButton
The user has to authorize my app in Facebook, so I have a FB access token.
And I have this method to test the Share button
func newShareButton(){
let content : FBSDKShareLinkContent = FBSDKShareLinkContent()
content.contentURL = NSURL(string: "http://www.google.com.br")
content.contentTitle = "Test title"
content.contentDescription = "Test description"
content.imageURL = NSURL(string: "https://www.google.com.br/images/nav_logo225.png")
shareButton.shareContent = content
}
Everything is working fine, but I can't figure it out how to get the response, if the user actually shared the content.
Is it possible? Do I have to use other method to get the result?
Thanks in advance!

Resources