Xcode How to Let Users Upload Pictures Firebase
Firebase and iOS
How to Upload Images to Cloud Storage for Firebase with Swift
Cloud Storage for Firebase is a bully place for us to store users' files. Uploading and downloading files to and from Cloud Storage is very simple and straightforward: Firebase provides a fix of SDK for iOS, Android, Spider web, C++ and Unity developers to manage Cloud Storage for Firebase in an like shooting fish in a barrel way. And it is piece of cake for developers to restrict the access right to each file with Firebase Security Rules.
Below is a simple analogy of what we are going to build in this tutorial:
Getting Started
Delight follow the official documentation to ready your Firebase project and enable Cloud Storage for Firebase using the Firebase console. Make sure you lot upgrade to the Blaze program if your storage requirements exceed the 5GB costless 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 image file only. Y'all can change them to other values for your ain purpose, e.chiliad. recording a video clip from a camera source.
Nosotros then need to implement both the UIImagePickerControllerDelegate
and UINavigationControllerDelegate
to handle the selected file information (file URL). Nosotros can and then initiate uploading the file to Cloud Storage for Firebase on line 24.
To learn more aboutUIImagePickerViewController
, check out the post-obit article:
Uploading Files to Cloud Storage for Firebase
We tin separate the in a higher place codes into the below sessions:
- Line 6–vii: Construct a unique file name to the file stored in Cloud Storage for Firebase. Please annotation that new files will overwrite the original file if the file names are the aforementioned.
- Line 9: Create a Storage reference with the filename. We can also store it inside a "folder" if you like. "Binder" is just a human-readable term, equally there are no directory in Cloud Storage. Files have no hierarchical human relationship.
Reference: Cloud Storage Object Name Documentation - Line ten–26: Upload the file data along with its metadata. After a successful upload, we can fetch the download URL of the uploaded image and then fetch it with any image loader, eastward.g. Kingfisher.
Handling errors
There could be errors during uploading files to Cloud Storage for Firebase at both server and client 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 error
Network stability is sometimes an issue on the customer-side. For instance, during surreptitious transportation or inside a elevator.
three. Costless quota is exceeded
According to the Firebase pricing, in that location is a 5GB gratis quota for projects on the Spark Plan. The projection owner has to upgrade to Blaze Plan in social club to take larger storage.
iv. 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 like to navigate to another screen during the uploading task.
🤔 Do you recall users must stay on the upload screen until the upload has finished?
The answer is "no".
SDK has taken intendance of this state of affairs by using a singleton pattern. The Storage
example returned from Storage.storage()
is a singleton, which lives as long equally 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 task when the user has navigate to a dissimilar screen?"
Ane way to do this is to utilize the Notification centre:
At the callback of the putData
, we tin enclose the storageMetaData
and error
into a lexicon, and then laissez passer it to Notification observers through userInfo
field.
Finally, we can add together a Notification observer at the base of operations UIViewController
such that all UIViewController
s in the app tin can share the same logic. storageMetaData
and error
tin be retrieved back from the userInfo
of the Notification object, run into line 11–12.
App Lifecycle — Machine break and resume
Later on discussing the UIViewController
lifecycle topic, let's focus on the app lifecycle. What will happen when users send the app to the background during uploading files? Will the task be resumed automatically after the app is agile in the foreground? The answer is "the SDK will take care of this, too!"
In iOS, there are iii application states:
-
active
— the app is in the foreground -
inactive
— the app is too in the foreground but cannot receive whatsoever events from the user. This is the case when you receive a call or an SMS, for example. -
background
— the app totally disappears from the screen
According to Apple tree's documentation, each app tin can take around 30s to behave out its operations when it is sent to the background. The length of time can exist shorter depending on the device situation. In other words, the Firebase SDK tin however keep the uploading task at this 30s stage just information technology is forced to stop by the system after that. Any incomplete upload tasks will exist resumed when the app becomes active again. See the in a higher place graph for a better illustration.
What about caching?
If you lot have used Cloud Firestore earlier, you were probably amazed by the local cache system. Even when the device is offline Firestore can save the user'southward new data at the local database commencement and reupload them to the server automatically once connectivity resumes.
However, Cloud Storage for Firebase does not accept a like feature. One time the file cannot be uploaded due to a network consequence, nosotros have to upload it later 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 event. The uploading task tin can continue fifty-fifty when the UIViewController
is dismissed.
SDK has handled the app lifecycle effect for developers. It can resume the paused uploading task right later on the app becomes active in the foreground.
However, SDK does non have a local cache system and users have to execute the uploading task manually.
Reference
- Introduction to Deject Storage for Firebase
two. Pricing nearly Deject Storage for Firebase and other dandy 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 "Xcode How to Let Users Upload Pictures Firebase"
Post a Comment