firebaseUI - get started

27 篇文章 0 订阅

转载方便查阅。


Get Started with Firebase Authentication on iOS

You can use Firebase Authentication to allow users to sign in to your app using one or more sign-in methods, including email address and password sign-in, and federated identity providers such as Google Sign-in and Facebook Login. This tutorial gets you started with Firebase Authentication by showing you how to add email address and password sign-in to your app.

Connect your app to Firebase

  1. Install the Firebase SDK.
  2. In the Firebase console, add your app to your Firebase project.

Add Firebase Authentication to your Xcode project

First, ensure the following dependency is in your project's Podfile:

 
 
pod 'Firebase/Auth'

Then, run pod install and open the created .xcworkspace file.

Initialize the Firebase SDK

In your app delegate, first import the Firebase SDK:

SWIFT

OBJECTIVE-C

 
  
@import Firebase;

Then, in the application:didFinishLaunchingWithOptions: method, initialize the FirebaseAppobject:

SWIFT

OBJECTIVE-C

 
  
// Use Firebase library to configure APIs
[FIRApp configure];

Listen for authentication state

For each of your app's views that need information about the signed-in user, attach a listener to the FIRAuth object. This listener gets called whenever the user's sign-in state changes.

Attach the listener in the view controller's viewWillAppear method:

SWIFT

OBJECTIVE-C

 
  
self.handle = [[FIRAuth auth]
    addAuthStateDidChangeListener
:^(FIRAuth *_Nonnull auth, FIRUser *_Nullable user) {
     
// ...
   
}];

And detach the listener in the view controller's viewWillDisappear method:

SWIFT

OBJECTIVE-C

 
  
[[FIRAuth auth] removeAuthStateDidChangeListener:_handle];

Sign up new users

Create a form that allows new users to register with your app using their email address and a password. When a user completes the form, validate the email address and password provided by the user, then pass them to the createUser method:

SWIFT

OBJECTIVE-C

 
  
[[FIRAuth auth] createUserWithEmail:email
                           password
:password
                         completion
:^(FIRUser *_Nullable user, NSError *_Nullable error) {
 
// ...
}];

Sign in existing users

Create a form that allows existing users to sign in using their email address and password. When a user completes the form, call the signIn method:

SWIFT

OBJECTIVE-C

 
  
[[FIRAuth auth] signInWithEmail:_emailField.text
                       password
:_passwordField.text
                     completion
:^(FIRUser *user, NSError *error) {
 
// ...
}];

Get user information

After a user signs in successfully, you can get information about the user. For example, in your authentication state listener:

SWIFT

OBJECTIVE-C

 
  
if (user) {
 
// The user's ID, unique to the Firebase project.
 
// Do NOT use this value to authenticate with your backend server,
 
// if you have one. Use getTokenWithCompletion:completion: instead.
 
NSString *uid = user.uid;
 
NSString *email = user.email;
  NSURL
*photoURL = user.photoURL;
 
// ...
}

Next steps

Learn how to add support for other identity providers and anonymous guest accounts:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值