Basic Example For Sharing Text or Image With Uiactivityviewcontroller in Swift

Basic example for sharing text or image with UIActivityViewController in Swift

UIActivityViewController Example Project

Set up your storyboard with two buttons and hook them up to your view controller (see code below).

Sample Image

Add an image to your Assets.xcassets. I called mine "lion".

Sample Image

Code

import UIKit
class ViewController: UIViewController {

// share text
@IBAction func shareTextButton(_ sender: UIButton) {

// text to share
let text = "This is some text that I want to share."

// set up activity view controller
let textToShare = [ text ]
let activityViewController = UIActivityViewController(activityItems: textToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

// exclude some activity types from the list (optional)
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]

// present the view controller
self.present(activityViewController, animated: true, completion: nil)

}

// share image
@IBAction func shareImageButton(_ sender: UIButton) {

// image to share
let image = UIImage(named: "Image")

// set up activity view controller
let imageToShare = [ image! ]
let activityViewController = UIActivityViewController(activityItems: imageToShare, applicationActivities: nil)
activityViewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash

// exclude some activity types from the list (optional)
activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]

// present the view controller
self.present(activityViewController, animated: true, completion: nil)
}

}

Result

Clicking "Share some text" gives result on the left and clicking "Share an image" gives the result on the right.

Sample Image

Notes

  • I retested this with iOS 11 and Swift 4. I had to run it a couple times in the simulator before it worked because it was timing out. This may be because my computer is slow.
  • If you wish to hide some of these choices, you can do that with excludedActivityTypes as shown in the code above.
  • Not including the popoverPresentationController?.sourceView line will cause your app to crash when run on an iPad.
  • This does not allow you to share text or images to other apps. You probably want UIDocumentInteractionController for that.

See also

  • Add sharing to your Swift app via UIActivityViewController
  • UIActivity​View​Controller from NSHipster
  • UIActivityViewController documentation
  • Share extension documentation
  • comparison with UIDocumentInteractionController

How to share both Image and Text together in swift?

Below is UIActivityViewController code is working for me. also attached screen shot for both the methods.

 func shareImage() {
let img = UIImage(named: "SoSampleImage")
let messageStr = "Ketan SO"
let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: [img!, messageStr], applicationActivities: nil)
activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
self.presentViewController(activityViewController, animated: true, completion: nil)
}

Screen shot for UIActivityViewController example :

Sample Image

Alternative Using SLComposeViewController :

func share(){
let img = UIImage(named: "SoSampleImage")
let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
composeSheet.setInitialText("Hello, Ketan!")
composeSheet.addImage(img)
self.presentViewController(composeSheet, animated: true, completion: nil)
}

Screen shot for SLComposeViewController example :

Sample Image

Hope it will help you..

Do let me know if you have any query.

Swift 4 - UIActivityViewController share to Outlook Subject and Title

Whatever view controller triggered the sharing should conform to UIActivityItemSource protocol in order to setup: preview title, email subject and as well the content of the email.

You can try out your self this example which triggers sharing on a button tap:

class ViewController: UIViewController, UIActivityItemSource {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

@IBAction func sharePressed(_ sender: Any) {
let item = [self, "Preview Title"] as [Any]

let activityViewController = UIActivityViewController(activityItems: item, applicationActivities: nil)

self.present(activityViewController, animated: true, completion: nil)
}

func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return ""
}

func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
return "

Email body message with red background

"
}

func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
return "Email Subject"
}

}

To have this expected output:


Some apps like Outlook and Gmail in different iOS Versions (iOS 11, iOS 12..) behave differently: some take the first line of the body and set it as the Subject. Just be sure to test it out for the iOS version you are targeting and the app that you want to target the sharing functionality to behave correctly. You could also set multiple itemForActivityType to target logic for different apps when sharing.

Swift : Share image using UIActivityViewController

If you are running this on iOS Simulator, Mail component is likely to fail.

Apart from that, I don't think you need to cast your activity items list as UIImage. Simply put an array of objects as a activityItems array.



Related Topics



Leave a reply



Submit