web-dev-qa-db-ja.com

Swift UIActivityViewController Image&Url ShareがFBで機能しない

UIActivityViewControllerを使用して画像を共有しています。 Facebookの最近の変更後。画像とURLを個別に共有することはできますが、画像とURLの両方を同時に共有することはできません。同じコードがMail、Twitterなどで正常に機能します。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

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)

}

そしてURL付きの画像を投稿しています。

このコードの結果:

enter image description here

14
Dharmesh Kheni

スウィフト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

これが私の解決策です。 UIActivityItemSourceプロトコルを実装します。テキスト、URL、画像をサポートします。

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

画像とテキストを共有するには

        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