Send image and text with OpenGraph FacebookSDK 4.4 in Swift

帅比萌擦擦* 提交于 2020-01-03 04:16:30

问题


I am working with Facebook SDK 4.4.0 in my Swift App. Because Facebook forced the removal of prefilled text, I am trying to use Open Graph to post on Facebook content and image from my app.

What I need to do is to send to the Facebook Timeline some text content and also a local image (not on the web). The image is local so it is within the app.

What I achieved until now is to send the local image OR the content, but I want to send both things together.

Here is the Swift code to send the image that works:

    @IBAction func facebookButton(sender: UIButton) {
        let photo : FBSDKSharePhoto = FBSDKSharePhoto()
        photo.image = UIImage(named: oldImageSaved! as String)
        photo.userGenerated = false

        var content : FBSDKSharePhotoContent = FBSDKSharePhotoContent()
        content.photos = [photo]

        let button:FBSDKShareButton = FBSDKShareButton()
        button.frame = CGRectMake(0, 0, 0, 0)
        button.shareContent = content
        button.sendActionsForControlEvents(UIControlEvents.TouchUpInside)
    }

And here is the OpenGraph code, that works, to send the content:

    @IBAction func facebookButton(sender: UIButton) {

            var current = NSUserDefaults.standardUserDefaults().objectForKey("currentPhrase") as? String
            var currentDescription = current!
            let ImageSaved = NSUserDefaults.standardUserDefaults().objectForKey("currentImageSaved") as? NSString

            // Create a FBSDKSharePhoto
            var photo : FBSDKSharePhoto = FBSDKSharePhoto()
            photo.image = UIImage(named: ImageSaved! as String)
            photo.userGenerated = false;

            let graphProperties : [NSObject : AnyObject]! = ["og:type": "fbtestapp:testing_test_app", "og:title":"Test App", "og:description": currentDescription]
            var graphObject : FBSDKShareOpenGraphObject = FBSDKShareOpenGraphObject(properties: graphProperties)

            // Create an action
            var action : FBSDKShareOpenGraphAction = FBSDKShareOpenGraphAction()

            action.actionType = "fbtestapp:merging"
            action.setObject(graphObject, forKey: "testing_test_app")

            // Create the content
            var content : FBSDKShareOpenGraphContent = FBSDKShareOpenGraphContent()
            content.action = action;
            content.previewPropertyName = "testing_test_app";

            FBSDKShareDialog.showFromViewController(self, withContent: content, delegate: nil)
    }

Any help on how can I send local image with some text content with OpenGraph in Swift will be really appreciated!


回答1:


You Open Graph actions needs to be approved for an additional capability: User Messages. You can read more on that in the Open Graph Docs.

This capability needs to be reviewed. When it is approved, you can use the message parameter on the FBSDKShareOpenGraphContent object.

Be aware: this is only allowed when the message is coming directly, and only, from the user. If you app is pre-filling this value, it will be blocked and you will not be able to use it.



来源:https://stackoverflow.com/questions/31812809/send-image-and-text-with-opengraph-facebooksdk-4-4-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!