How to share both Image and Text together in swift?

感情迁移 提交于 2020-07-17 09:33:26

问题


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: {}
)
}

回答1:


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.




回答2:


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)
}



回答3:


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.



来源:https://stackoverflow.com/questions/39632365/how-to-share-both-image-and-text-together-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!