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.
Add the following to your Podfile
:
pod 'FirebaseUI/Auth'
pod 'FirebaseUI/Phone'
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.
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];
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];
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:
- Add
APNS Key
orAPNS cert
to Firebase console project.
IfAPNS cert
is used than check that you uploaded certificate with the samebundleID
as Firebase iOS appbundleID
. - In the Xcode
Project settings
->Capabilities
enablePush Notifications
- In the project
Info.plist
set toNO
value ofFirebaseAppDelegateProxyEnabled
(add this key if needed) - 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. - In the
AppDelegate
application:didReceiveRemoteNotification:fetchCompletionHandler:
call[FUIAuth.defaultAuthUI.auth canHandleNotification:userInfo]
- In the
AppDelegate
application:didFinishLaunchingWithOptions:
call[FIRApp configure]
- In the
AppDelegate
application:openURL:options:
return[FUIAuth.defaultAuthUI handleOpenURL:url sourceApplication:sourceApplication]
- Add
REVERSED_CLIENT_ID
as URL scheme toProject settings
- Add
GoogleService-Info.plist
to your project - 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
}