FireUInoGoogle 0.1.1

FireUInoGoogle 0.1.1

测试已测试
语言语言 Obj-CObjective C
许可证 MIT
发布上次发布2016年2月

Eduard Ichim维护。



FireUInoGoogle 是没有 Google 静态库的 FirebaseUI for iOS — Firebase 的 UI 绑定

FireUInoGoogle 是一个开源库,用于 iOS,允许您快速将常见的 UI 元素连接到 Firebase 数据库以进行数据存储,允许视图在它们更改时实时更新,并为显示列表或物品集合等常见任务提供简单的接口。

此外,FireUInoGoogle 通过提供易于使用的认证方法简化了 Firebase 认证,这些方法类似于与 Facebook 和 Twitter 等常用身份提供程序集成,同时还允许开发人员使用内置的 headful UI 以便轻松开发。

安装 FireUInoGoogle for iOS

FireUInoGoogle 支持 iOS 8.0+。我们建议使用 CocoaPods,将以下内容添加到您的 Podfile

pod 'FireUInoGoogle'       # Pull in all FireUInoGoogle features

如果您不想使用 FireUInoGoogle 的所有功能,有两个 subspec,即 CoreAuth,可以分别安装完整特征集的子集。

pod 'FireUInoGoogle/Core'  # Only pull in the "Core" FireUInoGoogle features
pod 'FireUInoGoogle/Auth'  # Only pull in the "Auth" FireUInoGoogle features

如果您正在为 Swift 项目添加 FireUInoGoogle,请确保您还有

platform :ios, '8.0'
use_frameworks!

开始使用 Firebase

FireUInoGoogle 需要Firebase来存储位置数据。您可以在这里注册免费账户

FireUInoGoogle for iOS 快速入门

这是一个关于如何使用 FireUInoGoogle 的核心功能来加速 Firebase 用于 iOS 的开发的快速入门。

FireUInoGoogle 核心

提供核心数据绑定功能以及特定数据源的数据列表。有关更多信息,请跳转到 核心 API 概述

描述
FirebaseTableViewDataSource 数据源,将 Firebase 查询绑定到 UITableView
FirebaseCollectionViewDataSource 数据源,将 Firebase 查询绑定到 UICollectionView
FirebaseArray 将数组同步到 Firebase 查询
FirebaseDataSource 用于创建自定义数据源的通用超类

FireUInoGoogle Auth

提供认证提供者以及针对Facebook、Google、Twitter和Firebase电子邮件/密码的实体实现,以及一个完整的UI界面,用于处理认证状态和错误情况。更多信息请参阅Auth API概述

描述
FirebaseAuthProvider 认证提供者的通用超类
FirebaseFacebookAuthProvider 允许使用一种方法登录到Facebook
FirebaseTwitterAuthProvider 允许使用一种方法登录到Twitter
FirebasePasswordAuthProvider 允许使用一种方法登录到Firebase的电子邮件/密码认证
FirebaseLoginViewController 灵活的完整UI,用于处理来自所有身份提供者的登录、注销和错误情况

对于上述每个功能的更深入解释,请参阅下面的使用说明或阅读文档

FireUInoGoogle Core API

FirebaseTableViewDataSource

FirebaseTableViewDataSource实现了UITableViewDataSource协议,以便自动将Firebase用作你的UITableView的数据源。

Objective-C

YourViewController.h
...
@property (strong, nonatomic) Firebase *firebaseRef;
@property (strong, nonatomic) FirebaseTableViewDataSource *dataSource;
YourViewController.m
...
self.firebaseRef = [[Firebase alloc] initWithUrl:@"https://<YOUR-FIREBASE-APP>.firebaseio.com/"];
self.dataSource = [[FirebaseTableViewDataSource alloc] initWithRef:firebaseRef cellReuseIdentifier:@"<YOUR-REUSE-IDENTIFIER>" view:self.tableView];

[self.dataSource populateCellWithBlock:^(UITableViewCell *cell, FDataSnapshot *snap) {
  // Populate cell as you see fit, like as below
  cell.textLabel.text = snap.key;
}];

[self.tableView setDataSource:self.dataSource];

Swift

YourViewController.swift
...
let firebaseRef = Firebase(url:"https://<YOUR-FIREBASE-APP>.firebaseio.com/")
let dataSource: FirebaseTableViewDataSource!
...
self.dataSource = FirebaseTableViewDataSource(ref: self.firebaseRef, cellReuseIdentifier: "<YOUR-REUSE-IDENTIFIER>", view: self.tableView)

self.dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
  let snap = obj as! FDataSnapshot

  // Populate cell as you see fit, like as below
  cell.textLabel?.text = snap.key as String
}

self.tableView.dataSource = self.dataSource

FirebaseCollectionViewDataSource

FirebaseCollectionViewDataSource实现了UICollectionViewDataSource协议,以便自动将Firebase用作你的UICollectionView的数据源。

Objective-C

YourViewController.h
...
@property (strong, nonatomic) Firebase *firebaseRef;
@property (strong, nonatomic) FirebaseCollectionViewDataSource *dataSource;
YourViewController.m
...
self.firebaseRef = [[Firebase alloc] initWithUrl:@"https://<YOUR-FIREBASE-APP>.firebaseio.com/"];
self.dataSource = [[FirebaseTableViewDataSource alloc] initWithRef:firebaseRef cellReuseIdentifier:@"<YOUR-REUSE-IDENTIFIER>" view:self.CollectionView];

[self.dataSource populateCellWithBlock:^(UICollectionViewCell *cell, FDataSnapshot *snap) {
  // Populate cell as you see fit, like as below
  cell.backgroundColor = [UIColor blueColor];
}];

[self.collectionView setDataSource:self.dataSource];

Swift

YourViewController.swift
...
let firebaseRef = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com/")
let dataSource: FirebaseCollectionViewDataSource!
...
self.dataSource = FirebaseCollectionViewDataSource(ref: self.firebaseRef, cellReuseIdentifier: "<YOUR-REUSE-IDENTIFIER>", view: self.collectionView)

self.dataSource.populateCellWithBlock { (cell: UICollectionViewCell, obj: NSObject) -> Void in
  let snap = obj as! FDataSnapshot

  // Populate cell as you see fit, like as below
  cell.backgroundColor = UIColor.blueColor()
}

self.collectionView.dataSource = self.dataSource

自定义您的UITableView或UICollectionView

您可以使用FirebaseTableViewDataSourceFirebaseCollectionViewDataSource的几种方式来创建自定义UITableView或UICollectionView。有关如何创建自定义UITableView的更多信息,请查看以下TutsPlus教程。有关如何创建自定义UICollectionView的更多信息,特别是如何实现UICollectionViewLayout,请查看以下Objective-CSwift教程。

使用默认UI*ViewCell实现

您可以使用默认的UITableViewCellUICollectionViewCell实现来快速启动。对于UITableViewCell,这允许直接使用cell.textLabelcell.detailTextLabel。对于UICollectionViewCell,您需要向contentView添加子视图,以便使其有用。

Objective-C中的UITableView和UICollectionView与默认UI*ViewCell

self.dataSource = [[FirebaseTableViewDataSource alloc] initWithRef:firebaseRef cellReuseIdentifier:@"<YOUR-REUSE-IDENTIFIER>" view:self.tableView];

[self.dataSource populateCellWithBlock:^(UITableViewCell *cell, FDataSnapshot *snap) {
  // Populate cell as you see fit, like as below
  cell.textLabel.text = snap.key;
}];

[self.tableView setDataSource:self.dataSource];
self.dataSource = [[FirebaseCollectioneViewDataSource alloc] initWithRef:firebaseRef cellReuseIdentifier:@"<YOUR-REUSE-IDENTIFIER>" view:self.CollectionView];

[self.dataSource populateCellWithBlock:^(UICollectionViewCell *cell, FDataSnapshot *snap) {
  // Populate cell as you see fit by adding subviews as appropriate
  [cell.contentView addSubview:customView];
}];

[self.collectionView setDataSource:self.dataSource];

Swift中的UITableView和UICollectionView与默认UI*ViewCell

self.dataSource = FirebaseTableViewDataSource(ref: firebaseRef cellReuseIdentifier: @"<YOUR-REUSE-IDENTIFIER>" view: self.tableView)

self.dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
  // Populate cell as you see fit, like as below
  cell.textLabel.text = snap.key;
}

self.tableView.dataSource = self.dataSource;
self.dataSource = FirebaseCollectionViewDataSource(ref: firebaseRef cellReuseIdentifier: @"<YOUR-REUSE-IDENTIFIER>" view: self.collectionView)

self.dataSource.populateCellWithBlock { (cell: UICollectionViewCell, obj: NSObject) -> Void in
  // Populate cell as you see fit by adding subviews as appropriate
  cell.contentView.addSubview(customView)
}

self.collectionView.dataSource = self.dataSource;

使用Storyboard和原型单元格

创建一个包含 UITableViewControllerUICollectionViewControllerUIViewController(带有 UITableViewUICollectionView)的故事板。将原型单元格拖到 UITableViewUICollectionView 中,并为其分配一个匹配在实例化 Firebase*ViewDataSource 时所使用的重用标识符的自定义重用标识符。在使用原型单元格时,务必使用 prototypeReuseIdentifier 而不是 cellReuseIdentifier

将其他属性拖动到单元格中,并将它们关联到 UITableViewCellUICollectionViewCell 子类的属性。代码样本与上述类似。

使用 UI*ViewCell 的自定义子类

创建一个自定义的 UITableViewCellUICollectionViewCell 子类,可以选择性地包含 XIB 文件。确保调用 -initWithStyle: reuseIdentifier: 类初始化方法来实例化 UITableViewCell,或者调用 -initWithFrame: 类初始化方法来实例化 UICollectionViewCell。然后,您可以将自定义类连接到 FirebaseTableViewDataSource 的实现。

Objective-C 中的 UITableView 和 UICollectionView 使用 UI*ViewCell 的自定义子类

self.dataSource = [[FirebaseTableViewDataSource alloc] initWithRef:firebaseRef cellClass:[YourCustomClass class] cellReuseIdentifier:@"<YOUR-REUSE-IDENTIFIER>" view:self.tableView];

[self.dataSource populateCellWithBlock:^(YourCustomClass *cell, FDataSnapshot *snap) {
  // Populate custom cell as you see fit, like as below
  cell.yourCustomLabel.text = snap.key;
}];

[self.tableView setDataSource:self.dataSource];
self.dataSource = [[FirebaseCollectioneViewDataSource alloc] initWithRef:firebaseRef cellClass:[YourCustomClass class] cellReuseIdentifier:@"<YOUR-REUSE-IDENTIFIER>" view:self.CollectionView];

[self.dataSource populateCellWithBlock:^(YourCustomClass *cell, FDataSnapshot *snap) {
  // Populate cell as you see fit
  cell.customView = customView;
}];

[self.collectionView setDataSource:self.dataSource];

Swift 中的 UITableView 和 UICollectionView 使用 UI*ViewCell 的自定义子类

self.dataSource = FirebaseTableViewDataSource(ref: firebaseRef cellClass: YourCustomClass.self cellReuseIdentifier: @"<YOUR-REUSE-IDENTIFIER>" view: self.tableView)

self.dataSource.populateCellWithBlock { (cell: YourCustomClass, obj: NSObject) -> Void in
  // Populate cell as you see fit, like as below
  cell.yourCustomLabel.text = snap.key;
}

self.tableView.dataSource = self.dataSource;
self.dataSource = FirebaseCollectionViewDataSource(ref: firebaseRef cellClass: YourCustomClass.self cellReuseIdentifier: @"<YOUR-REUSE-IDENTIFIER>" view: self.collectionView)

self.dataSource.populateCellWithBlock { (cell: YourCustomClass, obj: NSObject) -> Void in
  // Populate cell as you see fit
  cell.customView = customView;
}

self.collectionView.dataSource = self.dataSource;

使用自定义的 XIB

创建一个自定义的 XIB 文件,并将其连接到原型单元格。然后,您可以使用这个文件就像使用其他任何 UITableViewCell 一样,要么使用自定义标记,要么使用与 XIB 关联的自定义类。

Objective-C 中的 UITableView 和 UICollectionView 使用自定义 XIB

self.dataSource = [[FirebaseTableViewDataSource alloc] initWithRef:firebaseRef nibNamed:@"<YOUR-XIB>" cellReuseIdentifier:@"<YOUR-REUSE-IDENTIFIER>" view:self.tableView];

[self.dataSource populateCellWithBlock:^(UITableViewCell *cell, FDataSnapshot *snap) {
  // Use tags to populate custom properties, or use properties of a custom cell, if applicable
  UILabel *yourCustomLabel = (UILabel *)[cell.contentView viewWithTag:<YOUR-TAG>];
  yourCustomLabel.text = snap.key
}];

[self.tableView setDataSource:self.dataSource];
self.dataSource = [[FirebaseCollectionViewDataSource alloc] initWithRef:firebaseRef nibNamed:@"<YOUR-XIB>" cellReuseIdentifier:@"<YOUR-REUSE-IDENTIFIER>" view:self.collectionView];

[self.dataSource populateCellWithBlock:^(UICollectionViewCell *cell, FDataSnapshot *snap) {
  // Use tags to populate custom properties, or use properties of a custom cell, if applicable
  UILabel *yourCustomLabel = (UILabel *)[cell.contentView viewWithTag:<YOUR-TAG>];
  yourCustomLabel.text = snap.key
}];

[self.tableView setDataSource:self.dataSource];

Swift 中的 UITableView 和 UICollectionView 使用自定义 XIB

self.dataSource = FirebaseTableViewDataSource(ref: firebaseRef nibNamed: "<YOUR-XIB>" cellReuseIdentifier: @"<YOUR-REUSE-IDENTIFIER>" view: self.tableView)

self.dataSource.populateCellWithBlock { (cell: UITableViewCell, obj: NSObject) -> Void in
  // Use tags to populate custom properties, or use properties of a custom cell, if applicable
  let yourCustomLabel: UILabel = cell.contentView.viewWithTag(<YOUR-TAG>) as! UILabel
  yourCustomLabel.text = snap.key
}

self.tableView.dataSource = self.dataSource;
self.dataSource = FirebaseCollectionViewDataSource(ref: firebaseRef cellClass: YourCustomClass.self cellReuseIdentifier: @"<YOUR-REUSE-IDENTIFIER>" view: self.collectionView)

self.dataSource.populateCellWithBlock { (cell: YourCustomClass, obj: NSObject) -> Void in
  // Use tags to populate custom properties, or use properties of a custom cell, if applicable
  let yourCustomLabel: UILabel = cell.contentView.viewWithTag(<YOUR-TAG>) as! UILabel
  yourCustomLabel.text = snap.key
}

self.collectionView.dataSource = self.dataSource;

理解 FirebaseUI Core 内部原理

FirebaseUI 包含几个构建模块,开发者在构建 FirebaseUI 上层功能之前应该了解它们,包括同步数组 FirebaseArray 和一个泛型数据源超类 FirebaseDataSource,它派生了 FirebaseTableViewDataSourceFirebaseCollectionViewDataSource 或其他自定义视图类。

FirebaseArrayFirebaseArrayDelegate 协议

FirebaseArray 是绑定 Firebase 引用与数组的同步数组。它通过 FirebaseArrayDelegate 协议传递 Firebase 事件。通常建议开发者在没有通过自定义数据源进行路由的情况下不要直接访问 FirebaseArray,但如果确实需要这样做,可以查看以下 FirebaseDataSource

Objective-C

Firebase *firebaseRef = [[Firebase alloc] initWithUrl:@"https://<YOUR-FIREBASE-APP>.firebaseio.com/"];
FirebaseArray *array = [[FirebaseArray alloc] initWithRef:firebaseRef];

Swift

let firebaseRef = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com/")
let array = FirebaseArray(ref: firebaseRef)

FirebaseDataSource

