Skip to main content

Overview

The TikTok Clone app uses three core Firebase services:

Firebase Storage

Stores video files uploaded by users

Cloud Firestore

NoSQL database for posts, users, and comments

Firebase Analytics

Tracks user engagement and app usage
This guide will walk you through setting up each service from scratch.

Step 1: Create a Firebase Project

1

Go to Firebase Console

Navigate to Firebase Console and sign in with your Google account.
2

Create a new project

  1. Click Add project
  2. Enter a project name: TikTok-Clone-iOS
  3. Click Continue
3

Configure Google Analytics

  1. Enable Google Analytics (recommended)
  2. Select or create an Analytics account
  3. Click Create project
Wait for the project to be provisioned (this takes about 30 seconds).

Step 2: Add iOS App to Firebase

1

Register your iOS app

  1. In the Firebase Console, click the iOS icon
  2. Enter your iOS Bundle ID:
com.yourname.tiktok-clone
This must match the Bundle Identifier in your Xcode project under Signing & Capabilities.
2

Add app nickname (optional)

Enter a nickname like TikTok Clone iOS to identify this app in the console.
3

Download configuration file

  1. Click Download GoogleService-Info.plist
  2. Save the file - you’ll add it to Xcode next
  3. Click Next
4

Add GoogleService-Info.plist to Xcode

  1. Open your Xcode project
  2. Right-click the KD Tiktok-Clone folder
  3. Select Add Files to “KD Tiktok-Clone”…
  4. Choose the downloaded GoogleService-Info.plist
  5. Ensure “Copy items if needed” is checked
  6. Click Add
The file should be placed in:
KD Tiktok-Clone/Utils/GoogleService-Info.plist
5

Verify Firebase initialization

Firebase is initialized in AppDelegate.swift:
AppDelegate.swift:10-21
import UIKit
import Firebase

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }
}
No additional code changes are needed!

Step 3: Enable Cloud Firestore

Firestore stores all post metadata, user profiles, and comments.
1

Navigate to Firestore

In the Firebase Console, click Firestore Database in the left sidebar.
2

Create database

  1. Click Create database
  2. Select Start in test mode (for development)
  3. Click Next
Test mode allows unrestricted read/write access. Change to production mode before launching publicly.
3

Choose Firestore location

Select a region close to your users (e.g., us-central or us-east1), then click Enable.
The location cannot be changed after creation, so choose carefully.
4

Create Post collection

The app expects a Post collection. Click Start collection:
  • Collection ID: Post
  • Click Next
5

Add sample document

Add a test document with these fields:
FieldTypeValue
videostringsample.mp4
videoURLstringhttps://firebase.storage.example.com/videos/sample.mp4
videoFileExtensionstringmp4
videoHeightnumber1920
videoWidthnumber1080
authorstringuser123
autherNamestringJohn Doe
captionstringCheck out my first video!
musicstringOriginal Sound
likeCountnumber0
shareCountnumber0
commentIDstringcomment123
pageNumbernumber1
Click Save
The pageNumber field is used for pagination in the home feed.

Firestore Data Structure

The app uses this Firestore schema:
Firestore Database
└── Post (collection)
    └── {documentId} (auto-generated)
        ├── video: String
        ├── videoURL: String
        ├── videoFileExtension: String
        ├── videoHeight: Number
        ├── videoWidth: Number
        ├── author: String
        ├── autherName: String
        ├── caption: String
        ├── music: String
        ├── likeCount: Number
        ├── shareCount: Number
        ├── commentID: String
        └── pageNumber: Number

How Posts Are Fetched

The app uses paginated queries to load videos:
PostsRequest.swift:22-34
static func getPostsByPages(pageNumber: Int, size: Int = 5, success: @escaping Success, failure:  @escaping Failure){
    db.whereField(field, isGreaterThanOrEqualTo: pageNumber)
        .whereField(field, isLessThan: pageNumber + size)
        .getDocuments(completion: {snapshot, error in
            if let error = error {
                failure(error)
            } else {
                if let snapshot = snapshot {
                    success(snapshot)
                }
            }
        })
}
Videos are loaded in batches of 5-10 posts based on the pageNumber field.

Step 4: Enable Firebase Storage

Firebase Storage stores the actual video files.
1

Navigate to Storage

In the Firebase Console, click Storage in the left sidebar.
2

Get started

Click Get started
3

Configure security rules

  1. Select Start in test mode
  2. Click Next
Test mode allows anyone to read/write files. Update rules before production:
rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /Videos/{videoId} {
      allow read: if true;
      allow write: if request.auth != null;
    }
  }
}
4

Choose storage location

Select the same region as your Firestore database, then click Done.
5

Create Videos folder

  1. Click Create folder
  2. Name it Videos
  3. Click Create
This folder will store all uploaded video files.

Video Upload Flow

