简体   繁体   English

如何从设置有Firebase的一个IOS App读取/写入另一个Firebase项目中包含的另一个Firebase数据库? 迅捷3

[英]How to read/write from one IOS App set up with firebase, to another firebase database contained in another firebase project? Swift 3

I have a Firebase database connected to my IOS app with the GoogleService-Info.plist. 我有一个Firebase数据库,它通过GoogleService-Info.plist连接到我的IOS应用程序。 In AppDelegate I configured the App FIRApp.configure(). 在AppDelegate中,我配置了App FIRApp.configure()。 I could read/write data. 我可以读/写数据。

Now, from within this IOS app, I would like to access another FireBase Database brevCustomer . 现在,从这个IOS应用程序中,我想访问另一个FireBase数据库brevCustomer For some reason let dbRef from viewDidLoad has a flag in Xcode saying this 'immutable value dbRef was never used' and the app crashes on the first line in fun startObserving() dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in . 出于某种原因, let dbRef viewDidLoad中的let dbRef在Xcode中带有一个标志,表明此“从未使用过不可变的值dbRef ”,并且应用程序在startObserving() dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in )的第一行崩溃dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in

Could anyone show how to do the configuration so that I can read/write to brevCustomer database? 谁能显示如何进行配置,以便可以读写brevCustomer数据库?

EDIT 编辑

Please consider the following scenario: 请考虑以下情形:

  • I have two IOS apps Customer and Worker and two Firebase Projects named CustomerFireBase and WorkerFirebase and I would like them to work in the following way. 我有两个IOS应用程序CustomerWorker,以及两个名为CustomerFireBaseWorkerFirebase的 Firebase项目,我希望它们以以下方式工作。

  • Customer registers with email and password, logs in, makes a booking, and data is saved in CustomerFireBase. 客户使用电子邮件和密码注册,登录,进行预订,然后将数据保存在CustomerFireBase中。

  • Worker registers with email and password, logs is, observe WorkerFirebase for value changes or child added 工作人员使用电子邮件和密码注册,日志是,观察WorkerFirebase的值更改或添加子项
    • read from CustomerFireBase 从CustomerFireBase读取
      • write to CustomerFireBase 写入CustomerFireBase
      • write to WorkerFirebase 写入WorkerFirebase

How can I achieve this? 我该如何实现? Basically, I need to get read/write access from one IOS app configured in the usual way with Firebase, to another Firebase Database contained in another Firebase project. 基本上,我需要从使用Firebase 按常规方式配置的一个IOS应用程序获得对另一个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

Responding the question and comments. 回答问题和评论。

As you know, when a user registers with Firebase, a user account is created on the Firebase server and the user is provided a user id (uid). 如您所知,当用户向Firebase注册时,会在Firebase服务器上创建一个用户帐户,并为该用户提供用户ID(uid)。

A typical design pattern is to have a /users node in Firebase that stores other information about the user, such as a nickname, address or phone number. 一种典型的设计模式是在Firebase中拥有一个/ users节点,该节点存储有关用户的其他信息,例如昵称,地址或电话号码。

We can leverage that /users node to also indicate what kind of user it is; 我们可以利用那个/ users节点来指出它是什么样的用户。 Worker or Client, which would tie into the rest of the app and Firebase so they get to the correct data. Worker或Client,将它们与应用程序和Firebase的其余部分绑定在一起,以便它们获取正确的数据。

For example 例如

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"

As you can see, John, George and Ringo are all workers and Paul is a client. 如您所见,John,George和Ringo都是工人,Paul是客户。

When the user logs in, the Firebase signIn function will return the users auth data, which contains the uid. 当用户登录时,Firebase登录功能将返回用户的身份验证数据,其中包含uid。

    Auth.auth().signIn(withEmail: "paul@harddaysnight.com", 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
        }
    })

If the app data is divided like this 如果应用数据是这样划分的

app
  client_data
     ...
  worker_data
     ...

A simple rule could be set up that verifies the users user_type is Worker for the worker_data node and Client for the client_data node. 可以设置一个简单的规则,以验证用户user_type对于worker_data节点是Worker,对于Client_data节点是Client。 Here's a pseudo example that will allow a Client user to only access the data in the client_data node (conceptual) 这是一个伪示例,该示例将允许Client用户仅访问client_data节点中的数据(概念性)

rules 
  client_data
     $user_id
        ".read": "auth != null && root.child(users)
                                      .child($user_id)
                                      .child("user_type") == 'Client'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从另一个 firebase 应用程序获取 Firebase 数据库 - Get Firebase database from another firebase app iOS应用程序能否通过Firebase从另一个项目收到通知 - Could a iOS app received Notifiction from another project with Firebase Firebase-将应用程序从iOS开发者转移到另一个 - Firebase - Transferred App From iOS Developer to another 带有 Firebase 数据库或 Firestore 读/写的 Swift 4 - Swift 4 with Firebase database OR Firestore read/write iOS Swift FIrebase:将数据移动到另一个firebase引用 - iOS Swift FIrebase: moving data to another firebase ref 在一个iOS应用程序中使用Gmail API和Firebase另一个应用程序的数据库 - Use Gmail API and Firebase's database of another app in one iOS application 是否可以从另一个框架将 Firebase 添加到 iOS 项目? - Is is possible to add Firebase to an iOS project from another framework? 使用Firebase将图像从一个视图控制器传递到另一个(swift) - pass an image from one view controller to another (swift) with Firebase Firebase和Swift:如何使用另一个数据库存储更大的文件? - Firebase & Swift: How to use another database for storing larger files? 如何从ios(swift)上的tablecell中的firebase读取图像? - How to read an image from firebase in tablecell on ios (swift)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM