You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Firebase Storage旧语法适配问题:downloadURL相关代码报错求助

Fixing Firebase Storage Download URL Deprecation Issue

Hey there! The error you're seeing is because Firebase Storage retired the old downloadURLWithCompletion() method a while back—they replaced it with more modern async APIs that fit better with Swift's current patterns. Let's get your code updated properly.

Why Your Current Code Breaks

The downloadURLWithCompletion() method no longer exists in recent Firebase Storage SDK versions. Instead, you have two solid options to get the download URL: using async/await (the recommended approach for modern Swift) or the updated completion handler method.


Option 1: Use Async/Await (Swift 5.5+, iOS 15+/macOS 12+)

This is the cleanest, most readable approach if your deployment target supports it. We'll wrap the URL fetch in a Task to handle the async work safely:

Storage.storage().reference().child(imgUid).putData(imgData, metadata: metadata) { [weak self] (metadata, error) in
    guard let self = self else { return }
    
    if let error = error {
        print("Didn't upload image: \(error.localizedDescription)")
        return
    }
    
    print("Image uploaded successfully!")
    
    // Fetch the download URL with async/await
    Task {
        guard let storageRef = metadata?.storageReference else {
            print("Error: No storage reference found in metadata")
            return
        }
        
        do {
            let downloadURL = try await storageRef.getDownloadURL()
            self.setUser(img: downloadURL)
        } catch {
            print("Failed to retrieve download URL: \(error.localizedDescription)")
        }
    }
}

Key improvements here:

  • Added [weak self] and guard let self = self to prevent memory leaks from nested closures.
  • Proper error handling with do/catch instead of force-unwrapping.
  • More descriptive error messages to help debug issues faster.

Option 2: Updated Completion Handler (For Older Deployment Targets)

If you need to support iOS versions before 15, use the updated getDownloadURL(completion:) method (note the name change from the old deprecated one):

Storage.storage().reference().child(imgUid).putData(imgData, metadata: metadata) { [weak self] (metadata, error) in
    guard let self = self else { return }
    
    if let error = error {
        print("Didn't upload image: \(error.localizedDescription)")
        return
    }
    
    print("Image uploaded successfully!")
    
    guard let storageRef = metadata?.storageReference else {
        print("Error: No storage reference found in metadata")
        return
    }
    
    // Use the updated completion handler method
    storageRef.getDownloadURL { downloadURL, error in
        if let error = error {
            print("Failed to retrieve download URL: \(error.localizedDescription)")
            return
        }
        
        if let url = downloadURL {
            self.setUser(img: url)
        }
    }
}

This follows the same callback pattern you're used to, but uses the supported method instead of the deprecated one.


Quick Notes

  • Make sure you're using the latest version of Firebase Storage SDK in your project—these APIs won't work with outdated versions.
  • Always handle errors explicitly instead of ignoring them; it'll save you hours of debugging down the line.
  • Using [weak self] is critical here to avoid retain cycles when working with nested closures in UI code.

内容的提问来源于stack exchange,提问作者OLEGA_zz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 06:53:36