如何从一个使用firebase设置的IOS应用程序读取/写入另一个Firebase项目中包含的另一个Firebase数据库? Swift 3
我有一个Firebase数据库连接到我的iOS应用程序与GoogleService-Info.plist。 在AppDelegate中,我configuration了应用FIRApp.configure()。 我可以读/写数据。
现在,从这个IOS应用程序,我想访问另一个FireBase数据库brevCustomer
。 出于某种原因, let dbRef
来自viewDidLoad
let dbRef
在Xcode中有一个标志,说这个“不可变的值dbRef
从来没有使用过”,而应用程序在有趣的startObserving() dbRef.observe(.value, with: { (snapshot: FIRDataSnapshot) in
。
任何人都可以展示如何进行configuration,以便我可以读取/写入到brevCustomer数据库?
编辑
请考虑以下情况:
-
我有两个IOS应用程序客户和工作人员,以及两个名为CustomerFireBase和WorkerFirebase的 Firebase项目,我希望他们以下面的方式工作。
-
客户使用电子邮件和密码注册,login,预订,数据保存在CustomerFireBase中。
- 工作者注册电子邮件和密码,日志是,观察WorkerFirebase的价值改变或添加子
- 从CustomerFireBase读取
- 写给CustomerFireBase
- 写给WorkerFirebase
- 从CustomerFireBase读取
我怎样才能做到这一点? 基本上,我需要从一个通过Firebaseconfiguration的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
回答问题和意见。
如您所知,当用户注册Firebase时,会在Firebase服务器上创build用户帐户,并为用户提供用户ID(uid)。
典型的devise模式是在Firebase中有一个/用户节点,用于存储有关用户的其他信息,例如昵称,地址或电话号码。
我们可以利用这个/用户节点来指明它是什么types的用户; 工作者或客户端,这将绑定到应用程序和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"
正如你所看到的,约翰,乔治和林戈都是工人,保罗是客户。
当用户login时,Firebaseloginfunction将返回包含uid的用户身份validation数据。
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 } })
如果应用程序数据是这样分开的
app client_data ... worker_data ...
可以设置一个简单的规则来validation用户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'"
- 使用AFNetworking重置基本authentication凭证
- 如何在iOS应用程序中对使用Facebook创build帐户的Instagram用户进行身份validation?
- Ionic Framework和Firebase 3.x版本:此域未授权用于您的Firebase项目的OAuth操作
- 了解Google身份validation器应用程序的工作原理
- 在viewDidDisappear中删除Firebase身份validation
- Phonegap的iOS AJAX请求永远不会完成
- 如何在新iTuneconnect中创build应用内购买testing用户?
- Firebase身份validation – 获取提供商ID
- 需要Firebase重新validation