FirebaseDataSource充当通用数据源,通过提供通用信息(例如数据源中对象的数量)以及要求子类根据视图适当实现FirebaseArrayDelegate方法,在其中扮演角色。此类不应被实例化,但在创建具有特定视图的特定适配器时应当被继承。《FirebaseTableViewDataSource》[链接](https://github.com/firebase/FirebaseUI-iOS/blob/master/FirebaseUI/Implementation/FirebaseTableViewDataSource.m)和《FirebaseCollectionViewDataSource》[链接](https://github.com/firebase/FirebaseUI-iOS/blob/master/FirebaseUI/Implementation/FirebaseCollectionViewDataSource.m)是此类用法的例子。本质上,FirebaseDataSource是一个围绕FirebaseArray的包装器。

FireUInoGoogle Auth API

FirebaseAppDelegate

FirebaseAppDelegate是标准AppDelegate的替代品,提供了使认证工作所需的功能。如果您计划使用FirebaseUI认证功能,则您的AppDelegate应该像这样继承自FirebaseAppDelegate:

Objective-C

// AppDelegate.h
#import <UIKit/UIKit.h>
#import <FirebaseUI/FirebaseAppDelegate.h>

@interface AppDelegate : FirebaseAppDelegate

@end

// AppDelegate.m
#import "AppDelegate.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [super application:application didFinishLaunchingWithOptions:launchOptions];
  // Override point for customization after application launch.

  return YES;
}
...

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url sourceApplication:(nullable NSString *)sourceApplication annotation:(nonnull id)annotation {
  [super application:app openURL:url sourceApplication:sourceApplication annotation:annotation];
  // Override point for customization.

  return YES;
}

@end

Swift

import UIKit
import FireUInoGoogle

@UIApplicationMain
class AppDelegate: FirebaseAppDelegate {

  func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    super.application(application, launchOptions);
    // Override point for customization after application launch.

    return true
  }
  ...

  func application(application: UIApplication, openURL url: NSURL, sourceApplication: String?, annotation: AnyObject) -> Bool {
    super.application(application, url, sourceApplication, annotation);
    // Override point for customization.

    return true
  }
}

FirebaseLoginViewController

FirebaseLoginViewContoller快速为您应用程序添加了满屏UI流程。此流程支持电子邮件/密码登录以及社交提供商(Facebook、Google、Twitter)。使用此功能,您可以轻松引导用户完成登录过程,获取当前用户的状态,并注销用户。此控制器可用的两种主要场景:

  1. 捕获式登录门户:在应用启动时触发,开发者必须指定关闭行为
  2. 登录模态视图:在用户操作时触发,开发者可以指定关闭行为或使用内置的FirebaseUI行为

此视图的看起来是这样的:

FirebaseLoginViewController with all providers enabled

对于创建捕获式门户,我们建议在viewDidLoad中创建FirebaseLoginViewController并启用提供者,但不要在视图创建之前显示它(在viewWillAppear或之后!)。最好将FirebaseLoginViewController作为视图控制器的属性,以便在其他方法中保留并访问。

Objective-C

