Skip to content

Latest commit

 

History

History
110 lines (85 loc) · 4.58 KB

README.md

File metadata and controls

110 lines (85 loc) · 4.58 KB

FirebaseUI for iOS — Phone Auth

You can use Firebase Phone Authentication to sign in a user by sending an SMS message to the user's phone.The user signs in using a one-time code contained in the SMS message.

Table of Contents

  1. Installation
  2. Integration
  3. Customization
  4. Integration cheat sheet

Installation

Importing FirebaseUI Phone Auth components

Add the following to your Podfile:

 pod 'FirebaseUI/Auth'
 pod 'FirebaseUI/Phone'

Configuring sign-in provider

To use FirebaseUI to authenticate users you first need to configure each provider you want to use in their own developer app settings. Please read the Before you begin section of the Firebase Phone Auth configuration guides.

Using FirebasePhoneUI for Authentication

Integration

In order to use Phone Auth you should initialize Phone provider and add it to the list of FUIAuth providers. Please notice that you should use only one instance of Phone Auth providers. It can be retrieved form FUIAuth providers list.

// Swift
import FirebasePhoneAuthUI

/* ... */

FUIAuth.defaultAuthUI()?.delegate = self
let phoneProvider = FUIPhoneAuth.init(authUI: FUIAuth.defaultAuthUI()!)
FUIAuth.defaultAuthUI()?.providers = [phoneProvider]
// Objective-C
@import FirebasePhoneAuthUI;

/* ... */

[FUIAuth defaultAuthUI].delegate = self; // delegate should be retained by you!
FUIPhoneAuth *phoneProvider = [[FUIPhoneAuth alloc] initWithAuthUI:[FUIAuth defaultAuthUI]];
[FUIAuth defaultAuthUI].providers = @[phoneProvider];

Sign In

To start the authentication flow:

// Swift
let phoneProvider = FUIAuth.defaultAuthUI()?.providers.first as! FUIPhoneAuth
phoneProvider.signIn(withPresenting: currentlyVisibleController, phoneNumber: nil)
// Objective-C
FUIPhoneAuth *phoneProvider = [FUIAuth defaultAuthUI].providers.firstObject;
[phoneProvider signInWithPresentingViewController:currentlyVisibleController phoneNumber:nil];

Integration cheat sheet

Here you can find steps things that need to be checked in case of any issues with Firebase Phone Auth integration problems.

In case need to handle push notifications yourself:

  1. Add APNS Key or APNS cert to Firebase console project.
    If APNS cert is used than check that you uploaded certificate with the same bundleID as Firebase iOS app bundleID.
  2. In the Xcode Project settings -> Capabilities enable Push Notifications
  3. In the project Info.plist set to NO value of FirebaseAppDelegateProxyEnabled (add this key if needed)
  4. In the AppDelegate didRegisterForRemoteNotificationsWithDeviceToken call [FUIAuth.defaultAuthUI.auth setAPNSToken:deviceToken]
    In this case The type of the token (production or sandbox) will be attempted to be automatically detected. There is other method to set it manually.
  5. In the AppDelegate application:didReceiveRemoteNotification:fetchCompletionHandler: call [FUIAuth.defaultAuthUI.auth canHandleNotification:userInfo]
  6. In the AppDelegate application:didFinishLaunchingWithOptions: call [FIRApp configure]
  7. In the AppDelegate application:openURL:options: return [FUIAuth.defaultAuthUI handleOpenURL:url sourceApplication:sourceApplication]
  8. Add REVERSED_CLIENT_ID as URL scheme to Project settings
  9. Add GoogleService-Info.plist to your project
  10. In you controller call:
  [FUIAuth defaultAuthUI].delegate = self; // delegate should be retained by you!
  FUIPhoneAuth *phoneProvider = [[FUIPhoneAuth alloc] initWithAuthUI:[FUIAuth defaultAuthUI]];
  [FUIAuth defaultAuthUI].providers = \@[phoneProvider];

11. To start Phone Auth, please call:

  FUIPhoneAuth *phoneProvider = [FUIAuth defaultAuthUI].providers.firstObject;
  [phoneProvider signInWithPresentingViewController:self phoneNumber:nil];

You can skip all errors, FirbaseUI Phone Auth will display all error messages for you.
You may need only to handle error code FUIAuthErrorCodeUserCancelledSignIn.

If you don't need to handle APNS yourself, than don't do steps 3, 4, 5.

Please notice that you can use either APNS key OR APNS certificate.
One APNS Key can be used for all your iOS app from the same Apple Developer account.

12. (Optional) To receive the callback response after the user attempt to sign in.

func authUI(_ authUI: FUIAuth, didSignInWith authDataResult: AuthDataResult?, error: Error?) {
    // Do something with the response 
}