0
我聽說幾年前Facebook的聊天API已被棄用,但我想知道是否通過使用新的Messenger API,可以發送消息給朋友通過Facebook。用戶使用登錄按鈕登錄並通過圖形請求獲取他們的信息,現在我想發送測試消息。從iOS應用通過Facebook SDK發送消息給朋友
下面是代碼內登錄(作品)
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)
}
})
}
}
感謝您的幫助!