如何从使用 Firebase 设置的一个 IOS 应用程序读取/写入另一个 Firebase 项目中包含的另一个 Firebase 数据库?雨燕3

2024-04-06

我有一个 Firebase 数据库通过 GoogleService-Info.plist 连接到我的 IOS 应用程序。在 AppDelegate 中,我配置了应用程序 FIRApp.configure()。我可以读/写数据。

现在,在这个 IOS 应用程序中,我想访问另一个 FireBase 数据库brevCustomer。因为某些原因let dbRef from viewDidLoadXcode 中有一个标志说这个“不可变值”dbRef从未使用过”并且应用程序在 fun startObserving() 的第一行崩溃dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in.

谁能展示如何进行配置,以便我可以读取/写入 brevCustomer 数据库?

EDIT

请考虑以下场景:

  • 我有两个 IOS 应用程序Customer and Worker和两个名为的 Firebase 项目客户火力基地 and 工人Firebase我希望他们按照以下方式工作。

  • 客户使用电子邮件和密码注册、登录、预订,数据保存在 CustomerFireBase 中。

  • Worker registers with email and password, logs is, observe WorkerFirebase for value changes or child added
    • read from CustomerFireBase
      • 写入客户 FireBase
      • 写入 WorkerFirebase

我怎样才能实现这个目标?基本上,我需要从配置的一个 IOS 应用程序获得读/写访问权限以通常的方式 https://firebase.google.com/docs/ios/setup使用 Firebase 到另一个 Firebase 项目中包含的另一个 Firebase 数据库。

Class Claim {

  var dbRef:FIRDatabaseReference! //create a reference to Firebase database `brevCustomer`, not the one from .plist file

   override func viewDidLoad() {
       super.viewDidLoad()

     let app = FIRApp(named: "brevCustomer")
     let dbRef = FIRDatabase.database(app: app!).reference().child("Users")
     startObservingDB() // observe the database for value changes
    }

 func startObservingDB() {
   //it crashes on the line below
    dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in

        //iterate over each user node child
        for user_child in snapshot.children {
             print(user_child)} 

          }, withCancel: { (Error: Any) in
         print(Error)
       })
   } // end of startObservingDB()
}//end of Claim class



class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

// Use Firebase library to configure APIs for the initial project with .plist file saved in Xcode
FIRApp.configure()


    /** 1. create a Firebase options object to hold the configuration data for the second Firebase Project */
    let secondaryOptions = FIROptions(googleAppID: "1:82424687545:ios:71df5d45218ad27",
                                      bundleID: "com.vivvdaplar.Brev",
                                      gcmSenderID: "8201647545",
                                      apiKey: "AIzaSyCNtyUf2T3UunH6-ci_WyvOqCl_RzXI",
                                      clientID: "8200687545-42vklp94reavi6li6bolhcraoofc6.apps.googleusercontent.com",
                                      trackingID: nil,
                                      androidClientID: nil,
                                      databaseURL: "https://brev-72e10.firebaseio.com",
                                      storageBucket: "com.vivvdaplar.Brev",
                                      deepLinkURLScheme: nil)

    // Configure the app
    FIRApp.configure(withName: "brevCustomer", options: secondaryOptions!) 
       return true
  }
} //end of AppDelegate

回答问题和评论。

如您所知,当用户注册 Firebase 时,会在 Firebase 服务器上创建一个用户帐户,并向该用户提供一个用户 ID (uid)。

典型的设计模式是在 Firebase 中有一个 /users 节点,用于存储有关用户的其他信息,例如昵称、地址或电话号码。

我们还可以利用 /users 节点来指示它是什么类型的用户; Worker 或 Client,它将与应用程序的其余部分和 Firebase 联系起来,以便他们获得正确的数据。

例如

users
  uid_0
    nickname: "John"
    user_type: "Worker"
  uid_1
    nickname: "Paul"
    user_type: "Client"
  uid_2
    nickname: "George"
    user_type: "Worker"
  uid_3
    nickname: "Ringo"
    user_type: "Worker"

正如您所看到的,John、George 和 Ringo 都是工人,Paul 是客户。

当用户登录时,Firebase 登录函数将返回用户身份验证数据,其中包含 uid。

    Auth.auth().signIn(withEmail: "[email protected] /cdn-cgi/l/email-protection", password: "dog",
     completion: { (auth, error) in

        if error != nil {
            let err = error?.localizedDescription
            print(err!)
        } else {
            print(auth!.uid)
            //with the uid, we now lookup their user type from the
            //users node, which tells the app if they are a client
            //or worker
        }
    })

如果app数据是这样划分的

app
  client_data
     ...
  worker_data
     ...

可以设置一个简单的规则来验证用户的 user_type 对于worker_data节点是Worker,对于client_data节点是Client。这是一个伪示例,它将允许客户端用户仅访问 client_data 节点中的数据(概念)

