Facebook分享链接和截图的问题

时间:2016-06-17 16:12:03

标签: ios swift facebook

当我只共享链接时,会显示该链接。当我只共享图像时,会出现图像。但是,当我想同时使用下面的代码时,只显示链接,没有图像。

我能同时做到吗?

let screen = UIScreen.mainScreen()

    if let window = UIApplication.sharedApplication().keyWindow {
        UIGraphicsBeginImageContextWithOptions(screen.bounds.size, false, 0);
        window.drawViewHierarchyInRect(window.bounds, afterScreenUpdates: false)
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();

        let composeSheet = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
        let urlPath: String = "https://itunes.apple.com/us/app/reflex-test!-how-far-can-you/id1123340346?ls=1&mt=8"
        let url: NSURL = NSURL(string: urlPath)!
        composeSheet.addURL(url)
        composeSheet.addImage(image)

        presentViewController(composeSheet, animated: true, completion: nil)

        }
    }   
}

2 个答案:

答案 0 :(得分:0)

您可以使用Facebook API执行操作,您可以在Facebook网站上执行此操作。

您无法在Facebook网站上同时分享链接和图片。

这意味着,您也无法使用API​​

但是,您可以将链接添加为帖子的文本。

答案 1 :(得分:0)

我使用相同的代码,它对我有用。请在下面找到附带的屏幕截图:

enter image description here

enter image description here

enter image description here

我使用以下代码:

let facebookSheet:SLComposeViewController = SLComposeViewController(forServiceType: SLServiceTypeFacebook)
            UIGraphicsBeginImageContextWithOptions(UIScreen.mainScreen().bounds.size, false, 0);
            UIApplication.sharedApplication().keyWindow!.drawViewHierarchyInRect(UIApplication.sharedApplication().keyWindow!.bounds, afterScreenUpdates: false)
            let image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            facebookSheet.addImage(image)
            facebookSheet.addURL(NSURL(string: "http://www.google.co.in"))
            self.presentViewController(facebookSheet, animated: true, completion: nil)