How to Share Both Image and Text Together in Swift

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.

Sharing multiple images AND text using activityController is not working

Make that array a flat object and it should work.

example:

func share() {
let activityItems = [
"Title",
"Body",
UIImage(systemName: "keyboard")!,
UIImage(systemName: "square.and.arrow.up")!
] as [Any]
let vc = UIActivityViewController(activityItems: activityItems, applicationActivities: nil)
vc.setValue("Testing", forKey: "Subject")
self.present(vc, animated: true, completion: nil)
}

Output:

Sample Image

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

IOS swift how can I share an image along with text using UIActivityViewController

For activity items you can give array of values so you can add another object as image

    @IBAction func Share_Action(_ sender: UIButton) {
let image = profile_image_string[indexPath.row] as UIImage
activityViewController = UIActivityViewController(activityItems: [Posts[sender.tag] as NSString, image], applicationActivities: nil)
present(activityViewController,animated: true,completion: nil)

}


Related Topics



Leave a reply



Submit