Create Facebook Open Graph Object Using Swift

I am creating Custom Story using Facebook iOS SDK, the app is written in Swift. I based my code on the example of my documentation https://developers.facebook.com/docs/ios/open-graph#createobject .

Everything works smoothly until I need to create the FBGraphObject

NSMutableDictionary<FBOpenGraphObject> *object = [FBGraphObject openGraphObjectForPost];

      

in Swift I rewrote this snippet as:

var object = FBGraphObject.openGraphActionForPost()

// specify that this Open Graph object will be posted to Facebook
object.setObject(true, forKey: "provisionedForPost")

// for og:title
object.setObject(description["title"]!, forKey: "title")

// for og:type, this corresponds to the Namespace you've set for your app and the object type name
object.setObject("bppridereport:ride", forKey: "type")

// for og:description
object.setObject(description["description"]!, forKey: "description")

FBRequestConnection.startForPostOpenGraphObject(object, completionHandler: { (connection:FBRequestConnection!, result:AnyObject!, error:NSError!) -> Void in

    if error == nil {
        let objectId = result["id"]

      } else {
          NSLog("Error posting the Open Graph object to the Object API:", error);
      }

  })

      

This gives me an error: NSMutableDictionary is not identical to FBOpenGraphObject , which makes sense when reading the docs, FBOpenGraphObject is just a clad NSMutableDictionary, so I passed the object as FBOpenGraphObject, resolving the error

var object = FBGraphObject.openGraphActionForPost() as FBOpenGraphObject

      

The problem is that when I now compile this and run the application, it crashes on this line ... I'm puzzled. There's very little presence covering Swift's SDK implementation for Facebook. Any ideas why this is happening?

+3


source to share


2 answers


I passed the type FBGraphObject in the Facebook call.

I am using different calls to one in your example, since I set up html pages with metadata, but the refactoring of your example would be like this:



var object = FBGraphObject.openGraphActionForPost()

// Code to create object metadata omitted //

FBRequestConnection.startForPostOpenGraphObject(object as FBGraphObject, completionHandler: {
(connection:FBRequestConnection!, result:AnyObject!, error:NSError!) in
    if error == nil {
        let objectId = result["id"]
    } else {
       NSLog("Error posting the Open Graph object to the Object API:", error)
   }
})

      

Hope this helps.

0


source


I haven't used the Facebook API, so this is an educated guess at best:



 var object = FBGraphObject.openGraphActionForPost() as NSMutableDictionary<FBOpenGraphObject>

      

0


source







All Articles