0
0
iOS Swiftmobile~5 mins

Cloud Storage in iOS Swift

Choose your learning style9 modes available
Introduction

Cloud Storage lets your app save files online so users can access them anytime and anywhere. It keeps data safe and easy to share.

You want to save user photos or documents and access them from multiple devices.
Your app needs to back up data automatically without using device storage.
You want to share files between users or devices easily.
You want to keep user data safe even if the app is deleted or device is lost.
Syntax
iOS Swift
import FirebaseStorage

let storage = Storage.storage()
let storageRef = storage.reference()

// Upload a file
let localFile = URL(fileURLWithPath: "path/to/local/file")
let fileRef = storageRef.child("folder/filename.ext")

fileRef.putFile(from: localFile, metadata: nil) { metadata, error in
  if let error = error {
    print("Upload failed: \(error.localizedDescription)")
  } else {
    print("Upload successful")
  }
}

Use Storage.storage() to get the cloud storage instance.

Use putFile to upload files asynchronously with a completion handler.

Examples
Create a reference to a file path in cloud storage.
iOS Swift
let storage = Storage.storage()
let storageRef = storage.reference()
let imageRef = storageRef.child("images/photo.jpg")
Upload a local file to the cloud and handle success or error.
iOS Swift
imageRef.putFile(from: localURL, metadata: nil) { metadata, error in
  if error == nil {
    print("File uploaded")
  }
}
Get the download URL to share or display the uploaded file.
iOS Swift
imageRef.downloadURL { url, error in
  if let url = url {
    print("Download URL: \(url)")
  }
}
Sample App

This SwiftUI app uploads a small text file to cloud storage when you tap the button. It shows a message if the upload worked or failed.

iOS Swift
import SwiftUI
import FirebaseStorage

struct ContentView: View {
  @State private var message = ""

  var body: some View {
    VStack(spacing: 20) {
      Text(message)
        .padding()
      Button("Upload Sample File") {
        uploadSampleFile()
      }
    }
    .padding()
  }

  func uploadSampleFile() {
    let storage = Storage.storage()
    let storageRef = storage.reference()
    let fileRef = storageRef.child("samples/hello.txt")

    // Create a temporary file with sample text
    let tempDir = FileManager.default.temporaryDirectory
    let localURL = tempDir.appendingPathComponent("hello.txt")
    try? "Hello Cloud Storage!".write(to: localURL, atomically: true, encoding: .utf8)

    fileRef.putFile(from: localURL, metadata: nil) { metadata, error in
      if let error = error {
        message = "Upload failed: \(error.localizedDescription)"
      } else {
        message = "Upload successful!"
      }
    }
  }
}
OutputSuccess
Important Notes

Make sure to configure Firebase in your app before using cloud storage.

Uploading files is asynchronous; use completion handlers to update UI.

Files are stored in paths like folders; organize them logically.

Summary

Cloud Storage saves files online for easy access and sharing.

Use storage references to point to file locations.

Upload files with putFile and handle success or errors.