web-dev-qa-db-fra.com

Swift UIActivityViewController Image & Url Share ne fonctionne pas avec FB

J'utilise UIActivityViewController pour partager l'image. Après les récents changements sur Facebook. Je peux partager une image et une URL individuellement, mais je ne peux pas partager à la fois l'image et l'URL. Le même code fonctionne bien avec Mail, Twitter, etc., je ne sais pas ce qui me manque pour 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)

}
14

J'ai modifié votre code pour 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)

}

Et il affiche une photo avec URL.

Résultat avec ce code:

enter image description here

14
Dharmesh Kheni

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)
6
Ahmed Safadi

Voici ma solution. Implémentez le protocole UIActivityItemSource. Soutenez le texte, l'URL et l'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
    }
}
2
Hilen

Pour partager l'image et le texte

        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)
1
Hardik Thakkar