우선, 특정 문자를 데이터베이스에 저장할 수 없다는 것이 맞습니다. 당신이 당신의 자신의 키를 만들 경우, 중포 기지 문서에 따르면, (https://firebase.google.com/docs/database/ios/structure-data)
, 그들은 UTF-8 인코딩은 768 바이트의 최대 수 있으며, 포함 할 수 있어야합니다., $, #, [,] ,/또는 ASCII 제어 문자 0-31 또는 127입니다.
그러나 "@"기호는 그 중 하나가 아닙니다. 점에 관해서는 점을 다른 문자로 대체하는 기능을 사용할 수 있습니다. 예를 들어, "•". 다음은 예입니다.
func createNewEmail(oldEmail: String) -> String{
return oldEmail.componentsSeparatedByString(".").joinWithSeparator("•")
}
"email @ example"및 "com"의 배열로 이메일을 분할합니다. 그런 다음 새로운 문자로 새 문자를 다시 결합하여 "email @ example • com"을 만듭니다.
이전 전자 메일이 필요한 경우 역순으로 동일한 작업을 수행 할 수 있습니다.
func getOldEmail(newEmail: String) -> String{
return newEmail.componentsSeparatedByString("•").joinWithSeparator(".")
}
그런 다음 마지막으로, 원래의 질문에 대해,이 함수는 그러나이
func userLookUpByEmail (email: String) -> String {
let newEmail = createNewEmail(email)
var userID: String = "nil"
ref.child("emails").child(newEmail).observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
if snapshot.value != nil {
print(snapshot.value)
userID = snapshot.value as! String
}
else {
print ("user not found")
userID = "nil"
}
})
return userID
}
처럼 쓸 수있는이
emails:
[email protected]•com: UID123456789
처럼 나무를 포맷 할 수있는 것이 가능 너야 매회 "무"을 얻을 것입니다. 이것은 observeSingleEventOfType이 클로저이기 때문입니다. 이는 앱의 백그라운드에서 실행됨을 의미합니다.이 때문에 사용자 ID가 클로저 내부에서 변경되기 전에 반환 될 수 있습니다. userID를 반환하는 대신 closure 내부의 userID를 기반으로 모든 코드를 실행할 수 있습니다. 대신이 일을 예를 들어
func userLookUpByEmail (email: String) -> String {
let newEmail = createNewEmail(email)
var userID: String = "nil"
ref.child("emails").child(newEmail).observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
if snapshot.value != nil {
print(snapshot.value)
userID = snapshot.value as! String
}
else {
print ("user not found")
userID = "nil"
}
})
return userID
}
doSomethingWith(lookUserUpByEmail([email protected]))
이 작업을 수행 할 수 있습니다 :
func userLookUpByEmail (email: String){
let newEmail = createNewEmail(email)
var userID: String = "nil"
ref.child("emails").child(newEmail).observeSingleEventOfType(.ChildAdded, withBlock: { snapshot in
if snapshot.value != nil {
print(snapshot.value)
userID = snapshot.value as! String
doSomethingWith(userID)
}
else {
print ("user not found")
userID = "nil"
doSomethingWith(userID)
}
})
}
귀하의 데이터 구조는 유효 JSON 없습니다. 올바른 JSON으로 질문을 업데이트하십시오. –
이런 식으로 가장 간단한 데이터 구조가 원하는 것을 할 수 있습니다. http://stackoverflow.com/questions/37897656/firebase-security-find-other-users-via-email/37909700#37909700 –