Swift UIActivityViewController Image&Url Share 不适用于 FB

Posted

技术标签:

【中文标题】Swift UIActivityViewController Image&Url Share 不适用于 FB【英文标题】:Swift UIActivityViewController Image&Url Share not working with FB 【发布时间】:2015-09-29 11:03:31 【问题描述】:

我正在使用 UIActivityViewController 来分享图像。 Facebook最近的变化之后。我可以单独分享图片和网址,但我无法同时分享图片和网址。相同的代码适用于 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)


【问题讨论】:

【参考方案1】:

我已经为 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 的图片。

此代码的结果:

【讨论】:

您的 Facebook 原生应用版本? 我也有,它工作正常。尝试用另一台设备发帖。 对不起哥们,我不知道为什么它不起作用。如果有人知道这个问题,请等待另一个答案。 不,您不能这样做,因为预定义的文本将被忽略。 正如我所说,你不能添加文本.. :)【参考方案2】:

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)

【讨论】:

我试图在 Facebook 上发布图片,但没有发布。只能分享 URL。我们不能使用 UIActivityViewController 在 Facebook 上分享图像/视频吗? 最近你不能用自己的SDK吗:(@AnandGautam 正确@Safad Funy,但是我们可以使用SLComposeViewController在Facebook上分享图片,但需要使用FB SDK进行视频分享。【参考方案3】:

这是我的解决方案。实现UIActivityItemSource 协议。支持文字、网址和图片。

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
    

【讨论】:

【参考方案4】:

分享图片和文字

        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)

【讨论】:

以上是关于Swift UIActivityViewController Image&Url Share 不适用于 FB的主要内容,如果未能解决你的问题,请参考以下文章

Swift 反射

Swift入门系列--Swift官方文档(2.2)--中文翻译--About Swift 关于Swift

swift 示例BS swift.swift

swift swift_bug.swift

ios 整理(一)swift和oc的区别

swift swift_extension5.swift