簡體   English   中英

通過iOS應用通過Facebook SDK向朋友發送消息

[英]Send a message to friend via Facebook SDK from iOS app

我聽說幾年前不推薦使用Facebook的聊天API,但我想知道是否可以通過使用新的Messenger API來通過Facebook向朋友發送消息。 用戶使用登錄按鈕登錄,並通過圖形請求獲取其信息,現在我想發送測試消息。

這是登錄代碼(有效)

class ViewController: UIViewController, FBSDKLoginButtonDelegate {

var currentUserIDString : String?

override func viewDidLoad() {
    super.viewDidLoad()

    let facebookLoginButton = FBSDKLoginButton()
    facebookLoginButton.delegate = self
    facebookLoginButton.center = self.view.center
    self.view.addSubview(facebookLoginButton)

    let button : UIButton = FBSDKMessengerShareButton.circularButtonWithStyle(FBSDKMessengerShareButtonStyle.Blue, width: 80)
    button.addTarget(self, action: "shareTest", forControlEvents: UIControlEvents.TouchUpInside)
    button.frame = CGRectMake(self.view.frame.size.width/2 - 40, 390, 80, 80)
    self.view.addSubview(button)

}

// other methods

}

登錄代表,獲取用戶信息(也可以)

func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {


    let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: ["fields": "id, name, gender, email"])

    graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in

        if error != nil {
            print(error.localizedDescription)
        }

        else if let result = result {

            self.currentUserIDString = result["id"] as? String
            print(self.currentUserIDString)

        }
    }

}

共享方法,這里有些麻煩...參數字典和字段中的代碼可能不正確,但是Facebook文檔非常差,因此很難找到好的示例。

func shareTestGraphRequest() {

    if FBSDKAccessToken.currentAccessToken() != nil {

        if FBSDKAccessToken.currentAccessToken().hasGranted("read_page_mailbox") {

            //establish param dictionary here
            let parameterDictionary = NSMutableDictionary()
            parameterDictionary.setObject("This is a test message, I'm building an app!", forKey: "message")
            parameterDictionary.setObject(self.currentUserIDString!, forKey: "from")
            parameterDictionary.setObject([], forKey: "to")

            let graphRequest = FBSDKGraphRequest.init(graphPath: "/{message-id}", parameters: parameterDictionary as [NSObject : AnyObject], HTTPMethod: "POST")

            graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in


            }

        }
    }

    else {

        let loginManager = FBSDKLoginManager()
        loginManager.logInWithPublishPermissions(["read_page_mailbox"], fromViewController: self, handler: { (result, error) -> Void in

            if error != nil {
                print(error)
            }

            else if let result = result {
                print(result)

            }
        })
    }
}

謝謝您的幫助!

使用Facebook Messenger Messenger SDK集成,您可以將消息發送給您的朋友。 但這僅允許發送諸如圖像,GIF,視頻之類的媒體,而您不能通過此SDK發送純文本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM