How to Setup Firebase
You might need to use Firebase for Firebase authentication of users in your app via phone number.
It might look a bit scary at first. But don’t panic and let’s check how to do this right step by step.
Firebase account and project registration
Follow these steps to register your Firebase account and create a Firebase project:
-
Register a Firebase account at Firebase console . You can use your Google account to authenticate at Firebase.
-
Click Create project
!> Note: If you have a Google project registered for your mobile app, select it from the Project name dropdown menu.
You can also edit your Project ID if you need. A unique ID is assigned automatically to each project. This ID is used in publicly visible Firebase features. For example, it will be used in database URLs and in your Firebase Hosting subdomain. If you need to use a specific subdomain, you can change it.
-
Fill in the fields required (Project name, Project ID, etc.) and click Continue.
-
Configure Google Analytics for your project and click Create project.
Then click Continue.
-
Select platform for which you need Firebase
-
Fill in the fields on Add Firebase to your iOS App screen and click Register App
!> Note: Please pay attention to add the bundle ID your app is using. It can be set only when you add an app to your Firebase project.
Connect Firebase SDK
For iOS projects Firebase has the following requirements to be added successfully:
- Xcode 9.0 or later
- An Xcode project for iOS 8 version or above
- For Swift projects - Swift 3.0 or later
- Your app’s bundle identifier
- CocoaPods 1.4.0 or later
- For Cloud Messaging:
- A physical iOS device
- Your Apple Developer account’s Apple Push Notification Authentication Key
- In Xcode you need to enable Push Notifications in App > Capabilities
Here is a step by step guide how to connect Firebase SDK to your iOS Project:
-
Download GoogleService-Info.plist config file. It can be also downloaded later if you need.
-
Add Firebase SDK
A. With CocoaPods
It is recommended using CocoaPods to install the libraries. CocoaPods can be installed according to this guide. You need to add the pods for the libraries you need to use.
-
If you don’t have an Xcode project yet, you need to create it.
-
Create a Podfile if you don’t have one. You can do that as follows:
-
!> Note: The above pod should add the libraries needed to make Firebase work in your iOS app as well as Google Analytics for Firebase. The list of available pods with their specifications can be found here.
- Install the pods of your choice and open your-project.xcworkspace file to see the project in Xcode.
- Download a GoogleService-Info.plist file from your Firebase account and add it to your app.
!> Note: Each bundle ID must be connected in Firebase. If you have several bundle IDs in your project, each of them can have its own GoogleService-Info.plist file.
B. Without CocoaPods
SDK frameworks can be imported directly without using CocoaPods as follows:
-
Download the framework SDK zip (since the file is about 500MB in size, it may take some time).
-
Unzip it and find README file.
-
Add the ObjC linker flag in your Other Linker Settings in your target’s build settings.
-
Add initialisation code:
- Import the Firebase module in your
UIApplicationDelegate
:
- Configure a FirebaseApp shared instance, typically in your application’s
application:didFinishLaunchingWithOptions:
method:
You are done now.
- Import the Firebase module in your
Firebase authentication
This option allows users in your app authenticate themselves via phone number. If you use this method for user authentication, the user receives an SMS with verification code and authenticates via that code in your app.
You need to follow these steps to add Firebase authentication to your iOS project:
-
Include the following pods in your Podfile:
-
Go to Firebase console >> Authentication >> Sign-in method section:
-
Enable Phone number sign-in method:
-
Enable app verification For Firebase phone authentication to work Firebase must be able to verify that phone number sign-in requests are coming from your app. There are two options how to verify it:
-
Silent APNs notifications
In order to enable APNS notifications for Firebase authentication follow these steps:
-
Enable push notifications for your project in Xcode
-
Upload your APNS certificate to Firebase. If you do not know how to create APNS certificate - please use our How to generate APNS certificate guide
-
-
reCAPTCHA verification
If sending silent push notifications fails (user has disabled background refresh or your app or you are testing on a simulator), Firebase uses reCAPTCHA verification to complete the phone log in.
In order to enable reCAPTCHA verification follow these steps:
-
Add custom URL schemes to your project in Xcode:
A. In your GoogleService-Info.plist configuration file find REVERSED_CLIENT_ID key and copy it.
B. Go to your project configuration >> Your Project name, then select your app from the Targets section, choose Info tab and open URL Types section.
C. Click + button and add your REVERSED_CLIENT_ID key to URL Schemes field.
-
(Optional) It is possible to customize the way your app presents
SFSafariViewController
orUIWebView
when displaying the reCAPTCHA to the user. For that purpose you need to create a custom class that conforms to theFIRAuthUIDelegate
protocol, and pass it toverifyPhoneNumber:UIDelegate:completion:
.
-
-
-
Call
verifyPhoneNumber:UIDelegate:completion:
passing to it the user’s phone number to request that Firebase send an authentication code to the user’s phone by SMS:!> Important note: As a best practice please do not forget to inform your users that if they use phone sign-in, they might receive an SMS message for verification and standard rates apply.
-
languageCode
property on your Auth instance allows specifying the auth language and therefore localize SMS message sent by Firebase: -
Save the verification ID and restore it when your app loads.
This measure should help you to have a valid verification ID if your app closes before the user can sign in (if the user checks SMS app, for instance).
A simple way is to save the verification ID with the
NSUserDefaults
object:Then, you can restore the saved value:
-
Create a
FIRPhoneAuthCredential
object using the verification code and the verification ID: -
Sign in the user with the FIRPhoneAuthCredential object:
-
Get Firebase
access_token
after SMS code verification as follows:
- Add ConnectyCube user sign in to your project as follows:
- Get your Project ID from your Firebase console
- Pass your Firebase
project_id
and Firebaseaccess_token
parameters tosignInUsingFirebasePhone
(logInWithFirebaseProjectID
v1 deprecated) method:
- Run your app to verify installation Once your user is logged in successfully, you will find him/her in your Dashboard >> Your App >> Users section.
So now you know how to use Firebase features in your ConnectyCube apps. If you have any difficulties - please let us know via support channel