rules 
  client_data
     $user_id
        ".read": "auth != null && root.child(users)
                                      .child($user_id)
                                      .child("user_type") == 'Client'"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从使用 Firebase 设置的一个 IOS 应用程序读取/写入另一个 Firebase 项目中包含的另一个 Firebase 数据库?雨燕3 的相关文章

  • AFNetworking XML 请求问题

    我在用着AFNetworking 2使用 JSON 响应 它工作正常 现在我必须将其转换为 XML 而不是使用 JSON 因为服务器响应是 XML 格式的 在我搜索之后 我找到了这段代码 但它不起作用 与 Charles 我发现请求是错误的
  • 如何设置 Firebase 用户的显示名称?

    根据Firebase网站上的JS Auth文档 它只展示了如何获取 displayName 以及如何更新 displayName 所以我尝试更新它 但这有点不合逻辑 因为你怎么能在不创建某些东西的情况下更新它呢 所以我的问题是 如何设置注册
  • React-native-vision-camera无法访问后面的普通摄像头

    我正在尝试在 iPhone 11 Pro 上使用 普通 相机 我使用反应本机视觉相机 当我运行这段代码时 const devices useCameraDevices const deviceBack devices back consol
  • 从一个模态视图无缝翻转到另一个模态视图,而不显示纯色背景

    我的 iPad 应用程序的 UI 如下 当我点击Settings按钮 我希望对话框水平翻转以显示设置对话框 我这个工作正常 但是 当对话翻转时会显示背景颜色 如你看到的 有什么办法可以让对话框翻转时不显示该颜色块吗 我希望它看起来更加无缝
  • AngularFire 访问子元素方法

    我正在寻找一种方法来获取子元素的方法 而不是单独加载该元素 假设我有一个帖子模型 每个帖子都有评论 这就是我获取帖子模型的方式 var post firebase new Firebase FIREBASE URL posts post n
  • iOS 中 NSDecimalNumber 的小数分隔符错误

    我尝试通过以下方式输出具有正确的小数分隔符的十进制数的描述 NSString strValue 9 94300 NSDecimalNumber decimalNumber NSDecimalNumber decimalNumberWithS
  • CALayer边框奇怪问题

    我正在向 CALayer 添加边框 但有一些奇怪的行为 在我应用的边框之后出现模糊边框 参见屏幕截图 这是我的代码 void configureLabel self hidden YES self textAlignment NSTextA
  • iOS:生成pdf时绘制文本时如何设置字体?

    我在ios应用程序中使用drawpdf函数生成pdf 同时调用nsobject类中的drawtext函数 它根据我指定的框架和字符串清楚地绘制文本 我的代码是 void drawText NSString textToDraw inFram
  • 使用未解析的标识符“FlurryAdInterstitial”

    我正在尝试整合Flurry Interstitial Ads使用cocoapods in Swift and Xcode 7 1 1 我正在关注开发人员雅虎网站上的此文档 https developer yahoo com flurry d
  • 会话重新启动后 AVcapture 会话启动缓慢

    我有一个主视图控制器 它连接到具有 avcapturesession 的第二个视图控制器 我第一次从主视图控制器转向捕获会话控制器 大约需要 50 毫秒 使用 仪器 检查 然后我从捕获会话返回到主视图控制器 然后从主控制器返回到 avcap
  • 如何设置Firestore安全规则? Resource.data:空值错误

    我需要一些帮助来使我的 Firestore 安全规则发挥作用 这些是我的 Firestore 规则 service cloud firestore match databases database documents match order
  • AVAssetExportSession 无法导出从 iCloud 下载的视频

    我正在尝试创建从用户相册中选择的视频的缩小版本 输出的最大尺寸为 720p 因此 在检索视频时 我使用 mediumQualityFormat as the deliveryMode 如果用户设备中不存在原始视频或其中等质量版本 这会导致
  • iOS 7 上 Safari 浏览器的用户代理

    我只想在带有 Safari 浏览器的 iPhone 和 iPod 中打开我的网站 对于 Chrome Dolphin 等任何其他浏览器 它不应该打开 但目前我从几乎所有设备获得相同的用户代理 对于Safari User Agent Stri
  • watchOS 错误:控制器接口描述中的未知属性

    我将 WKInterfacePicker 添加到情节提要中 并将其连接到界面控制器中的 IBOutlet 运行应用程序时 它在控制台中显示一条错误消息 控制器的接口描述 watchPicker 中的未知属性 Code interface I
  • 如何在button.addTarget操作中发送多个按钮?斯威夫特3

    如何将button和button2发送到我的pressButton2函数中 当用户触摸按钮2时 我需要更改按钮和按钮2的颜色 当我的 button2 addTarget 看起来像这样时 我收到错误 表达式列表中存在预期表达式 import
  • 如何在代码中编辑约束

    我有一个以 100 开始宽度限制的网页 当用户单击按钮时 我想将约束更改为 200 我试过这个 NSLayoutConstraint constrain NSLayoutConstraint constraintWithItem self
  • firebase :: 无法读取 null 的属性“props”

    你好 我正在尝试将react router与firebase一起使用 但它给了我这个错误 无法读取 null 的属性 props 这正是代码 我正在其中使用我的反应路由器 向下代码位于作为登录面板组件的组件上 else if this em
  • 如何在 UICollectionView 中将行居中?

    我有一个UICollectionView与随机细胞 有什么方法可以让我将行居中吗 默认情况下它是这样的 x x x x x x x x x x x x x x 这是所需的布局 x x x x x x x x x x x x 我必须做这样的事
  • 模态转场需要点击 2 次而不是 1 次

    我的 UITableView 需要点击 2 次才能显示所选单元格的详细信息页面 一次用于选择 另一次用于显示详细信息视图 我希望有一个 CLI 直接显示所单击单元格的详细视图 我在 UITableViewManager m 中使用此方法的模
  • 是否可以跨 2 个不同的 iOS 应用程序访问数据?

    假设我在 App1 中存储了一些 ID 数据 并希望在同一设备上的 App2 中访问它 平台上可以这样吗 如果没有的话有什么解决方法吗 您可以使用iOS 钥匙扣 http developer apple com library ios do

随机推荐