Facebook, Messenger - Photo, Video, Link sharing in iOS Swift.

Mohammad Mahmudul Hasan
4 min readJun 7, 2021

In this tutorial we will learn FBSDKShareKi to share Image, Video and Link in facebook story, facebook post / news feed, facebook group. And also learn to share Hashtag and Quote in share content.

Step 1: Register and Configure Your App with Facebook

If you don’t have Facebook Developer Account , please create. Then create an App and copy App ID for Step 2.

Create App (1)
Create App (2)
App ID (Copy)

Step 2: Set Up Your Development Environment

Make sure you have the CocoaPods gem installed on your machine before installing the FBSDKShareKi pod.

Add the following to your Pod-file:

pod ‘FBSDKLoginKit’

Run the following command in your project root directory from a terminal window:

pod install

Step 3: Configure Your Project

  1. Configure the Info.plist file with an XML snippet.
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLSchemes</key>
<array>
<string>fb[APP_ID]</string>
</array>
</dict>
</array>
<key>FacebookAppID</key>
<string>[APP_ID]</string>
<key>FacebookDisplayName</key>
<string>[APP_NAME]</string>

2. Replace [APP_ID] with your App ID.

3. Replace [APP_NAME] with the name of your app(FacebookDisplayName).

4. Application’s Info.plist also needs to include the XML snippet below to use any of the Facebook dialogs (e.g., Login, Share, App Invites, etc.)

<key>LSApplicationQueriesSchemes</key>
<array>
<string>fbapi</string>
<string>fbapi20130214</string>
<string>fbapi20130410</string>
<string>fbapi20130702</string>
<string>fbapi20131010</string>
<string>fbapi20131219</string>
<string>fbapi20140410</string>
<string>fbapi20140116</string>
<string>fbapi20150313</string>
<string>fbapi20150629</string>
<string>fbapi20160328</string>
<string>fbauth</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>

Step 4: Prepare App Delegate and Scene Delegate

Update AppDelegate like below:

// Swift
//
// AppDelegate.swift
import UIKit
import FBSDKCoreKit
@main
class AppDelegate: UIResponder, UIApplicationDelegate {

func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {

ApplicationDelegate.shared.application(
application,
didFinishLaunchingWithOptions: launchOptions
)
return true
}
}

iOS 13 moved opening URL functionality to the SceneDelegate. Please add the following method to your SceneDelegate :

// Swift
//
// SceneDelegate.swift
import UIKit
import FBSDKCoreKit
func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let url = URLContexts.first?.url else {
return
}
ApplicationDelegate.shared.application(
UIApplication.shared,
open: url,
sourceApplication: nil,
annotation: [UIApplication.OpenURLOptionsKey.annotation]
)
}

Step 5: Implement Photo Sharing (News Feed, Group, Story, Messenger)

// Assumes your assets contain an image named "test"
guard let image = UIImage(named: "test") else {
return
}

let photo = SharePhoto(image: image, userGenerated: true)
let content = SharePhotoContent()
content.photos = [photo]
// Optional
/*content.hashtag = Hashtag("#MatrixSolution")
content.quote = "MatrixSolution"
content.contentURL = URL.init(string: "https://matrixsolution.xyz/")!*/
let dialog = ShareDialog.init(fromViewController: self, content: content, delegate: self)// Recommended to validate before trying to display the dialog
do {
try dialog.validate()
} catch {
print(error)
}

dialog.show()

Step 6: Implement Video Sharing (News Feed, Group, Story, Messenger)

// Assuming you have a URL for a PHAsset
let video = ShareVideo(videoURL: assetURL)
let content = ShareVideoContent()
content.video = video
// Optional
/*content.hashtag = Hashtag("#MatrixSolution")
content.quote = "MatrixSolution"
content.contentURL = URL.init(string: "https://matrixsolution.xyz/")!*/
let dialog = ShareDialog.init(fromViewController: self, content: content, delegate: self)// Recommended to validate before trying to display the dialog
do {
try dialog.validate()
} catch {
print(error)
}

dialog.show()

Step 7: Implement Link Sharing (News Feed, Group, Story, Messenger)

guard let url = URL(string: "https://matrixsolution.xyz/") else {
preconditionFailure("URL is invalid")
}

let content = ShareLinkContent()
content.contentURL = url
// Optional
/*content.hashtag = Hashtag("#MatrixSolution")
content.quote = "MatrixSolution"*/

let dialog = ShareDialog.init(fromViewController: self, content: content, delegate: self)
do {
try dialog.validate()
} catch {
print(error)
}

dialog.show()

For Messenger Just use below line for init dialog instead of above one

let dialog = MessageDialog(content: content, delegate: self)

And don’t forget to implement SharingDelegate

func sharer(_ sharer: Sharing, didCompleteWithResults results: [String : Any]) {}func sharer(_ sharer: Sharing, didFailWithError error: Error) {}func sharerDidCancel(_ sharer: Sharing) {}

Sample Output:

a. Share Dialog b. Message Dialog

Conclusion

And finally we are done. Hope this will help you. Thank you so much. Happy coding :)

--

--

Mohammad Mahmudul Hasan

A learner, constantly striving to learn new technologies and look the ways to be better in this rapidly changing industry.