Here’s how videos are uploaded to Firebase Storage:
VideoPostRequest.swift:21-54
static func publishPost(post: Post, videoURL: URL, success: @escaping Success, failure:  @escaping Failure){
    let videoRef = Storage.storage().reference().child("Videos/\(post.video)")
    
    // Upload the file to Firebase Storage
    let _ = videoRef.putFile(from: videoURL, metadata: nil) { metadata, error in
        if let error = error {
            failure(error)
            return
        }
        guard let metadata = metadata else { return }
        let size = metadata.size / 1024 / 1024
        print("File Size: \(size)MB")
        
        // Get download URL
        videoRef.downloadURL { (url, error) in
            guard let downloadURL = url else { return }
            var tempPost = post
            tempPost.videoURL = downloadURL
            _publishPostToDatabase(post: tempPost)
        }
    }
}
The upload process:
  1. Upload video to Storage
  2. Get the download URL
  3. Save post metadata + URL to Firestore

Step 5: Configure Firebase Analytics

Analytics is automatically enabled when you add Firebase to your app.
1

Verify Analytics setup

Firebase Analytics is initialized automatically via FirebaseApp.configure().
2

Test analytics

Run the app and navigate through different screens. Events are logged automatically.
3

View analytics data

  1. Go to Analytics → Events in Firebase Console
  2. Wait 24 hours for initial data to appear
  3. View metrics like:
    • Screen views
    • User engagement
    • Retention rates
Analytics data has a delay of up to 24 hours. Use DebugView for real-time testing.

Step 6: Update Security Rules (Production)

Before launching your app, update security rules:

Firestore Rules

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    // Allow read access to all posts
    match /Post/{postId} {
      allow read: if true;
      allow write: if request.auth != null;
    }
    
    // Restrict user data access
    match /User/{userId} {
      allow read: if true;
      allow write: if request.auth != null && request.auth.uid == userId;
    }
    
    // Comments require authentication
    match /Comment/{commentId} {
      allow read: if true;
      allow write: if request.auth != null;
    }
  }
}

Storage Rules

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /Videos/{videoId} {
      // Anyone can read videos
      allow read: if true;
      
      // Only authenticated users can upload
      allow write: if request.auth != null
                   && request.resource.size < 50 * 1024 * 1024 // Max 50MB
                   && request.resource.contentType.matches('video/.*');
    }
  }
}
These rules require Firebase Authentication. Implement user auth before deploying to production.

Testing Your Firebase Setup

1

Run the app

Build and run the app in Xcode (⌘ + R)
2

Check Firestore connection

The home feed should load posts from your Firestore database. Check Xcode console for logs:
Successfully fetched 5 posts from Firestore
3

Test video upload

  1. Tap the + button to record a video
  2. Grant camera and microphone permissions
  3. Record a short video
  4. Add a caption and tap Post
  5. Check Firebase Console → Storage to see the uploaded file
4

Verify video caching

Videos are automatically cached after viewing. Check the app’s document directory:
VideoCacheManager.swift:33-42
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
let diskDirectory = paths.last! + "/VideoCache"
if !diskCache.fileExists(atPath: diskDirectory) {
    do {
        try diskCache.createDirectory(atPath: diskDirectory, withIntermediateDirectories: true, attributes: nil)
    } catch {
        print("Unable to create disk cache due to: " + error.localizedDescription)
    }
}

Troubleshooting

Solution: Verify that:
  1. GoogleService-Info.plist is in your Xcode project
  2. The file is included in the app target
  3. FirebaseApp.configure() is called in AppDelegate
Solution: Check your Firestore security rules. For development, use test mode:
allow read, write: if true;
Solution:
  1. Verify Storage is enabled in Firebase Console
  2. Check Storage security rules allow writes
  3. Ensure video file size is under 50MB
  4. Check network connectivity
Solution: The Bundle ID in Xcode must match Firebase Console:
  1. Check Xcode: Target → Signing & Capabilities → Bundle Identifier
  2. Check Firebase: Project Settings → Your apps → iOS apps
  3. Update to match, then re-download GoogleService-Info.plist

Firebase Console Overview

Key sections you’ll use:

Firestore Database

View and edit posts, users, and comments

Storage

Browse uploaded videos and manage storage

Analytics

Track user engagement and app metrics

Project Settings

Manage API keys and app configurations

Next Steps

Architecture

Learn about MVVM, RxSwift, and the app’s code structure

API Reference

Explore Firebase request methods and data models

Best Practices

Development vs Production:
  • Development: Use test mode for easier debugging
  • Production: Implement authentication and strict security rules
Cost Management:
  • Firebase has a generous free tier
  • Monitor Storage usage (1GB download/day free)
  • Monitor Firestore reads (50K reads/day free)
  • Set up budget alerts in Firebase Console
Video Optimization:
  • Keep videos under 30 seconds
  • Use H.264 codec for best compatibility
  • Compress videos to 720p or 1080p max
  • Implement video transcoding for production apps

Build docs developers (and LLMs) love