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

Step 1: Register and Configure Your App with Facebook

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

Step 2: Set Up Your Development Environment

pod ‘FBSDKLoginKit’
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>
<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

// 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
}
}
// 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()
let dialog = MessageDialog(content: content, delegate: self)
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

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mohammad Mahmudul Hasan

Mohammad Mahmudul Hasan

37 Followers

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