This article contains a utility class with 2 methods that would help you to upload Images, Videos, Audios and all other types of files to Firebase Storage in Swift IOS. It’s a guide to explain how to upload files into Firebase Storage in Swift IOS.
You can also find the same methods here to Upload Images, Videos & Files to Firebase Storage in Java Android.
In this article, it is considered that you have already added the following Firebase Storage Pod into your Xcode project PodFile
.
pod 'Firebase/Storage'
If you are new to Firebase or Firebase Cloud Storage, you can read about these in our Step by Step Guide to Integrate Firebase into your Project or Firebase Storage for Beginners.
Copy the following class FirebaseStorageManager.swift
into your project. In this class, you have 2 methods ‘uploadFile
‘ & ‘uploadImageData
‘. To upload images to Firebase Storage you can use ‘uploadImageData
‘ function. And to upload all kinds of files you can call ‘uploadFile
‘ method.
FirebaseStorageManager.swift class
import UIKit
import FirebaseStorage
class FirebaseStorageManager {
public func uploadFile(localFile: URL, serverFileName: String, completionHandler: @escaping (_ isSuccess: Bool, _ url: String?) -> Void) {
let storage = Storage.storage()
let storageRef = storage.reference()
// Create a reference to the file you want to upload
let directory = "uploads/"
let fileRef = storageRef.child(directory + serverFileName)
_ = fileRef.putFile(from: localFile, metadata: nil) { metadata, error in
fileRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
completionHandler(false, nil)
return
}
// File Uploaded Successfully
completionHandler(true, downloadURL.absoluteString)
}
}
}
public func uploadImageData(data: Data, serverFileName: String, completionHandler: @escaping (_ isSuccess: Bool, _ url: String?) -> Void) {
let storage = Storage.storage()
let storageRef = storage.reference()
// Create a reference to the file you want to upload
let directory = "uploads/"
let fileRef = storageRef.child(directory + serverFileName)
_ = fileRef.putData(data, metadata: nil) { metadata, error in
fileRef.downloadURL { (url, error) in
guard let downloadURL = url else {
// Uh-oh, an error occurred!
completionHandler(false, nil)
return
}
// File Uploaded Successfully
completionHandler(true, downloadURL.absoluteString)
}
}
}
}
How to Use this Utility Class
You can create an instance of this class and call the methods to upload Images & files to Firebase Storage. The code is shown following.
1. To Upload Images into Firebase Storage
if let data = image.pngData() { // convert your UIImage into Data object using png representation
FirebaseStorageManager().uploadImageData(data: data, serverFileName: "your_server_file_name.png") { (isSuccess, url) in
print("uploadImageData: \(isSuccess), \(url)")
}
}
2. To Upload Files into Firebase Storage
FirebaseStorageManager().uploadFile(localFile: YOUR_LOCAL_FILE_URL, serverFileName: "your_server_file_name.mp4") { (isSuccess, url) in
print("uploadImageData: \(isSuccess), \(url)")
}
Note: This class will work only if your user is already authenticated to upload files to Firebase Storage, or your Security Rules are public. Otherwise, please read about Firebase Storage Security Rules to run this solution successfully.
That’s it!! This is how to upload images videos & other files to Firebase Storage in Swift. 🙂
You can also find other useful helping materials and tutorials in our Coding Articles & Tutorials Knowledge Base.
Appreciate it for helping out, fantastic info. “You must do the things you think you cannot do.” by Eleanor Roosevelt.
I went over this internet site and I believe you have a lot of excellent information, bookmarked (:.