如何从一个使用firebase设置的IOS应用程序读取/写入另一个Firebase项目中包含的另一个Firebase数据库? Swift 3 [英] How to read/write from one IOS App set up with firebase, to another firebase database contained in another firebase project? Swift 3
问题描述
现在,从这个IOS应用程序中,我想访问另一个FireBase数据库 brevCustomer
。出于某种原因让来自
在Xcode中有一个标志,表示这个'不可变的值 viewDidLoad
的dbRef dbRef
从来没有被使用过,并且应用程序在第一行有趣的startObserving() dbRef.observe(.value,其中:{(snapshot:FIRDataSnapshot)in $ c
$ p
$ b $ p
$ b编辑
请考虑以下情况:
$ b $我有两个IOS应用程序客户和工作和两个名为 CustomerFireBase 和 WorkerFirebase 我希望他们按以下方式工作:
我怎样才能做到这一点?基本上,我需要通过配置的以通常的方式配置一个IOS应用程序的读/写访问与Firebase一起转换为另一个Firebase项目中包含的另一个Firebase数据库。
类声明{
var DBREF:FIRDatabaseReference! //创建对Firebase数据库brevCustomer的引用,而不是.plist文件中的引用
覆盖func viewDidLoad(){
super.viewDidLoad()
let app = FIRApp(named:brevCustomer)
let dbRef = FIRDatabase.database(app:app!)。reference()。child(Users)
startObservingDB()//观察数据库对于值变化
func startObservingDB(){
//它在
下面的行崩溃dbRef.observe(.value,其中:{(snapshot: FIRSTDataSnapshot)
$ b $ //遍历每个用户节点child
在snapshot.children {
print(user_child)}
}中的user_child,withCancel
print(Error)
))
} // startObservingDB()
结束{//错误:任何)//结束声明class
class AppDelegate:UIResponder,UIApplicationDelegate {
var window:UIWindow?
func application(_ application:UIApplication,didFinishLaunchingWithOptions launchOptions:[UIApplicationLaunchOptionsKey:Any]?) - > Bool {
//使用Firebase库为保存在Xcode中的.plist文件配置初始项目的API
FIRApp.configure()
/ ** 1.创建一个Firebase选项对象来保存第二个Firebase项目的配置数据* /
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:co m.vivvdaplar.Brev,
deepLinkURLScheme:nil)
//配置应用程序
FIRApp.configure(withName:brevCustomer,options:secondaryOptions!)
返回true
}
} //结束AppDelegate
如您所知,当用户注册Firebase时,会在Firebase服务器上创建用户帐户,用户提供了一个用户标识(uid)。
一个典型的设计模式是在Firebase中有一个/ users节点,用于存储关于用户的其他信息,例如昵称,地址或电话号码。
我们可以利用这个/ users节点来指出它是什么类型的用户;工作者或客户端,这将绑定到应用程序和Firebase的其余部分,以便他们获得正确的数据。
例如
users
uid_0
昵称:John
user_type:Worker
uid_1
昵称: Paul
user_type:Client
uid_2
昵称:George
user_type:Worker
uid_3
昵称:Ringo
user_type:Worker
正如你所看到的,John,George和Ringo都是工人, Paul是一个客户端。
当用户登录时,Firebase登录功能将返回包含uid的用户身份验证数据。
Auth.auth()。signIn(withEmail:paul@harddaysnight.com,password:dog,
completion:{(auth,错误)in
if error!= nil {
let err = error?.localizedDescription
print(err!)
} else {
print AUTH!的.ui d)
//用uid,我们现在从
// users节点查找他们的用户类型,它告诉应用程序它们是客户
//还是worker
如果应用程序的数据像这样分开的话
$
$ b app
client_data
...
worker_data
...
$ b 可以设置一个简单的规则来验证用户user_type是worker_data节点的Worker和client_data节点的Client。下面是一个允许客户端用户只访问client_data节点中的数据的伪示例(概念性的)
规则
客户数据
$ user_id
.read:auth!= null&& root.child(users)
.child($ user_id)
.child( user_type)=='Client'
I have a Firebase database connected to my IOS app with the GoogleService-Info.plist. In AppDelegate I configured the App FIRApp.configure(). I could read/write data.
Now, from within this IOS app, I would like to access another FireBase Database 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
.
Could anyone show how to do the configuration so that I can read/write to brevCustomer database?
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.
Customer registers with email and password, logs in, makes a booking, and data is saved in CustomerFireBase.
- Worker registers with email and password, logs is, observe WorkerFirebase for value changes or child added
- read from CustomerFireBase
- write to CustomerFireBase
- write to 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.
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).
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.
We can leverage that /users node to also indicate what kind of user it is; Worker or Client, which would tie into the rest of the app and Firebase so they get to the correct data.
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.
When the user logs in, the Firebase signIn function will return the users auth data, which contains the 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. Here's a pseudo example that will allow a Client user to only access the data in the client_data node (conceptual)
rules
client_data
$user_id
".read": "auth != null && root.child(users)
.child($user_id)
.child("user_type") == 'Client'"
这篇关于如何从一个使用firebase设置的IOS应用程序读取/写入另一个Firebase项目中包含的另一个Firebase数据库? Swift 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!