Firebase UI Auth提供程序iOS Swift示例 [英] Firebase UI Auth Provider iOS Swift Example

查看:235
本文介绍了Firebase UI Auth提供程序iOS Swift示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在启动一个新的iOS Swift应用程序,并希望使用FirebaseUI Auth。以下是在 Drop-in认证解决方案 Firebase身份验证。 FirebaseUI Auth for Android非常简单和容易。看起来,iOS的例子已经过时了,因为API在版本之间似乎已经发生了巨大的变化。看起来他们的版本是 3.1

这个指示也是一点点: https ://github.com/firebase/FirebaseUI-iOS



有人可以帮助我,并提供一个示例AppDelegate和ViewController的Facebook和谷歌登录?



我使用的是Xcode 8.3,Swift 3.

Podfile:

 #取消注释下一行为您的项目定义一个全局平台
platform:ios,'9.0'

target'Project 'do
#如果你不使用Swift而不想使用动态框架,请注意下一行
use_frameworks!

pod'FirebaseUI','〜> Firebase / Core'
pod'Firebase / Core'
pod'Firebase /数据库'
pod'Firebase / Auth'
pod'Firebase /存储'
pod'GoogleSignIn'
pod'FBSDKLoginKit'


target'ProjectTests'do
inherit! :search_paths
#用于测试的单元
结束

目标'ProjectUITests'做
继承! :search_paths
#用于测试的单元
结束
$ b结束

这是我的AppDelegate

 导入UIKit 
