Swift 4 Firebase Storage Upload User Uiimage
Firebase and iOS
How to Upload Images to Cloud Storage for Firebase with Swift
Cloud Storage for Firebase is a great identify for usa to shop 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.
Beneath is a simple analogy of what we are going to build in this tutorial:
Getting Started
Please follow the official documentation to fix your Firebase projection and enable Cloud Storage for Firebase using the Firebase console. Make sure yous upgrade to the Blaze plan 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 image file merely. You can change them to other values for your own purpose, e.g. recording a video prune from a camera source.
We and so demand to implement both the UIImagePickerControllerDelegate
and UINavigationControllerDelegate
to handle the selected file information (file URL). We tin then initiate uploading the file to Cloud Storage for Firebase on line 24.
To learn more aboutUIImagePickerViewController
, check out the following article:
Uploading Files to Deject Storage for Firebase
We can split the to a higher place codes into the below sessions:
- Line six–vii: Construct a unique file name to the file stored in Deject Storage for Firebase. Please note that new files will overwrite the original file if the file names are the same.
- Line 9: Create a Storage reference with the filename. We can as well store it inside a "folder" if you similar. "Folder" is only a human-readable term, every bit there are no directory in Deject Storage. Files take no hierarchical relationship.
Reference: Deject Storage Object Name Documentation - 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 then fetch it with any epitome loader, e.thou. Kingfisher.
Treatment errors
There could exist errors during uploading files to Deject Storage for Firebase at both server and customer sides. Nosotros tin can handle the errors with the observe
function. Hither are some common errors:
- Violation of Cloud Storage Security Rules
Firebase server tin can turn down any read and write operations based on the security rules. For case, the only logged-in user may upload files:
2. Network connectedness mistake
Network stability is sometimes an upshot on the client-side. For example, during underground transportation or inside a lift.
3. Costless quota is exceeded
Co-ordinate 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 take larger storage.
4. Cancelled past user
Singleton Nature —✖️ UIViewController Lifecycle
Uploading a file to the Cloud 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 some other screen during the uploading chore.
🤔 Practise you think users must stay on the upload screen until the upload has finished?
The reply is "no".
SDK has taken care of this situation by using a singleton blueprint. The Storage
instance returned from Storage.storage()
is a singleton, which lives every bit long as the app lives. Therefore, the uploading task is still running in the background even the user has exited the folio for uploading files.
Your 2nd question probably is "How can we handle the event (completion or error) of the uploading task when the user has navigate to a different screen?"
One style to do this is to utilize the Notification center:
At the callback of the putData
, we can enclose the storageMetaData
and fault
into a dictionary, and then pass it to Notification observers through userInfo
field.
Finally, we can add a Notification observer at the base UIViewController
such that all UIViewController
due south in the app can share the same logic. storageMetaData
and mistake
can be retrieved back from the userInfo
of the Notification object, see line xi–12.
App Lifecycle — Auto suspension and resume
After discussing the UIViewController
lifecycle topic, allow'south focus on the app lifecycle. What volition happen when users send the app to the groundwork during uploading files? Will the task be resumed automatically afterwards the app is active in the foreground? The reply is "the SDK will take care of this, too!"
In iOS, in that location are three awarding states:
-
active
— the app is in the foreground -
inactive
— the app is too in the foreground but cannot receive any 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
Co-ordinate to Apple'south documentation, each app can accept effectually 30s to behave out its operations when information technology is sent to the background. The length of time tin can be shorter depending on the device state of affairs. In other words, the Firebase SDK can withal continue the uploading job at this 30s stage but it is forced to cease by the system after that. Whatsoever incomplete upload tasks volition be resumed when the app becomes active again. See the above graph for a better illustration.
What near caching?
If yous have used Cloud Firestore before, yous were probably amazed by the local cache system. Fifty-fifty when the device is offline Firestore can salve the user's new data at the local database commencement and reupload them to the server automatically once connectivity resumes.
Still, Deject Storage for Firebase does not have a similar feature. Once the file cannot be uploaded due to a network consequence, we have to upload it later once again manually.
Summary
Cloud Storage for Firebase is a cloud tool for developers to store files and fetch them at the customer-side with the assistance of a convenient SDK.
The SDK uses a singleton pattern to tackle the UIViewController
lifecycle issue. The uploading chore can continue even when the UIViewController
is dismissed.
SDK has handled the app lifecycle consequence for developers. It tin resume the paused uploading task right subsequently the app becomes active in the foreground.
However, SDK does non have a local cache organisation and users accept to execute the uploading job manually.
Reference
- Introduction to Cloud Storage for Firebase
ii. Pricing well-nigh Deject Storage for Firebase and other great tools
3. Background tasks framework
4. UIImagePickerController introduction
flinchumstren1944.blogspot.com
Source: https://medium.com/firebase-developers/how-to-upload-image-from-uiimagepickercontroller-to-cloud-storage-for-firebase-bad90f80d6a7
Post a Comment for "Swift 4 Firebase Storage Upload User Uiimage"