How to share both Image and Text together in swift?

前端 未结 3 1657
無奈伤痛
無奈伤痛 2021-02-07 04:06

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 UIActivity

3条回答
  •  情深已故
    2021-02-07 04:38

    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 :

    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 :

    Hope it will help you..

    Do let me know if you have any query.

提交回复
热议问题