导入CoreData
导入Firebase
导入FirebaseAuthUI
导入FirebaseAuth
导入GoogleSignIn
导入FBSDKLoginKit
$ b @UIApplicationMain $ b $ class AppDelegate:UIResponder,UIApplicationDelegate {

var window :UIWindow?

func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - > Bool {
//在应用程序启动后替代自定义点。
FIRApp.configure()
返回true
}

func applicationWillResignActive(_ application:UIApplication){
//当应用程序即将从活动状态转移到非活动状态。对于某些类型的临时中断(例如来电或SMS消息)或用户退出应用程序并开始转换到后台状态时,可能会发生这种情况。
//使用此方法暂停正在进行的任务,禁用定时器,并使图形呈现回调无效。游戏应该使用这种方法来暂停游戏。

$ b $ func applicationDidEnterBackground(_ application:UIApplication){
//使用此方法释放共享资源,保存用户数据,使计时器无效并将足够的应用程序状态信息存储到如果稍后终止,则将应用程序恢复到当前状态。
//如果您的应用程序支持后台执行,则调用此方法而不是applicationWillTerminate:当用户退出时。


func applicationWillEnterForeground(_ application:UIApplication){
//作为从背景转换到活动状态的一部分;在这里你可以撤消进入背景的许多变化。


func applicationDidBecomeActive(_ application:UIApplication){
//在应用程序处于非活动状态时,重新启动暂停(或尚未启动)的任何任务。如果应用程序以前位于后台,则可以选择刷新用户界面。





$ p $这些是我的ViewController

 导入UIKit 
导入Firebase
导入FirebaseAuth
导入FirebaseAuthUI
导入FirebaseDatabaseUI
导入FirebaseGoogleAuthUI
导入FirebaseFacebookAuthUI
导入FBSDKCoreKit
导入FBSDKLoginKit
$ b $ class ViewController:UIViewController,FUIAuthDelegate {

var kFacebookAppID =111111111111111
重写func viewDidLoad(){
super.viewDidLoad()

//FIRApp.configure()
checkLoggedIn()
}

func checkLoggedIn(){
FIRAuth.auth()?. addStateDidChangeListener {auth,user
if user!= nil {
//用户登录
} else {
//没有用户登录
self.login()
}
}
}

func login (){
let authUI = FUIAuth.defaultAuthUI()
let FacebookProvider = F UIGoogleAuth()
let googleProvider = FUIFvertiseAuth()
authUI?.delegate = self
authUI?.providers = [googleProvider,facebookProvider]
let authViewController = authUI?.authViewController()
self.present(authViewController!,animated:true,completion:nil)
}
$ b @IBAction func logoutUser(_ sender:AnyObject){
try! FIRAuth.auth()!. signOut()
}

func authUI(_ authUI:FUIAuth,didSignInWith user:FIRUser ?,错误:错误?){
if error! =无{
//签入
login()
}的问题else {
//用户在!这是我们在登录



$ b code $ <$ pre

解决方案

你非常接近! Wei Jay是正确的,你需要在你的应用程序Info.plist文件中定义你的URL方案,并在你的应用程序委托中添加回调。

基本上你需要添加
$ b

 < key> CFBundleURLTypes< / key> 
< array>
< dict>
< key> CFBundleURLSchemes< / key>
< array>
< string> com.googleusercontent.apps。{app-id-here}< / string>
< / array>
< / dict>
< dict>
< key> CFBundleURLSchemes< / key>
< array>
< string> fb {app-id-here}< / string>
< / array>
< / dict>
< / array>
< key> FacebookAppID< / key>
< string> {app-id-here}< / string>
< key> FacebookDisplayName< / key>
< string> {name-here}< / string>
< key> LSApplicationQueriesSchemes< / key>
< array>
< string> fbauth2< / string>
< / array>

您可以从 RESERVED_CLIENT_ID

  func application(_ app:UIApplication,open url:URL,options:[UIApplicationOpenURLOptionsKey:Any] = [:]) - > Bool {

let googleSignIn = GIDSignIn.sharedInstance()。handle(url,sourceApplication:options [UIApplicationOpenURLOptionsKey.sourceApplication] as?String,annotation:options [UIApplicationOpenURLOptionsKey.annotation])

让facebookSignIn = FBSDKApplicationDelegate.sharedInstance()。application(app,open:url,sourceApplication:options [UIApplicationOpenURLOptionsKey.sourceApplication] as?String,annotation:options [UIApplicationOpenURLOptionsKey.annotation])

return googleSignIn || facebookSignIn

解释这里关于如何设置Facebook的设置

这个家伙这里提供了一个很好的例子他的Firebase身份验证UI的实现


I am starting a new iOS Swift application and want to use FirebaseUI Auth. Here is the link to the docs where it talks about it under Drop-in authentication solution Firebase Auth. The FirebaseUI Auth for Android was very simple and easy. It appears that the iOS examples are out of date as there API seems to have changed drastically between versions. It looks like they are on version 3.1.

The directions are also a little bare: https://github.com/firebase/FirebaseUI-iOS

Can someone please help me out and provide an example AppDelegate and ViewController for facebook and google login?

I am using Xcode 8.3, Swift 3.

Podfile:

# Uncomment the next line to define a global platform for your project
platform :ios, '9.0'

target 'Project' do
  # Comment the next line if you're not using Swift and don't want to use dynamic frameworks
  use_frameworks!

  pod 'FirebaseUI', '~> 3.1'
  pod 'Firebase/Core'
  pod 'Firebase/Database'
  pod 'Firebase/Crash'
  pod 'Firebase/Auth'
  pod 'Firebase/Storage'
  pod 'GoogleSignIn'
  pod 'FBSDKLoginKit'


  target 'ProjectTests' do
    inherit! :search_paths
    # Pods for testing
  end

  target 'ProjectUITests' do
    inherit! :search_paths
    # Pods for testing
  end

end

Here is my AppDelegate

import UIKit
import CoreData
import Firebase
import FirebaseAuthUI
import FirebaseAuth
import GoogleSignIn
import FBSDKLoginKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    FIRApp.configure()
    return true
}

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 invalidate graphics rendering callbacks. 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 active 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.
}

}

Here is my ViewController

import UIKit
import Firebase
import FirebaseAuth
import FirebaseAuthUI
import FirebaseDatabaseUI
import FirebaseGoogleAuthUI
import FirebaseFacebookAuthUI
import FBSDKCoreKit
import FBSDKLoginKit

class ViewController: UIViewController, FUIAuthDelegate {

    var kFacebookAppID = "111111111111111"
override func viewDidLoad() {
    super.viewDidLoad()

    //FIRApp.configure()
    checkLoggedIn()
}

func checkLoggedIn() {
    FIRAuth.auth()?.addStateDidChangeListener { auth, user in
        if user != nil {
            // User is signed in.
        } else {
            // No user is signed in.
            self.login()
        }
    }
}

func login() {
    let authUI = FUIAuth.defaultAuthUI()
    let facebookProvider = FUIGoogleAuth()
    let googleProvider = FUIFacebookAuth()
    authUI?.delegate = self
    authUI?.providers = [googleProvider, facebookProvider]
    let authViewController = authUI?.authViewController()
    self.present(authViewController!, animated: true, completion: nil)
}

@IBAction func logoutUser(_ sender: AnyObject) {
    try! FIRAuth.auth()!.signOut()
}

func authUI(_ authUI: FUIAuth, didSignInWith user: FIRUser?, error: Error?) {
    if error != nil {
        //Problem signing in
        login()
    }else {
        //User is in! Here is where we code after signing in

    }
}
}

解决方案

You're very close! Wei Jay was right in that you need to define your URL schemes inside your apps Info.plist file, and add the callback in your app delegate.

Essentially you need to add the below to the root of the plist.

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>com.googleusercontent.apps.{app-id-here}</string>
        </array>
    </dict>
    <dict>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>fb{app-id-here}</string>
        </array>
    </dict>
</array>
<key>FacebookAppID</key>
<string>{app-id-here}</string>
<key>FacebookDisplayName</key>
<string>{name-here}</string>
<key>LSApplicationQueriesSchemes</key>
<array>
    <string>fbauth2</string>
</array>

You can get your google app id from the RESERVED_CLIENT_ID entry in your GoogleService-Info.plist file.

Next, my AppDelegate file just implements the openURL delegate method:

func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {

    let googleSignIn = GIDSignIn.sharedInstance().handle(url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])

    let facebookSignIn = FBSDKApplicationDelegate.sharedInstance().application(app, open: url, sourceApplication: options[UIApplicationOpenURLOptionsKey.sourceApplication] as? String, annotation: options[UIApplicationOpenURLOptionsKey.annotation])

    return googleSignIn || facebookSignIn
}

Explaination here for how to set up settings for Facebook

This dude here has provided a nice example of his implementation of Firebase Auth UI

这篇关于Firebase UI Auth提供程序iOS Swift示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