草庐IT

ios - Firebase UI Auth Provider iOS Swift 示例

coder 2023-09-23 原文

我正在启动一个新的 iOS Swift 应用程序并想使用 FirebaseUI Auth。这是文档的链接,它在 Drop-in authentication solution Firebase Auth 下讨论了它.适用于 Android 的 FirebaseUI Auth 非常简单易行。似乎 iOS 示例已过时,因为 API 似乎在版本之间发生了巨大变化。看起来他们的版本是 3.1 .

方向也有点简单:https://github.com/firebase/FirebaseUI-iOS

有人可以帮助我并提供一个用于 facebook 和 google 登录的示例 AppDelegate 和 ViewController 吗?

我正在使用 Xcode 8.3、Swift 3。

播客文件:

# 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

这是我的 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.
}

}

这是我的 View Controller

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

    }
}
}

最佳答案

您的代码看起来不错!要与 Google/Facebook/Twitter Auth 通信,您必须添加对 URL Scheme 的支持到你的应用程序。 CHECK THIS OUT!

func configureAuth() {
    // TODO: configure firebase authentication
    let provider: [FUIAuthProvider] = [FUIGoogleAuth(), FUIFaceBookAuth()]
    FUIAuth.defaultAuthUI()?.providers = provider 

    // listen for changes in the authorization state
    _authHandle = FIRAuth.auth()?.addStateDidChangeListener { (auth: FIRAuth, user: FIRUser?) in

        // check if there is a current user
        if let activeUser = user {
            // check if current app user is the current FIRUser
            if self.user != activeUser {
                // sign in
            }
        } else {
            // user must sign in
            self.loginSession()
        }
    }


}

func loginSession() {
    let authViewController = FUIAuth.defaultAuthUI()!.authViewController()
    self.present(authViewController, animated: true, completion: nil)
}

关于ios - Firebase UI Auth Provider iOS Swift 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42545249/

有关ios - Firebase UI Auth Provider iOS Swift 示例的更多相关文章

  1. ruby - 如何验证 IO.copy_stream 是否成功 - 2

    这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下

  2. Ruby 文件 IO 定界符? - 2

    我正在尝试解析一个文本文件,该文件每行包含可变数量的单词和数字,如下所示:foo4.500bar3.001.33foobar如何读取由空格而不是换行符分隔的文件?有什么方法可以设置File("file.txt").foreach方法以使用空格而不是换行符作为分隔符? 最佳答案 接受的答案将slurp文件,这可能是大文本文件的问题。更好的解决方案是IO.foreach.它是惯用的,将按字符流式传输文件:File.foreach(filename,""){|string|putsstring}包含“thisisanexample”结果的

  3. Get https://registry-1.docker.io/v2/: net/http: request canceled while waiting - 2

    1.错误信息:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:requestcanceledwhilewaitingforconnection(Client.Timeoutexceededwhileawaitingheaders)或者:Errorresponsefromdaemon:Gethttps://registry-1.docker.io/v2/:net/http:TLShandshaketimeout2.报错原因:docker使用的镜像网址默认为国外,下载容易超时,需要修改成国内镜像地址(首先阿里

  4. postman——集合——执行集合——测试脚本——pm对象简单示例02 - 2

    //1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json

  5. ruby - 为什么不能使用类IO的实例方法noecho? - 2

    print"Enteryourpassword:"pass=STDIN.noecho(&:gets)puts"Yourpasswordis#{pass}!"输出:Enteryourpassword:input.rb:2:in`':undefinedmethod`noecho'for#>(NoMethodError) 最佳答案 一开始require'io/console'后来的Ruby1.9.3 关于ruby-为什么不能使用类IO的实例方法noecho?,我们在StackOverflow上

  6. Ruby-vips 图像处理库。有什么好的使用示例吗? - 2

    我对图像处理完全陌生。我对JPEG内部是什么以及它是如何工作一无所知。我想知道,是否可以在某处找到执行以下简单操作的ruby​​代码:打开jpeg文件。遍历每个像素并将其颜色设置为fx绿色。将结果写入另一个文件。我对如何使用ruby​​-vips库实现这一点特别感兴趣https://github.com/ender672/ruby-vips我的目标-学习如何使用ruby​​-vips执行基本的图像处理操作(Gamma校正、亮度、色调……)任何指向比“helloworld”更复杂的工作示例的链接——比如ruby​​-vips的github页面上的链接,我们将不胜感激!如果有ruby​​-

  7. arrays - 如何在下面的示例中将两个值数组分组为 n 个值数组? - 2

    我已经有很多两个值数组,例如下面的例子ary=[[1,2],[2,3],[1,3],[4,5],[5,6],[4,7],[7,8],[4,8]]我想把它们分组到[1,2,3],[4,5],[5,6],[4,7,8]因为意思是1和2有关系,2和3有关系,1和3有关系,所以1,2,3都有关系我如何通过ruby​​库或任何算法来做到这一点? 最佳答案 这是基本Bron–Kerboschalgorithm的Ruby实现:classGraphdefinitialize(edges)@edges=edgesenddeffind_maximum_

  8. ruby - Google-api-ruby-client 翻译 API 示例 - 2

    很高兴看到google代码:google-api-ruby-client项目,因为这对我来说意味着Ruby人员可以使用GoogleAPI-s来完善代码。虽然我现在很困惑,因为给出的唯一示例使用Buzz,并且根据我的实验,Google翻译(v2)api的行为必须与google-api-ruby-client中的Buzz完全不同。.我对“Explorer”演示示例很感兴趣——但据我所知,它并不是一个探索器。它所做的只是调用一个Buzz服务,然后浏览它已经知道的关于Buzz服务的事情。对我来说,Explorer应该让您“发现”所公开的服务和方法/功能,而不一定已经知道它们。我很想听听使用这个

  9. ruby - 是否有 SproutCore 或 Cappuccino 的现场演示/示例应用程序 - 2

    在他们的网站上找不到任何内容。我主要只是想看看哪个值得一试(当然是RIA)。谢谢 最佳答案 SproutCoredemos 关于ruby-是否有SproutCore或Cappuccino的现场演示/示例应用程序,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/1419788/

  10. ruby - 如何设置自动测试以仅重新运行失败的 rspec 示例 - 2

    我对自动测试的工作方式的印象(基于cucumbergithubwiki和其他在线内容)是它应该重新运行红色示例,直到它们通过。我的问题是它会重新运行规范文件中找到失败示例的所有示例,包括通过的示例。我不想浪费时间在修复失败示例的同时重新运行通过的示例。是否可以配置自动测试以便仅运行失败的示例? 最佳答案 您需要rspec-retrygem。以下是文档中有关如何实现它的一些示例:将它应用到覆盖整个测试套件的configureblock中...RSpec.configuredo|config|config.verbose_retry=t

随机推荐