Skip to content

Instantly share code, notes, and snippets.

@Coder-ACJHP
Created March 7, 2019 08:05
Show Gist options
  • Save Coder-ACJHP/10d54cee559d62828aa54cb16135c13e to your computer and use it in GitHub Desktop.
Save Coder-ACJHP/10d54cee559d62828aa54cb16135c13e to your computer and use it in GitHub Desktop.
Example code for "how to share image on Facebook, Instagram, Whatsapp and Activity" with Swift 4+
/* Common steps to use these codes */
// 1- you need to implement FBSharekit for sharing on Facebook
// 2- Copy and Paste the following text to your "info.plist" (anywhere)
/*
<key>FacebookAppID</key>
<string>08760425023140553</string> //This is a mock id, you must add your own real app id else it never will work
<key>FacebookDisplayName</key>
<string>DoYourBest</string> //Add your real app name here
<key>LSApplicationQueriesSchemes</key>
<array>
<string>whatsapp</string>
<string>instagram</string>
<string>fbapi</string>
<string>fb-messenger-share-api</string>
<string>fbauth2</string>
<string>fbshareextension</string>
</array>
*/
import FBSDKShareKit
class ShareHelper: NSObject {
static func shareImageViaWhatsapp(image: UIImage, onView: UIView) {
let urlWhats = "whatsapp://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
guard let imageData = image.pngData() else { debugPrint("Cannot convert image to data!"); return }
let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/whatsAppTmp.wai")
do {
try imageData.write(to: tempFile, options: .atomic)
self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
self.documentInteractionController.uti = "net.whatsapp.image"
self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: onView, animated: true)
} catch {
self.callAlertView(title: NSLocalizedString("information", comment: ""),
message: "There was an error while processing, please contact our support team.",
buttonText: "Close", fromViewController: topViewController!)
return
}
} else {
self.callAlertView(title: NSLocalizedString("warning", comment: ""),
message: "Cannot open Whatsapp, be sure Whatsapp is installed on your device.",
buttonText: "Close", fromViewController: topViewController!)
}
}
}
}
static func shareImageViaInstagram(image: UIImage, onView: UIView) {
let urlWhats = "instagram://app"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed) {
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
guard let imageData = image.pngData() else { debugPrint("Cannot convert image to data!"); return }
let tempFile = URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Documents/fitbestPhoto.igo")
do {
try imageData.write(to: tempFile, options: .atomic)
self.documentInteractionController = UIDocumentInteractionController(url: tempFile)
self.documentInteractionController.uti = "com.instagram.exclusivegram"
self.documentInteractionController.presentOpenInMenu(from: CGRect.zero, in: onView, animated: true)
} catch {
self.callAlertView(title: NSLocalizedString("information", comment: ""),
message: "There was an error while processing, please contact our support team.",
buttonText: "Close", fromViewController: topViewController!)
return
}
} else {
self.callAlertView(title: NSLocalizedString("warning", comment: ""),
message: "Cannot open Instagram, be sure Instagram is installed on your device.",
buttonText: "Close", fromViewController: topViewController!)
}
}
}
}
static func shareImageViaFacebook(image: UIImage, fromViewController: UIViewController) {
let sharePhoto = FBSDKSharePhoto()
sharePhoto.image = image
sharePhoto.isUserGenerated = true
let content = FBSDKSharePhotoContent()
content.photos = [sharePhoto]
let dialog = FBSDKShareDialog()
dialog.delegate = (fromViewController as! FBSDKSharingDelegate)
dialog.fromViewController = fromViewController
dialog.shareContent = content
dialog.mode = .shareSheet
dialog.show()
}
static func shareImageViaActivity(image: UIImage, onView: UIViewController) {
let vc = UIActivityViewController(activityItems: [image], applicationActivities: [])
// so that iPads won't crash
if UtilitiesHelper.deviceModel().contains("iPad") {
if let popoverController = vc.popoverPresentationController {
popoverController.sourceView = onView.view
popoverController.sourceRect = CGRect(x: onView.view.bounds.midX, y: onView.view.bounds.maxY, width: 0, height: 0)
popoverController.permittedArrowDirections = []
}
}
// exclude some activity types from the list (optional)
onView.present(vc, animated: true)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment