Upload an Image From Ios App to Firebase
Firebase and iOS
How to Upload Images to Cloud Storage for Firebase with Swift
Cloud Storage for Firebase is a great place for united states of america to store users' files. Uploading and downloading files to and from Cloud Storage is very simple and straightforward: Firebase provides a set of SDK for iOS, Android, Web, C++ and Unity developers to manage Deject Storage for Firebase in an easy way. And it is easy 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
Delight follow the official documentation to fix your Firebase project and enable Deject Storage for Firebase using the Firebase console. Make sure you upgrade to the Blaze plan if your storage requirements exceed the 5GB free quota.
Edifice the UI
UIImagePickerController is a special kind of UIViewController which allows users to select photos or video clips from camera or anthology source. For demonstration purposes, I prepare the sourceType to photoLibrary and mediaTypes to prototype file just. You can alter them to other values for your ain purpose, eastward.g. recording a video clip from a camera source.
We then need to implement both the UIImagePickerControllerDelegate and UINavigationControllerDelegate to handle the selected file information (file URL). We can and so initiate uploading the file to Deject Storage for Firebase on line 24.
To larn more aboutUIImagePickerViewController, check out the following commodity:
Uploading Files to Cloud Storage for Firebase
Nosotros tin can split up the above codes into the below sessions:
- Line 6–7: Construct a unique file name to the file stored in Cloud Storage for Firebase. Please note that new files volition overwrite the original file if the file names are the same.
- Line 9: Create a Storage reference with the filename. We can also store information technology inside a "folder" if you lot similar. "Binder" is only a human-readable term, as there are no directory in Deject Storage. Files have no hierarchical human relationship.
Reference: Cloud Storage Object Proper noun Documentation - Line 10–26: Upload the file data along with its metadata. After a successful upload, we tin fetch the download URL of the uploaded prototype so fetch it with any paradigm loader, e.g. Kingfisher.
Handling errors
There could be errors during uploading files to Cloud Storage for Firebase at both server and customer sides. We can handle the errors with the observe function. Here are some common errors:
- Violation of Cloud Storage Security Rules
Firebase server can reject any read and write operations based on the security rules. For example, the only logged-in user may upload files:
2. Network connection fault
Network stability is sometimes an event on the customer-side. For example, during secret transportation or inside a elevator.
three. Gratis quota is exceeded
According to the Firebase pricing, at that place is a 5GB gratuitous quota for projects on the Spark Plan. The project owner has to upgrade to Blaze Plan in order to have larger storage.
4. Cancelled by user
Singleton Nature —✖️ UIViewController Lifecycle
Uploading a file to the Deject server can exist a long-running operation depending on the user's network condition (and the file size). Sometimes, users probably similar to navigate to another screen during the uploading job.
🤔 Do y'all think users must stay on the upload screen until the upload has finished?
The answer is "no".
SDK has taken care of this situation by using a singleton pattern. The Storage instance returned from Storage.storage() is a singleton, which lives as long as the app lives. Therefore, the uploading task is notwithstanding running in the background fifty-fifty the user has exited the page for uploading files.
Your 2nd question probably is "How tin can we handle the event (completion or fault) of the uploading task when the user has navigate to a different screen?"
I way to practice this is to apply the Notification middle:
At the callback of the putData, we can enclose the storageMetaData and error into a dictionary, and and so laissez passer it to Notification observers through userInfo field.
Finally, we can add a Notification observer at the base UIViewController such that all UIViewControllers in the app can share the aforementioned logic. storageMetaData and error can be retrieved dorsum from the userInfo of the Notification object, see line 11–12.
App Lifecycle — Auto pause and resume
After discussing the UIViewController lifecycle topic, let's focus on the app lifecycle. What will happen when users transport the app to the background during uploading files? Volition the task be resumed automatically afterwards the app is agile in the foreground? The answer is "the SDK will take care of this, likewise!"
In iOS, there are three awarding states:
-
active— the app is in the foreground -
inactive— the app is also in the foreground but cannot receive whatever events from the user. This is the instance when you lot receive a call or an SMS, for instance. -
background— the app totally disappears from the screen
According to Apple'southward documentation, each app can have around 30s to carry out its operations when it is sent to the background. The length of time can be shorter depending on the device situation. In other words, the Firebase SDK can however continue the uploading task at this 30s stage simply it is forced to finish by the system after that. Any incomplete upload tasks will be resumed when the app becomes active again. See the in a higher place graph for a meliorate illustration.
What about caching?
If you take used Deject Firestore before, yous were probably amazed by the local cache system. Even when the device is offline Firestore can save the user's new data at the local database beginning and reupload them to the server automatically in one case connectivity resumes.
Nevertheless, Cloud Storage for Firebase does not accept a like feature. Once the file cannot be uploaded due to a network issue, we accept to upload it later again manually.
Summary
Deject Storage for Firebase is a deject tool for developers to store files and fetch them at the client-side with the aid of a user-friendly SDK.
The SDK uses a singleton pattern to tackle the UIViewController lifecycle upshot. The uploading task can continue fifty-fifty when the UIViewController is dismissed.
SDK has handled the app lifecycle result for developers. It can resume the paused uploading task right later on the app becomes active in the foreground.
Yet, SDK does not have a local cache organisation and users accept to execute the uploading task manually.
Reference
- Introduction to Deject Storage for Firebase
2. Pricing nigh Cloud Storage for Firebase and other great tools
3. Background tasks framework
4. UIImagePickerController introduction
Source: https://medium.com/firebase-developers/how-to-upload-image-from-uiimagepickercontroller-to-cloud-storage-for-firebase-bad90f80d6a7
0 Response to "Upload an Image From Ios App to Firebase"
Post a Comment