Just a quick video to show you how to use the facebook native ios sdk in swift.
follow the directions to get your plist file updated here: https://developers.facebook.com/quickstarts/?platform=ios
Once that’s done follow the instructions to add your build identity to facebooks records. Then…
- drag the frameworks into your project
- update your appdelegate file
- update your view controller
- make your view controller a delegate for the login button
- test
AppDelegate.swift:
replace your applicationdidlaunchwithoptions:
[pastacode lang=”javascript” message=”” highlight=”” provider=”manual”]
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
// Override point for customization after application launch.
return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
}
[/pastacode]
Then add the following:
[pastacode lang=”javascript” message=”” highlight=”” provider=”manual”]
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject?) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}
[/pastacode]
Then in your “applicationDidBecomeActive“:
[pastacode lang=”javascript” message=”” highlight=”” provider=”manual”]
FBSDKAppEvents.activateApp()
[/pastacode]
NOTE: make sure to import:
[pastacode lang=”javascript” message=”” highlight=”” provider=”manual”]
import FBSDKCoreKit
[/pastacode]
ViewController.swift:
[pastacode lang=”javascript” message=”” highlight=”” provider=”manual”]
//
// ViewController.swift
// FacebookTutorial
//
// Created by Mike Newell on 8/13/15.
// Copyright (c) 2015 Mike Newell. All rights reserved.
//
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
if FBSDKAccessToken.currentAccessToken() != nil {
// user already has access token
self.logUserData()
} else {
let loginButton = FBSDKLoginButton()
loginButton.center = self.view.center
loginButton.readPermissions = ["email"]
self.view.addSubview(loginButton)
loginButton.delegate = self
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
self.logUserData()
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError!) {
println("logged in")
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
println("logged out")
}
func logUserData() {
let graphRequest = FBSDKGraphRequest(graphPath: "me", parameters: nil)
graphRequest.startWithCompletionHandler { (connection, result, error) -> Void in
if error != nil {
println(error)
} else {
println(result)
}
}
}
}
[/pastacode]
That’s it! here’s a video explaining more:
There’s also a great tutorial for anyone looking to do the older “bridging” method of importing the sdk: http://www.brianjcoleman.com/tutorial-how-to-use-login-in-facebook-sdk-4-0-for-swift/
Hello,
Great post and video. I was able to get logged in by the end of the video thanks! I do have a question. I am very new to ios, I have been coding C# for a few years and would like to start working on some apps that I have in mind but I can’t seem to be able to post a image to facebook. I have been able to get a message to post through the sdk but not an image. Have you dont that yet? If so could you please share the code . Thanks again.
Kurt
Use Graph API. As long you have correct access token you can send anything
Hi, Nice tutorial.
I already add all those required framework. But when I put ‘Import FBSDKCoreKit’ in my AppDelegate.Swift, it shows an error and state that “No such module FBSDKCoreKit”.
What’s wrong with my code?