我想用新的Firebase实现Facebook登录,但我无法做到正确。
我添加了cocoapods,浏览了Firebase和Facebook的文档。当我打开我的模拟器时,我得到了这个:http://d.pr/i/bVPZ/4IBJKx9T
这是我的ViewController:
import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
import Firebase
class ViewController: UIViewController, FBSDKLoginButtonDelegate {
let loginButton: FBSDKLoginButton = FBSDKLoginButton()
override func viewDidLoad() {
super.viewDidLoad()
// Optional: Place the button in the center of your view.
loginButton.center = self.view.center
loginButton.readPermissions = ["public_profile", "email", "user_friends"]
loginButton.delegate = self
view!.addSubview(loginButton)
}
func loginButton(loginButton: FBSDKLoginButton!, didCompleteWithResult result: FBSDKLoginManagerLoginResult!, error: NSError?) {
if let error = error {
print(error.localizedDescription)
return
}
print("User logged in")
}
func loginButtonDidLogOut(loginButton: FBSDKLoginButton!) {
print("User logged out")
}
func loginButtonWillLogin(loginButton: FBSDKLoginButton!) -> Bool {
print("User will login")
return true
}
}
这是我的AppDelegate:
import UIKit
import Firebase
import FBSDKLoginKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
FIRApp.configure()
FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
return true
}
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
let handled: Bool = FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
return handled
}
func applicationWillResignActive(application: UIApplication) {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
func applicationDidEnterBackground(application: UIApplication) {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
func applicationWillEnterForeground(application: UIApplication) {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
func applicationDidBecomeActive(application: UIApplication) {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
FBSDKAppEvents.activateApp()
}
func applicationWillTerminate(application: UIApplication) {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
}
答案 0 :(得分:1)
将此作为else添加到您的loginButton:didCompleteWithResult方法。它完成了Firebase端的登录。虽然,它不能解释您当前的错误。
let credential = FIRFacebookAuthProvider.credentialWithAccessToken(FBSDKAccessToken.currentAccessToken().tokenString)
FIRAuth.auth()?.signInWithCredential(credential, completion: { (user, error) in
if let error = error {
print(error)
} else {
self.signedIn(user)
}
})
至于当前错误,您是否在为info.plist创建应用时添加了facebook在开发者控制台中提供的网址方案,应用ID和应用名称?在我看来,这就是它失败的地方。您的应用需要获得Facebook的许可才能登录。
答案 1 :(得分:0)
我得到了它的工作。
这就是问题:
func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
return FBSDKApplicationDelegate.sharedInstance().application(application, openURL: url, sourceApplication: sourceApplication, annotation: annotation)
}