I heard that the chat API for Facebook was deprecated a couple years back but I'm wondering if by using the new messenger API, it's possible to send a message to a friend via Facebook. The user logs in with the login button and gets their info with a graph request and now I want to send a test message.
Here is the code to login (works)
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
}
Login delegate, getting user's info (also works)
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)
}
}
}
Share method, having some trouble here... the code within the parameters dictionary and fields is probably incorrect, but the Facebook docs are pretty poor so it's hard to find good examples.
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)
}
})
}
}
Thanks for the help!