Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift UIActivityViewController Image Share not working

Tags:

ios

swift

I am using UIActivityViewController to share image. After the WhatsApp recent changes to allow share i am able to see WhatsApp in the share option. I am sharing an image and message, I can see the text message but i am not able to share images. The same code works fine with Viber, FB, twitter etc., Not sure what i am missing for WhatsApp.

func shareImage() {
    var messageStr:String  = "Check out my awesome photo!"
    var img: UIImage = currentPhoto!

    var shareItems:Array = [img, messageStr]

    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]

    self.presentViewController(activityViewController, animated: true, completion: nil)

}
like image 316
Dinesh Jeyasankar Avatar asked May 02 '15 13:05

Dinesh Jeyasankar


2 Answers

It seems like WhatsApp shares image only when the array contains images and not combination of both image and text.

func shareImage() {
    //var messageStr:String  = "Check out my awesome iPicSafe photo!"
    var img: UIImage = currentPhoto!
    //var shareItems:Array = [img, messageStr]
    var shareItems:Array = [img]
    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
    self.presentViewController(activityViewController, animated: true, completion: nil)
}
like image 195
Dinesh Jeyasankar Avatar answered Nov 03 '22 18:11

Dinesh Jeyasankar


To share Text || Image try this Swift.

func share(shareText shareText:String?,shareImage:UIImage?){

    var objectsToShare = [AnyObject]()

    if let shareTextObj = shareText{
        objectsToShare.append(shareTextObj)
    }

    if let shareImageObj = shareImage{
        objectsToShare.append(shareImageObj)
    }

    if shareText != nil || shareImage != nil{
        let activityViewController = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)
        activityViewController.popoverPresentationController?.sourceView = self.view

        presentViewController(activityViewController, animated: true, completion: nil)
    }else{
        print("There is nothing to share")
    }
}

To share just call like this:

let imageToShare = UIImage(named: "myImage")
share(shareText: "Sharing this text", shareImage: imageToShare)
like image 6
Mohammad Zaid Pathan Avatar answered Nov 03 '22 19:11

Mohammad Zaid Pathan