Download and Upload Image to Firebase With Swift 4

Firebase and iOS

Cloud Storage for Firebase is a great place for us to store users' files. Uploading and downloading files to and from Deject Storage is very simple and straightforward: Firebase provides a fix of SDK for iOS, Android, Web, C++ and Unity developers to manage Cloud Storage for Firebase in an easy manner. And it is like shooting fish in a barrel for developers to restrict the access right to each file with Firebase Security Rules.

Below is a unproblematic illustration of what we are going to build in this tutorial:

Getting Started

Please follow the official documentation to set up your Firebase project and enable Cloud Storage for Firebase using the Firebase console. Brand sure you upgrade to the Blaze program if your storage requirements exceed the 5GB complimentary quota.

Building the UI

UIImagePickerController is a special kind of UIViewController which allows users to select photos or video clips from camera or album source. For demonstration purposes, I set the sourceType to photoLibrary and mediaTypes to prototype file but. You can change them to other values for your ain purpose, eastward.k. recording a video clip from a camera source.

UIImagePickerController for selecting images from the user's album

We then demand to implement both the UIImagePickerControllerDelegate and UINavigationControllerDelegate to handle the selected file information (file URL). We can then initiate uploading the file to Cloud Storage for Firebase on line 24.

To larn more aboutUIImagePickerViewController, cheque out the post-obit article:

Uploading Files to Cloud Storage for Firebase

We can split the in a higher place codes into the beneath sessions:

  1. Line 6–7: Construct a unique file name to the file stored in Cloud Storage for Firebase. Please note that new files will overwrite the original file if the file names are the aforementioned.
  2. Line 9: Create a Storage reference with the filename. We can as well store it within a "folder" if y'all like. "Binder" is just a human being-readable term, every bit there are no directory in Cloud Storage. Files have no hierarchical relationship.
    Reference: Cloud Storage Object Name Documentation
  3. Line 10–26: Upload the file information along with its metadata. Afterward a successful upload, nosotros can fetch the download URL of the uploaded image so fetch it with whatever image loader, e.g. Kingfisher.

Handling errors

There could exist errors during uploading files to Deject Storage for Firebase at both server and client sides. We can handle the errors with the observe function. Hither are some common errors:

  1. Violation of Cloud Storage Security Rules
    Firebase server tin reject any read and write operations based on the security rules. For example, the but logged-in user may upload files:

ii. Network connection error
Network stability is sometimes an issue on the client-side. For case, during undercover transportation or within a lift.

3. Complimentary quota is exceeded
According to the Firebase pricing, there is a 5GB free quota for projects on the Spark Plan. The project owner has to upgrade to Blaze Plan in social club to accept larger storage.

4. Cancelled by user

Singleton Nature —✖️ UIViewController Lifecycle

Uploading a file to the Cloud server tin exist a long-running operation depending on the user'due south network condition (and the file size). Sometimes, users probably similar to navigate to another screen during the uploading chore.

đŸ¤” Exercise you think users must stay on the upload screen until the upload has finished?

The answer is "no".

SDK has taken intendance of this situation past using a singleton pattern. The Storage case returned from Storage.storage() is a singleton, which lives as long as the app lives. Therefore, the uploading job is still running in the background even the user has exited the page for uploading files.

Your 2d question probably is "How can we handle the event (completion or error) of the uploading chore when the user has navigate to a different screen?"

One way to do this is to use the Notification center:

At the callback of the putData, we can enclose the storageMetaData and error into a dictionary, and and so pass it to Notification observers through userInfo field.

Finally, we tin add together a Notification observer at the base of operations UIViewController such that all UIViewControllers in the app can share the same logic. storageMetaData and fault tin can exist retrieved back from the userInfo of the Notification object, run across line xi–12.

App Lifecycle — Auto intermission and resume

Later discussing the UIViewController lifecycle topic, let'southward focus on the app lifecycle. What will happen when users send the app to the background during uploading files? Will the job be resumed automatically after the app is active in the foreground? The reply is "the SDK will take care of this, too!"

In iOS, there are three application states:

  1. active — the app is in the foreground
  2. inactive — the app is too in the foreground just cannot receive any events from the user. This is the example when you receive a call or an SMS, for example.
  3. groundwork — the app totally disappears from the screen

Co-ordinate to Apple's documentation, each app can take effectually 30s to carry out its operations when information technology is sent to the background. The length of time tin can be shorter depending on the device situation. In other words, the Firebase SDK can all the same continue the uploading task at this 30s stage only it is forced to stop by the system after that. Any incomplete upload tasks volition be resumed when the app becomes active again. See the to a higher place graph for a meliorate illustration.

What about caching?

If you take used Deject Firestore before, you were probably amazed by the local cache system. Fifty-fifty when the device is offline Firestore tin save the user's new information at the local database commencement and reupload them to the server automatically in one case connectivity resumes.

However, Deject Storage for Firebase does not have a similar feature. Once the file cannot be uploaded due to a network issue, we have to upload it after again manually.

Summary

Cloud Storage for Firebase is a cloud tool for developers to store files and fetch them at the client-side with the help of a user-friendly SDK.

The SDK uses a singleton design to tackle the UIViewController lifecycle upshot. The uploading chore can go on even when the UIViewController is dismissed.

SDK has handled the app lifecycle issue for developers. It can resume the paused uploading task right after the app becomes active in the foreground.

However, SDK does not accept a local cache organization and users have to execute the uploading task manually.

Reference

  1. Introduction to Cloud Storage for Firebase

two. Pricing near Cloud Storage for Firebase and other great tools

3. Background tasks framework

four. UIImagePickerController introduction

torreschand1976.blogspot.com

Source: https://medium.com/firebase-developers/how-to-upload-image-from-uiimagepickercontroller-to-cloud-storage-for-firebase-bad90f80d6a7

0 Response to "Download and Upload Image to Firebase With Swift 4"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel