Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share both Image and Text together in swift?

Tags:

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: {} ) } 
like image 974
hussain Avatar asked Sep 22 '16 07:09

hussain


1 Answers

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.

like image 64
Ketan P Avatar answered Sep 23 '22 09:09

Ketan P