A simple iOS application that demonstrates how end-to-end encryption works with Firebase as a backend service for authentication and chat messaging. While this is a chat app, you can reuse it in any other apps to protect user data, documents, images.

1. Prerequisites

This demo uses several modules, including Virgil SDK and Firebase Firestore. These packages are distributed via CocoaPods. So, in this chapter we'll need to install Homebrew and use it to install Cocoapods.

1.1. Install Homebrew by opening Terminal and following instructions on the website

1.2. After it's installed, in the same Terminal type the following: 

brew update
brew install cocoapods

1.3. Using the Terminal go to the project folder (use cd to navigate)

1.4. Run pod install 

2. Firebase set up

We'll be using Xcode to set up and configure the Firebase backend.

2.1. Install Xcode
2.2. In Terminal run open Firebase\ Chat\ iOS.xcworkspace/ 

2.3.  Change bundleID of Xcode project to yours. It can be anything, but the structure is com.<company>.id .

2.4. Go to the Firebase console and create your own project.

2.5. Click "Get started" on "Authentication" panel.

2.6. Click "Sign in method" tab in the top.

2.7. Click Email/Password and turn on the Enable switch, then click Save.

2.8. Select "Database" panel and enable "Cloud Firestore".

2.9. Click on the "Rules" tab, paste:

service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth.uid != null;
    }
  }
}

and click "Publish".

2.10. Go to the "Project Settings".

2.11. Choose "Add Firebase to your iOS app"

and paste your Bundle Identifier from Xcode there.

2.12. Download the generated GoogleService-Info.plist file from Project Settings and copy it to the Firebase Chat iOSdirectory of this sample.

You can skip the rest steps in app registering tutorial in Firebase console. 

3. Cloud functions

Now you'll need to install the Firebase Cloud Functions in order to use them.

3.1 Now download ZIP archive or clone this repository from GitHub. Using Terminal access the folder of this repository (see step 1.3).

3.2. Download and install the latest Node.js if you don't have one yet.

3.3. Return to the Terminal and run npm cache verify 

3.4. Run npm install -g firebase-tools 

3.5. Run firebase login 

3.6. Make sure that you're in Demo Firebase Functions project folder in your Terminal. 

3.7. Run firebase init in the same folder

3.8. Select Functions: Configure and deploy Cloud Functions  with the SPACEBAR, then hit ENTER

3.9. Select your Virgil Firebase Demo Chat, press ENTER.

3.10. Select the following answers:

? What language would you like to use to write Cloud Functions? TypeScript
? Do you want to use TSLint to catch probable bugs and enforce style? Yes
? File functions/package.json already exists. Overwrite? No
? File functions/tslint.json already exists. Overwrite? No
? File functions/tsconfig.json already exists. Overwrite? No
? File functions/src/index.ts already exists. Overwrite? No
? Do you want to install dependencies with npm now? Yes

3.11. Run

firebase functions:config:set virgil.apiprivatekey="YOUR_API_PRIVATE_KEY" virgil.appid="YOUR_APP_ID" virgil.apikeyid="YOUR_API_KEY_ID"

with your values from Virgil Dashboard instead of "YOUR_..._...".

3.12.  Run firebase deploy --only functions.
Note: While Cloud Functions are in Beta, this command may fail with an unexpected error (HTTP 503 "The service is currently unavailable" in the log file), in which case, simply try running it again.

3.13. Go to the Firebase console -> Functions tab and copy your function url from the Event column

3.14. Go to Xcode -> Firebase Chat iOS/Helpers/Virgil/VirgilHelper.swift and change variable jwtEndpoint to: 

https://YOUR_FUNCTION_URL.cloudfunctions.net/api/generate_jwt

4. Run the demo

Now your app is ready to go. Run it and enjoy the demo!

Need help?

Check out other articles of this section to find the answers to your questions.

Our developer support team is here to help you.  You can contact us via Intercom, find us on Twitter or send us email [email protected].

Also, get extra help from our support team on Slack.

Did this answer your question?