首页 文章

使用私有Google存储的Firebase iOS SDK身份验证

提问于
浏览
8

我正在我的应用程序(macOS app)中使用适用于iOS / macOS的Firebase SDK进行测试 . 我使用以下方法安装了SDK:

pod 'FirebaseCore', :git => 'https://github.com/firebase/firebase-ios-sdk.git', :tag => '4.8.2'
pod 'FirebaseAuth', :git => 'https://github.com/firebase/firebase-ios-sdk.git', :tag => '4.8.2'
pod 'FirebaseStorage', :git => 'https://github.com/firebase/firebase-ios-sdk.git', :tag => '4.8.2'

安装工作正常,我可以使用 [FIRApp configure]; 在AppDelegate中配置我的应用程序

我想知道我是否可以使用SDK将用户登录到他/她的私有Google Cloud 端存储(GCS)?我知道我可以使用SDK在应用程序存储中存储到GCS,但是登录用户自己的GCS以检索存储桶和文件列表会很不错 . 如果有人有如何做到这一点的例子我会很感激 . 我找到的所有示例都是匿名存储登录 .

Update: 我可以指定我希望Firebase SDK包含允许我访问自己的Google Cloud 端存储帐户的身份验证方法 . 也许Firebase不是这个的正确选择,但是我会对Swift / objective-c登录/上传/下载到Google Cloud 存储的替代SDK的建议非常感兴趣 .

2 回答

  • 3

    您确实可以使用Firebase SDK for iOS与Firebase Cloud Storage(which in fact stores data in Google Cloud Platform Cloud Storage's buckets)一起使用Swift和Objective-C .

    iOS中的Firebase Cloud 存储

    关于Firebase应用程序中 Cloud 存储桶的使用情况,您可以开始使用this documentation page . 首先,您必须为存储桶设置适当的安全规则:您可以允许公共访问,仅访问经过身份验证的用户,甚至是每用户ID访问 . 有一些sample rules可以用来开始使用它 .

    一旦您为存储桶设置了适当的访问权限(如果每个用户都有自己的存储桶,那么我假设每个用户都有一个带有私有存储桶的GCP帐户,他们必须自己设置配置访问权限,因为您不会可以访问它们,您可以将 Cloud 存储依赖项添加到您的iOS应用程序:

    pod 'Firebase/Core'
    pod 'Firebase/Storage'
    

    然后运行 pod install ,您可以在应用程序中初始化Firebase之后已经create the reference to Cloud Storage(这里有一个Swift示例代码,但您也可以查看文档中的Objective-C示例):

    // Import and set up Firebase
    import Firebase
    FirebaseApp.configure()
    
    // Create a storage reference
    let storage = Storage.storage()
    let storageRef = storage.reference()
    
    // Refer to a child directory or even a file
    let folderRef = storageRef.child("my_folder")
    var fileRef = folderRef.child("my_file.txt")
    

    一旦掌握了所有这些,您就可以继续阅读更复杂的指南,例如uploading filesdownloading files或(重要)handling errors . 请记住,这些只是逐步记录文档后可以执行的操作的一些示例,但请随意浏览所有页面,以便更深入地了解所有这些是如何工作的 .

    iOS中的 Cloud 存储Firebase身份验证

    此外,关于身份验证,您可以遵循可能已用于其余Firebase应用程序的相同规则 . 让我分享另一个页面,讨论提供Firebase Authentication的一些机制,特别是如何提供Firebase Authetication on iOS .

  • 2

    我不确定我完全明白你在问什么 . 但如果我这样做......这可能有所帮助 . 我使用Google Storage来保存照片 . 要访问这些照片,我需要存储URL以找到这些照片 . 我是在Firebase实时数据库中完成的 . 如果将不同类型的文件存储到GCS,则只需要该URL来检索数据 .

    if let photoData = UIImageJPEGRepresentation(jpegRepresentation!, 1.0) {
                        storePhoto(photoData, angel.name!, completion: { (url, err) in
                            if err != nil {
                                print(err?.localizedDescription)
                                angelToSave["photo"] = nil
                                myAngelsRef.updateChildValues(angelToSave, withCompletionBlock: { (error, ref) in
                                    if error != nil {
                                        completion(error!)
                                    } else {
                                        completion(nil)
                                    }
                                })
                            } else {
                                      // ### HERE ####
                                angelToSave["photo"] = url?.absoluteString
                                angelNameRef.updateChildValues(angelToSave)
                                completion(nil)
                            }
                        })
                    }
    
    func storePhoto(_ photo: Data, _ name: String, completion: @escaping (_ result: URL?, _ error: NSError?) -> Void) {
            let storageRef = Storage.storage().reference().child(name)
            storageRef.putData(photo, metadata: nil) { (storageMetaData, err) in
                if err != nil {
                    completion(nil, NSError(domain: (err?.localizedDescription)!, code: 0, userInfo: nil))
                } else {
                    completion(storageMetaData?.downloadURL(), nil)
                }
            }
        }
    

    保存照片后,我能够获取URL位置并将其保存到我存储在RTDB中的对象 . 现在,当我从use的RTDB中提取数据时,我得到了存储数据的URL .

相关问题