Skip to content

Instantly share code, notes, and snippets.

@iaserrat
Last active April 7, 2016 06:16
Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save iaserrat/4765b0f3532dfe87986e to your computer and use it in GitHub Desktop.
Save iaserrat/4765b0f3532dfe87986e to your computer and use it in GitHub Desktop.
This is a swift port of the official FacebookSDK login tutorial. It's been tested on Xcode 6 Beta 1 with the iOS 8 Simulator. You can find the official FB tutorial here: https://developers.facebook.com/docs/facebook-login/ios/v2.0
// YourProject-Bridging-Header.h
#import <FacebookSDK/FacebookSDK.h>
// AppDelegate.swift
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
.
.
.
func application(application: UIApplication, openURL url: NSURL, sourceApplication: NSString?, annotation: AnyObject) -> Bool {
var wasHandled:Bool = FBAppCall.handleOpenURL(url, sourceApplication: sourceApplication)
return wasHandled
}
}
// ViewController.swift
import Foundation
import UIKit
class LoginViewController: UIViewController, FBLoginViewDelegate {
var fbl: FBLoginView = FBLoginView()
@IBOutlet var loginView : FBLoginView
@IBOutlet var profilePictureView : FBProfilePictureView
@IBOutlet var userNameTxt : UILabel
@IBOutlet var logStatusTxt : UILabel
override func viewDidLoad() {
super.viewDidLoad()
fbl.delegate = self
loginView.readPermissions = ["public_profile", "email", "user_friends"]
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func loginViewShowingLoggedInUser(loginView: FBLoginView) {
logStatusTxt.text = "You are logged in!"
}
func loginViewFetchedUserInfo(loginView: FBLoginView?, user: FBGraphUser) {
profilePictureView.profileID = user.objectID
userNameTxt.text = user.first_name + " " + user.last_name
}
func loginViewShowingLoggedOutUser(loginView: FBLoginView?) {
profilePictureView.profileID = nil
userNameTxt.text = ""
logStatusTxt.text = "You are logged out!"
}
}
@stirman
Copy link

stirman commented Jun 21, 2014

Line #35, getting "Can't unwrap Optional.None" (XCode 6, beta 2)

Any idea? Thanks so much for this!

@iaserrat
Copy link
Author

Did you create the outlets from the storyboard?

@stirman
Copy link

stirman commented Jun 23, 2014

Ah, no... trying to integrate this with an app not using storyboards.

@stirman
Copy link

stirman commented Jun 23, 2014

Got it figured out. Maybe I'll post a version w/out storyboards.

@jonathanpeterwu
Copy link

@iaserrat could you clarify on referencing the correct outlets from the storyboard. I've been able to link the username, profile picture, and log status elements but am getting an error on line #35 " can't unwrap optional.none"

    loginView.readPermissions = ["public_profile", "email", "user_friends"]

@bagvendt
Copy link

This code crashes on Xcode 6 Beta 3, but works fine on Beta 2.

Relevant stack trace:

1.  While type-checking 'APIController' at /Users/bagvendt/projects/locy-swift/locy-swift/APIController.swift:30:1
2.  While resolving type FBGraphUser at [/Users/bagvendt/projects/locy-swift/locy-swift/APIController.swift:168:45 - line:168:45] RangeText="F"
<unknown>:0: error: unable to execute command: Segmentation fault: 11

@parruda
Copy link

parruda commented Jul 12, 2014

FINALLY someone with the same problem haha.
I tested on xcode beta 1 and 3, same problem. Downloading beta 2 now...

@parruda
Copy link

parruda commented Jul 12, 2014

I don't get the same error on Xcode6, however, the app will stop running with:
dyld: lazy symbol binding failed: Symbol not found: __TFSsg3nilVSs4_Nil

@pocketkk
Copy link

Same issue. Originally worked fine with beta 2, doesn't work with beta 3. Tried reopening in beta 2 and got different errors. Beta 3 must of made changes and now i can't go back :(

@iaserrat
Copy link
Author

@pocketkk Exact issue here. I've already submitted a bug report to apple...

@rodmoreno
Copy link

This guide helped me a lot. But when I want to declare the delegate of Facebook in UIViewController no longer compiles and Xcode (beta 3) begins to fail with the following warning: SourceKitService Terminated

It's so sad :(

@iaserrat
Copy link
Author

Can you guys help me to test if this is working on Beta 4? I think it is.

@thomasle
Copy link

In this code, I don't understand why you have to declare
var fbl: FBLoginView = FBLoginView()
and the
fbl.delegate = self
is then not needed.
I would have set the
loginView.delegate = self
instead

and in the AppDelegate.swift
func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
// Override point for customization after application launch.
FBLoginView.self()
return true
}

@amichalski
Copy link

@iaserrat, checked, works in Beta4

@parruda
Copy link

parruda commented Jul 28, 2014

It works on beta 4!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment