【问题标题】:Swift: Trying to use Facebook Open Graph storiesSwift:尝试使用 Facebook Open Graph 故事
【发布时间】:2017-12-08 11:28:34
【问题描述】:

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

我按照 Facebook 文档创建对象,然后是操作,最后是内容。但这似乎不起作用。

下面是我的代码:

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)
}
}

【问题讨论】:

    标签: ios swift facebook facebook-opengraph


    【解决方案1】:

    在联系 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")
             }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多