Swift:尝试使用Facebook Open Graph故事

时间:2017-07-04 23:53:01

标签: ios swift facebook facebook-opengraph

我试图在Swift中使用Facebook的Open Graph故事功能,在每次查看位置时在用户时间轴上发布信息。但是每个共享对话框都会打开,我不断收到this错误。

我按照Facebook Docs创建了一个对象,然后是一个动作,最后是内容。但它似乎并没有起作用。

以下是我的代码:

class rateViewController: UIViewController, FBSDKSharingDelegate{

@IBOutlet weak var card: UIView!
@IBOutlet weak var cardBackground: UIView!

// VARIABLES

// CREATE THE OPENGRAPH OBJECT
var openGraphTest = FBSDKShareOpenGraphObject.init(properties: [

    "place:location:latitude": "51.043553",
    "place:location:longitude":"-114.078141",
    "og:type" : "place",
    "og:title" : "Rating"
])

// CREATE AN ACTION
var action = FBSDKShareOpenGraphAction.init()

// CREATE CONTENT
let content = FBSDKShareOpenGraphContent.init()

override func viewDidLoad() {
    super.viewDidLoad()

    action.actionType = "og.likes"
    action.setObject(openGraphTest, forKey: "place")

    content.action = action
    content.previewPropertyName = "place"

    // Do any additional setup after loading the view.
}

@IBAction func panCard(_ sender: UIPanGestureRecognizer) {

    let card = sender.view! // Represents card UIView
    let point = sender.translation(in: view) // How far you moved your finger, when touching view and dragging

    card.center = CGPoint(x:(view.center.x + point.x), y: (view.center.y + point.y))

    // RECENTERING THE CARD AFTER USER REMOVES FINGER FROM SCREEN

    if(sender.state == UIGestureRecognizerState.ended){

        if(card.center.x < 75){

            // MOVE CARD TO THE LEFT SIDE OF SCREEN
            UIView.animate(withDuration: 0.3, animations: { 

                card.center = CGPoint(x: self.cardBackground.center.x - 300, y: self.cardBackground.center.y + 75)
                card.alpha = 0
            })
            return
        }
        else if(card.center.x > (view.frame.width - 75)){

            // MOVE CARD TO THE RIGHT SIDE OF SCREEN
            UIView.animate(withDuration: 0.3, animations: {

                card.center = CGPoint(x: self.cardBackground.center.x + 300, y: self.cardBackground.center.y + 75)
                card.alpha = 0
            })
            // SHOW THE FACEBOOK SHARE DIALOG

            try FBSDKShareDialog.show(from: self, with: content, delegate: self)
             return
        }
        UIView.animate(withDuration: 0.2) {
            card.center = self.cardBackground.center
        }
    }
}

@IBAction func likeButton(_ sender: UIButton) {

    // MOVE CARD TO THE RIGHT SIDE OF SCREEN

    UIView.animate(withDuration: 0.3, animations: {

        self.card.center = CGPoint(x: self.cardBackground.center.x + 300, y: self.cardBackground.center.y + 75)
        self.card.alpha = 0
    })

}

@IBAction func dislikeButton(_ sender: UIButton) {

    // MOVE CARD TO LEFT SIDE OF SCREEN

    UIView.animate(withDuration: 0.3, animations: {

        self.card.center = CGPoint(x: self.cardBackground.center.x - 300, y: self.cardBackground.center.y + 75)
        self.card.alpha = 0
    })
}

// FACEBOOK SHARING DELEGATE

/**

 Sent to the delegate when the share completes without error or cancellation.
 - Parameter sharer: The FBSDKSharing that completed.
 - Parameter results: The results from the sharer.  This may be nil or empty.

 */
public func sharer(_ sharer: FBSDKSharing!, didCompleteWithResults results: [AnyHashable : Any]!){


}


/**

 Sent to the delegate when the sharer encounters an error.
 - Parameter sharer: The FBSDKSharing that completed.
 - Parameter error: The error.

 */
public func sharer(_ sharer: FBSDKSharing!, didFailWithError error: Error!){

    print("Error: ", error)
}


/**

 Sent to the delegate when the sharer is cancelled.
 - Parameter sharer: The FBSDKSharing that completed.

 */
public func sharerDidCancel(_ sharer: FBSDKSharing!){

    self.dismiss(animated: true, completion: nil)
}
}

1 个答案:

答案 0 :(得分:0)

在联系Facebook并与其中一位开发人员交谈之后,起初我认为API中存在错误,但事实证明我做错了。以下是我的代码:

// CREATE THE OPEN GRAPH OBJECT

    let properties : [AnyHashable : Any] = [
        "fb:ap_id":"APP ID FOUND ON FACEBOOK APP DASHBOARD",
        "og:type":"object",
        "og:title": "Some Title",
    ]

    let object : FBSDKShareOpenGraphObject = FBSDKShareOpenGraphObject(properties: properties)

    // CREATE AN ACTION

    action.actionType = "og.likes"
    action.setObject(object, forKey:"object")

    // CREATE CONTENT MODEL

    content.action = action
    content.previewPropertyName = "object"

    // SETTING UP THE SHARE DIALOG

    shareDialog.fromViewController = self
    shareDialog.shareContent = content

   // SHOW THE SHARE DIALOG
   if(shareDialog.canShow()){
      shareDialog.show()
   }
   else{
      print("Unable to show dialog")
   }

   do{
      try shareDialog.validate()
      }
   catch{
      print("Invalid")
         }