- (void)viewDidLoad {
  [super viewDidLoad];

  Firebase *firebaseRef = [[Firebase alloc] initWithUrl:@"https://<YOUR-FIREBASE-APP>.firebaseio.com/"];

  self.loginViewController = [[FirebaseLoginViewController alloc] initWithRef:firebaseRef];
  [self.loginViewController enableProvider:FAuthProviderFacebook];
  [self.loginViewController enableProvider:FAuthProviderTwitter];
  [self.loginViewController enableProvider:FAuthProviderPassword];
  // Scenario 1: Set up captive portal login flow
  [self.loginViewController didDismissWithBlock:^(FAuthData *user, NSError *error) {
    if (user) {
      // Handle user case
    } else if (error) {
      // Handle error case
    } else {
      // Handle cancel case
    }
  }];

  // Scenario 2: Set up user action based login flow
  [loginButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
  [logoutButton addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
}

// Scenario 1: Application launches login flow, handles dismissal and routing in `didDismissWithBlock:`
- (void)viewDidAppear:(BOOL)animated {
  if (![self.loginViewController currentUser]) {
    [self presentViewController:self.loginViewController animated:YES completion:nil];
  }
}

// Scenario 2: User action launches login flow, dismissal and routing handled by `FirebaseLoginViewController`
- (void)login {
  if (![self.loginViewController currentUser]) {
    [self presentViewController:self.loginViewController animated:YES completion:nil];
  }
}

- (void)logout {
  if ([self.loginViewController currentUser]) {
    [self.loginViewController logout];
  }
}

Swift

override func viewDidLoad() {
  super.viewDidLoad()

  let firebaseRef = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com/")

  self.loginViewController = FirebaseLoginViewController(ref: firebaseRef)
  self.loginViewController.enableProvider(FAuthProvider.Facebook)
  self.loginViewController.enableProvider(FAuthProvider.Twitter)
  self.loginViewController.enableProvider(FAuthProvider.Password)
  // Scenario 1: Set up captive portal login flow
  self.loginViewController.didDismissWithBlock { (user: FAuthData, error: NSError) -> Void in
    if (user) {
      // Handle user case
    } else if (error) {
      // Handle error case
    } else {
      // Handle cancel case
    }
  }

  // Scenario 2: Set up user action based login flow
  loginButton.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
  logoutButton.addTarget(self, action: "logout", forControlEvents: UIControlEvents.TouchUpInside)
}

// Scenario 1: Application launches login flow, handles dismissal and routing in `didDismissWithBlock:`
override func viewDidAppear(animated: Bool) {
  if (!self.loginViewController.currentUser()) {
    self.presentViewController(self.loginViewcontroller, animated: true, completion: nil)
  }
}

// Scenario 2: User action launches login flow, dismissal and routing handled by `FirebaseLoginViewController`
func login() {
  if (!self.loginViewController.currentUser()) {
    self.presentViewController(self.loginViewcontroller, animated: true, completion: nil)
  }
}

func logout() {
  if (self.loginViewController.currentUser()) {
    self.loginViewController.logout()
  }
}

FirebaseFacebookAuthProvider

FirebaseFacebookAuthProvider是Facebook登录的包装器。要启用此功能,请访问Firebase控制台中的认证选项卡,并选中复选框以启用此提供者,然后创建一个新的Facebook项目并遵循安装说明。您还必须在“Info.plist”中添加“FacebookAppID”和“FacebookDisplayName”键以及几个URL方案。有关设置信息的更多信息,请参阅Firebase Facebook认证文档

Objective-C

Firebase *firebaseRef = [[Firebase alloc] initWithUrl:@"https://<YOUR-FIREBASE-APP>.firebaseio.com/"];
FirebaseFacebookAuthProvider *facebookProvider = [[FirebaseFacebookAuthProvider alloc] initWithRef:firebaseRef authDelegate:self];
[facebookProvider login];
...
[facebookProvider logout];

Swift

let firebaseRef = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com/")
let facebookProvider = FirebaseFacebookAuthProvider(ref: firebaseRef, authDelegate: self)
facebookProvider.login()
...
facebookProvider.logout()

FirebaseTwitterAuthProvider

FirebasTwitterAuthProvider是一个围绕Twitter登录的包装器。为了启用它,请访问Firebase仪表盘的认证标签页,勾选复选框以启用此提供者,然后创建一个新的Twitter项目,并在该页面上输入您的Twitter API密钥和密码。您还必须将键"TwitterApiKey"添加到您的应用"Info.plist"中。有关设置的更多信息,请参阅FirebaseTwitter认证文档

Objective-C

Firebase *firebaseRef = [[Firebase alloc] initWithUrl:@"https://<YOUR-FIREBASE-APP>.firebaseio.com/"];
FirebaseTwitterAuthProvider *twitterProvider = [[FirebaseTwitterAuthProvider alloc] initWithRef:firebaseRef authDelegate:self twitterDelegate:self];
[twitterProvider login];
...
[twitterProvider logout];

Swift

let firebaseRef = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com/")
let twitterProvider = FirebaseTwitterAuthProvider(ref: firebaseRef, authDelegate: self, twitterDelegate: self)
twitterProvider.login()
...
twitterProvider.logout()

FirebasePasswordAuthProvider

FirebasePasswordAuthProvider是一个围绕Firebase电子邮件/密码登录的包装器。为了启用此功能,请访问Firebase仪表板的认证标签页,通过勾选复选框来启用此提供者。有关设置的更多信息,请参阅Firebase电子邮件/密码认证文档

Objective-C

Firebase *firebaseRef = [[Firebase alloc] initWithUrl:@"https://<YOUR-FIREBASE-APP>.firebaseio.com/"];
FirebasePasswordAuthProvider *passwordProvider = [[FirebasePasswordAuthProvider alloc] initWithRef:firebaseRef authDelegate:self];
[passwordProvider loginWithEmail:@"email" andPassword:@"password"];
...
[passwordProvider logout];

Swift

let firebaseRef = Firebase(url: "https://<YOUR-FIREBASE-APP>.firebaseio.com/")
let passwordProvider = FirebasePasswordAuthProvider(ref: firebaseRef, authDelegate: self)
passwordProvider.login(email: "email", password: "password")
...
passwordProvider.login()

了解FirebaseUI Auth的内部工作原理

FirebaseAuthProvider

FirebaseAuthProvider是所有身份提供者超类,提供默认构造函数[FirebaseAuthProvider initWithRef:authDelegate:]以及loginlogoutconfigureProvider方法以简化跨提供者的标准认证。由于在基本实现中没有实现loginconfigureProvider方法,因此在调用时将抛出异常,因此每个提供者都应该重写这些方法。`logout`已实现,用于取消给定Firebase引用的认证,并且始终应在任何子类实现的末尾用[super logout]调用。

FirebaseAuthProvider还注册了一个单例认证监听器,监控所有提供者的全局认证状态,并根据适当的路由authProvider:onLogin:onLogout事件。

FirebaseAuthDelegate和TwitterAuthDelegate协议

每个认证事件都通过FirebaseAuthDelegate进行连接,它有四个方法:

  1. [FirebaseAuthDelegate authProvider:onLogin:]
  2. [FirebaseAuthDelegate onLogout:]
  3. [FirebaseAuthDelegate authProvider:onUserError:]
  4. [FirebaseAuthDelegate authProvider:onProviderError:]

前两个方法,登录和注销,对于实现FirebaseAuthDelegate协议的类来说是必需的,而后两个方法是可选的,尽管强烈推荐。所有认证事件(无论提供者如何)都将通过这些方法进行。

一般来说,用户错误(如无效的密码或代表用户的认证请求取消)是可恢复的,并应提示用户重新进行认证,而提供者错误(例如配置不正确或提供者侧的问题)通常超出用户的控制,应引导用户走另一条路径(禁用提供者、临时禁用应用等)。

TwitterAuthDelegate作为处理同一设备上零个或多个Twitter账户的特殊情况包括在内,因为开发者需要提示用户创建Twitter账户(或使用手机登录),或从多个账户中选择。可以用于这些目的的[TwitterAuthDelegate createTwitterAccount][TwitterAuthDelegate selectTwitterAccount:]方法。

通过 FirebaseLoginViewController 创建自定义完整头部 UI

FirebaseLoginViewController 是基于 FirebaseUI 认证组件构建的简单完整头部 UI 的一种实现。此类包含不同提供者的 Provider 方法以及关于当前提供者(因此是用户)的状态,这允许从外部视图控制器同步调用 currentUserlogout,并将 FirebaseLoginViewController 作为认证状态的唯一真相来源。

FirebaseLoginViewController 中的所有 UI 元素都是可重新配置的(除了按钮颜色),因此根据您的应用程序配置 UI 不会很难。如果主题不合适,您可以自由地使用 FirebaseLoginViewController 的概念来创建自己的认证控制器。

许可证

此库是 FirebaseUI 的一个版本,移除了核心部分的 Google 集成,因此所有功劳都归功于 FirebaseUI。FireUInoGoogle 在 MIT 许可证下提供。有关更多信息,请参阅 LICENSE 文件。