Home United States USA — software Get Started With Firebase for iOS Apps

Get Started With Firebase for iOS Apps

251
0
SHARE

Firebase acts as a remote database to use in iOS app development. Learn how to configure it and use it for user authentication and more in this tutorial.
An iOS app needs the backend services for performing the various task like storing data in the database, hosting of files, caching data, monitoring etc. Firebase is a cloud-based service that provides most of the server side things for our iOS apps so that we don’t have to manage the infrastructure like databases, servers, and server-side things by yourself. Firebase will take care of everything. Firebase provides many useful services that make our life easier, but the most important services are
In this post, we will see how to get started with Firebase and how to configure the database and authentication services provided by Firebase. Let’s get started
Here, we need to specify the Bundle ID of our iOS app, which has to be unique. I have created com.sashikantjagtap. XCTalk for my personal project.
There is a little bug in Xcode where you need to check that the GoogleService-Info.plist file has the correct target membership, as we have to explicitly add it for some reason.
The next step is to configure CocoaPods that comes with Firebase.
We basically need the Firebase Database and Authentication mechanism, so add those to the Podfile.
Then install the pod using the pod install command.
The final step is to configure our iOS app to use Firebase. We can modify the AppDelegate. Swift file to use Firebase in our app.
That is everything needed and everything is well documented in the setup guide for Firebase. I have followed all the steps for the demo app. Now that, the interesting part is to use the Firebase and its services inside our App. At the end, you will see your app in the Firebase console.
Now that we have done all the setup for the iOS app to use Firebase Database and Authentication, let’s try to create the database and add a dummy message to test that the connection is working.
Let’s add the following code in the AppDelegate. Swift file just below where we have configured Firebase, i.e. FirebaseApp.configure() .
Now run the app and see if you can write to Firebase Database.
Probably not- you will get a permission denied error in the Xcode console, which indicates that we need to allow permission for our app to write to Firebase. So head over to the Firebase console and select Database Service and Rules and change it to something like this:
We are basically allowing everyone to write to the database for now. If you run the app, you can see that your message has been logged in the Cloud database. Yay!
We probably need to add more complex stuff to the database, not the just hello message. Let’s consider your app has a UI to add messages and we are adding messages and sender info to Firebase. We can create a child database and add it using code that looks something like this:
Let’s see how those values look like in the Firebase Database.
We can also retrieve the data from database using Firebase. Let’s see how to retrieve the sender and messages that we just added to the database.
We can do the more complex operations on the database, but for now, we will carry on with the next section: authentication of users.
Let’s consider our app has a UI to enter username and password for registration and login.
We can easily register users using the Firebase method createUser, like this:
Firebase will take care all the registration stuff internally.
Similar to registration, Firebase recognizes a registered user and allows them to sign in using the signIn method:
Firebase also has the method to log out users. It has the signOut method, which can be used like this:
You can see how easy it is to use Firebase for registering users to an app.
There are many other services that Firebase provides for iOS apps, but we will have to stop here, as you might have a basic understanding of how Firebase works in general.
Using cloud-based services like Firebase, we can simplify a lot of iOS app development activities and develop applications rapidly in the fast-paced market. However, you have to find the balance ofhow much you depend on the cloud. What are your experiences with using Firebase in production? Let me know in the comments. Thanks for reading.

Continue reading...