15

I am using UIActivityViewController to share image. After the Facebook recent changes . I am able share an image and Url individualy but i am unable to share both image and url at a time. The same code works fine with Mail, twitter etc., Not sure what i am missing for Facebook.

func shareImage() {
    var myWebsite = NSURL(string:"http://www.google.com/")
    var img: UIImage = currentPhoto!
    var shareItems:Array = [img, myWebsite]
    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
    self.presentViewController(activityViewController, animated: true, completion: nil)

}
0

4 Answers 4

15

I have modified your code for swift 2.0:

@IBAction func shareButtonPressed(sender: AnyObject) {

    let myWebsite = NSURL(string:"http://www.google.com/")
    let img: UIImage = image!

    guard let url = myWebsite else {
        print("nothing found")
        return
    }

    let shareItems:Array = [img, url]
    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityTypePrint, UIActivityTypePostToWeibo, UIActivityTypeCopyToPasteboard, UIActivityTypeAddToReadingList, UIActivityTypePostToVimeo]
    self.presentViewController(activityViewController, animated: true, completion: nil)

}

And it is posting a picture with URL.

Result with this code:

enter image description here

14
  • 1
    your Facebook native App version? Sep 29, 2015 at 11:55
  • I have too and it is working fine.. Try to post with another device. Sep 29, 2015 at 12:01
  • Sorry buddy I have no idea why it is not working. Wait for another answer if any body knows about this issue. Sep 29, 2015 at 12:19
  • No you can not because predefined text will be ignored. Oct 1, 2015 at 6:18
  • As I said you can not add text.. :) Oct 1, 2015 at 6:21
6

SWIFT 3

    let myWebsite = NSURL(string: visitedlink)

    let img: UIImage = UIImage(named:"Logo")!

    guard let url = myWebsite else {
        print("nothing found")
        return
    }

    let shareItems:Array = [img,url]
    let activityViewController:UIActivityViewController = UIActivityViewController(activityItems: shareItems, applicationActivities: nil)
    activityViewController.excludedActivityTypes = [UIActivityType.print, UIActivityType.postToWeibo, UIActivityType.copyToPasteboard, UIActivityType.addToReadingList, UIActivityType.postToVimeo]
    self.present(activityViewController, animated: true, completion: nil)
3
  • I tried to post the image on Facebook but not posting. Only can able to share URL.. Can't we share an image/video on Facebook using UIActivityViewController? Feb 25, 2019 at 13:26
  • Will recently you can't you have to use there own SDK :( @AnandGautam Feb 26, 2019 at 0:42
  • Correct @Safad Funy , but we can share an image on Facebook using SLComposeViewController, but need to use FB SDK for Video share. Feb 26, 2019 at 12:50
2

Here is my solution. Implement the UIActivityItemSource protocol. Support text, URL and image.

func doShare() {
    let viewController = UIActivityViewController(activityItems: ["ScreenSort for iOS.", URL.init(string: "https://itunes.apple.com/app/id1170886809")!], applicationActivities: nil)
    viewController.popoverPresentationController?.sourceView = self.view // so that iPads won't crash
    self.present(viewController, animated: true, completion: nil)
}

extension YourViewController: UIActivityItemSource {
    func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
        return ""
    }

    func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivityType) -> Any? {
        return URL.init(string: "https://itunes.apple.com/app/id1170886809")!
    }

    func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivityType?) -> String {
        return "ScreenSort for iOS: https://itunes.apple.com/app/id1170886809"
    }

    func activityViewController(_ activityViewController: UIActivityViewController, thumbnailImageForActivityType activityType: UIActivityType?, suggestedSize size: CGSize) -> UIImage? {
        return nil
    }
}
1

To share image and text

        let imgShare : UIImage = UIImage(named: "store")!
        let textToShare = "Image form My app."

        let objectsToShare = [textToShare, imgShare]
        let activityVC = UIActivityViewController(activityItems: objectsToShare, applicationActivities: nil)

        activityVC.popoverPresentationController?.sourceView = sender
        self.presentViewController(activityVC, animated: true, completion: nil)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.