22

I am trying to share both image and text in swift. but when i choose to share via facebook, messenger or whatsapp it only gives text (image is not shared). I am using UIActivityViewController for sharing.

here is my code:

func displayShareSheet(latitude:NSString?, longitude:NSString?, image:UIImage?, address:NSString? ) {
    let activityViewController = UIActivityViewController(activityItems: [(latitude as NSString?)!, (longitude as NSString?)!, (image as UIImage?)!, (address as NSString?)!], applicationActivities: nil)
    presentViewController(activityViewController, animated: true, completion: {}
)
}
Ketan P
  • 4,259
  • 3
  • 30
  • 36
hussain
  • 769
  • 2
  • 7
  • 24

3 Answers3

14

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 :

enter image description here

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 :

enter image description here

Hope it will help you..

Do let me know if you have any query.

Ketan P
  • 4,259
  • 3
  • 30
  • 36
  • does this works for only facebook ? or everything @Ketan P – hussain Sep 23 '16 at 03:49
  • @hussain your question is for FB so I prepare code for FB only... But I think it should work for all option which support both image & text sharing together... – Ketan P Sep 23 '16 at 05:43
  • @hussain If helps you. pl. make it as answer. It will help other SO user to resolve same kind if issue. – Ketan P Sep 23 '16 at 07:32
  • @hussain thanks for the reply you may forget to make it answer. Note : It will help other SO user to resolve same kind if issue. – Ketan P Sep 30 '16 at 11:26
  • what is SLComposeViewController?? – hussain Oct 04 '16 at 04:18
  • Please refer : https://developer.apple.com/reference/social/slcomposeviewcontroller http://stackoverflow.com/questions/12503287/tutorial-for-slcomposeviewcontroller-sharing Do let me know if you require any other information. – Ketan P Oct 04 '16 at 05:57
  • either of it was not working for you ? can you share your code ? what error you get ? – Ketan P Oct 06 '16 at 06:20
  • let activityViewController = UIActivityViewController (activityItems: [latitude!,longitude!,image!,address!, ourURL], applicationActivities: nil) activityViewController.popoverPresentationController?.sourceView = self.view activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop,UIActivityTypePostToTwitter] self.presentViewController(activityViewController, animated: true, completion: nil) – hussain Oct 06 '16 at 06:53
  • let image = UIImage(named: "SoSampleImage1") let latitude = 23.23232412 let longitude = 46.23232412 let address = "Address" let ourURL = "our URL" let activityViewController:UIActivityViewController = UIActivityViewController (activityItems: [latitude,longitude,image!,address, ourURL], applicationActivities: nil) activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop,UIActivityTypePostToTwitter] self.presentViewController(activityViewController, animated: true, completion: nil) I use this rather lat and long everything else I am able to see. – Ketan P Oct 06 '16 at 08:52
  • Same code is working for me.. Thats why attach screen shot as well. – Ketan P Oct 06 '16 at 11:18
  • This won't share the text when shared to Facebook Messenger. How do you do that? – AndiAna Feb 24 '21 at 08:40
  • @AndiAna may be SDK is changed now. need to check for the same. – Ketan P Feb 24 '21 at 10:29
  • This method won't work when you try to send both text and image together to whatsapp. Refer: https://developer.apple.com/forums/thread/670873 – Shankar Jun 28 '22 at 09:54
0

Try this This is working for me!!!

@IBAction func btnExport(sender: AnyObject)
{

    print("Export")
    let someText:String = "Hello want to share text also"
    let objectsToShare:UIImage = self.imgView.image!
    let sharedObjects:[AnyObject] = [objectsToShare,someText]
    let activityViewController = UIActivityViewController(activityItems : sharedObjects, applicationActivities: nil)
    activityViewController.popoverPresentationController?.sourceView = self.view

    activityViewController.excludedActivityTypes = [ UIActivityTypeAirDrop, UIActivityTypePostToFacebook,UIActivityTypePostToTwitter]

    self.presentViewController(activityViewController, animated: true, completion: nil)
}
Sanjeet Verma
  • 551
  • 4
  • 15
  • it is not working for facebook, whatsapp aan messenger. if u want to send mail or text this works. – hussain Sep 22 '16 at 09:28
0

I achieve this with the help of VisualActivityViewController which is present in this GitHub repository

It gives me a nice, custom view as well--one that shows the user both the text and image that the user is going to share.

chb
  • 1,727
  • 7
  • 25
  • 47