set correct CNContact.predicateForContacts to select email - swift

What is the correct way to set CNContact.predicateForContacts to select the field "email" in CNContacts? Like in SQL where email like "%lbs%"?
This is my function to delete a lot of imported contacts I want to get rid of. But the function return 0 hits. But there are more than 1000 contacts with "lbs" in the field email.
func deleteContacts(){
let store = CNContactStore()
let predicate = CNContact.predicateForContacts(matchingName: "lbs")
let toFetch = [CNContactEmailAddressesKey]
do{
let contacts = try store.unifiedContacts(matching: predicate,keysToFetch: toFetch as [CNKeyDescriptor])
guard contacts.count > 0
else{
print("No contacts found")
return
}
guard let contact = contacts.first else{
return
}
let req = CNSaveRequest()
let mutableContact = contact.mutableCopy() as! CNMutableContact
req.delete(mutableContact)
do{
try store.execute(req)
print("Success, deleted the data: Count: \(contacts.count)")
} catch let e{
print("Error = \(e)")
}
} catch let err{
print(err)
}
}

Your predicate is trying to find contacts where the person's name matches the string lbs.
There is no built-in predicate for finding contacts that have an email address containing a specific string. The solution is to use enumerateContacts and look at each individual contact's list of email addresses. You will then need to check to see if any of the contact's email address contains the string you wish to check.

Related

refrence to document ID

#IBAction func NextButtonTapped(_ sender: Any) {
//validate the fileds
let Error = validateFields()
if Error != nil {
// there is somthing wrong with the fields show error message
showError(Error!)
}
else {
// create cleaned versions of the data
let Password = PasswordTextField.text!.trimmingCharacters(in:
.whitespacesAndNewlines)
let Email = EmailTextField.text!.trimmingCharacters(in:
.whitespacesAndNewlines)
let Firstname = FirstnameTextField.text!.trimmingCharacters(in:
.whitespacesAndNewlines)
let Lastname = LastnameTextField.text!.trimmingCharacters(in:
.whitespacesAndNewlines)
let Age = AgeTextField.text!.trimmingCharacters(in:
.whitespacesAndNewlines)
// create the user
Auth.auth().createUser(withEmail: Email, password: Password) {
(results, Err) in
// check for errors
if Err != nil {
// there was an error creating the user
self.showError("Error creating user")
}
else {
// user was created succesfully store user info
let db = Firestore.firestore()
db.collection("users").document(results!.user.uid).setData(["first
name":Firstname, "last name":Lastname, "age":Age,
"uid":results!.user.uid]) { (Error) in
if Error != nil {
// show error message
self.showError("error saving user data")
}
}
//transition to the home screen
self.transitionToHome()
}
}
}
}
So basically here I am authenticating the user on firebase. (I made
the document ID = the user ID) then I am entering the users info
into the firebase database into a document where its ID is also
equal to the users ID from when they are authenticated. Now what I
am trying to do is to create or get a reference to the document ID
where some of the users info is already stored like name, last name,
age... so I can later add/merge more info into that same document
the name, last name and age is stored under. This is how I am trying
to merge the info together in a diffrent view controller
db.collection("users").document(*******).setData(["middle
Name":Middlename, "favourite colour":Favouritecolour], merge: true)
{ (Error) in
if Error != nil {
// show error messgae
self.showError("error saving user data")
}
}
Where I put "*******" is where I am supposed to reference the
document ID so I can merge/add this info into the same document as
the other users information where the name, last name and age is
stored.
The code I showed you and asked about earlier on how to get a
document ID from, was code I found on stack overflow where they guy
had a similar problem as mine. He was trying to access something out
of his document but I am just trying to create a reference to the
document ID.
The code form earlier:
func getDocument() {
//get specific document from current user
let docRef =
Firestore.firestore().collection("users").document(Auth.auth().currentUs er?.uid ?? "")
//get data
docRef.getDocument { (document, Error) in
if let document = document, document.exists {
let dataDescription = document.data()
print(dataDescription?["uid"])
} else {
print("Document does not exist")
}
}
}
But I don't know if this code will help me, I just thought it might
because its also accessing the document, thats why I though it might
be my answer to getting the document ID.
So basically my question is what do I need to do wether its adding
to the code I found on this site, or if I have to write my own code,
so I can get a reference to the Document ID where my name, last name
and Age is stored, so I can merge more Info into that document
Thank You Very Much!!!
To print the document ID do something like this:
let docRef = Firestore.firestore().collection("users").document(Auth.auth().currentUser?.uid ?? "")
// Get data
docRef.getDocument { (document, error) in
if let document = document, document.exists {
let dataDescription = document.data()
print(dataDescription?["firstname"])
print(document.documentID)
} else {
print("Document does not exist")
}
}
I highly recommend spending some time in the Firestore documentation, as this is covered in there under getting multiple documents from a collection

Document ID retrieval

this is supposed to take the user ID from the result!.user.uid and store it a variable or function in order for me to use it again.
the problem is that I dont know how to get it to store the value outside of this function.
Ive tried to make it store to a variable outside of the initial button function, and Ive also tried to return it outside of the function by removing a part of the code which made it become a void. Im not sure where i need to go/what else I can try and do in order to fix this problem.
If anybody know how do I retrieve my document ID from this code your help would be greaty appreciated
#IBAction func NextButtonTapped(_ sender: Any) {
//validate the fileds
let Error = validateFields()
if Error != nil {
// there is somthing wrong with the fields show error message
showError(Error!)
}
else {
// create cleaned versions of the data
let Password = PasswordTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let Email = EmailTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let Firstname = FirstnameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let Lastname = LastnameTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
let Age = AgeTextField.text!.trimmingCharacters(in: .whitespacesAndNewlines)
// create the user
Auth.auth().createUser(withEmail: Email, password: Password) { (results, Err) in
// check for errors
if Err != nil {
// there was an error creating the user
self.showError("Error creating user")
}
else {
// user was created succesfully store first and last name
let db = Firestore.firestore()
db.collection("users").document(results!.user.uid).setData(["first name":Firstname, "last name":Lastname, "age":Age, "uid":results!.user.uid]) { (Error) in
if Error != nil {
// show error message
self.showError("error saving user data")
}
//showing users document id
}
//transition to the home screen
self.transitionToHome()
}
}
}
}
I have no idea what to do any help would be amazing,
thank you very much!!!!
Define a uid variable outside of the IBAction function like so.
var uid: String? = nil
Then, within the createUser function
self.uid = results!.user.uid

Iterate through emails in Firebase - Swift

I am currently trying to iterate through all user emails in firebase, however, whenever I run my code, and try to add the "test#gmail.com" user, there is an error. However, when I try to add my current user's email address, "test1#gmail.com", there is a success.
Below is a snippet of my code demonstrating this. B
Below is also an image showing the structure of my current database.
Note that each user's email is under a unique userID under the "users" part of the database.
Iterating through email snippet.
func searchEmails() {
var ref : DatabaseReference
let currentUserID = Auth.auth().currentUser?.uid
ref = Database.database().reference()
let userRef = ref.child("users")
userRef.observeSingleEvent(of: .value, with: { snapshot in
let enumerator = snapshot.children
while let rest = enumerator.nextObject() as? DataSnapshot {
userRef.child(rest.key).child("email").observe(.value, with: { snapshot in
print(snapshot.value!)
// print("Other rest is this \(otherRest.value!) value")
if(snapshot.value as? String == self.shareEmail.text) {
SVProgressHUD.showSuccess(withStatus: "Request sent to user!")
}
else {
SVProgressHUD.showError(withStatus: "Email not valid.")
}
})
}
})
SVProgressHUD.dismiss()
}
Why don't you try this, Might turn out to be much less headache.. :-
if self.shareEmail.text != "" && self.shareEmail.text.isEmpty == false{
Database.database().reference().child("users").queryOrdered(byChild: "email").queryEqual(toValue: "somemail").observe(.value, with: {(Snapshot) in
if Snapshot.exists(){
// The email that you have been searching for exists in the
// database under some particular userID node which can
// be retrieved from ....
print(Snapshot)
}else{
// No such email found
}
}, withCancel: {(error) in
// Handle any error occurred while making the call to firebase
})
}else{
//Your textfield must not be empty;.... Handle that error
}
Note : This is only gonna work if Firebase Security rules allow it... so you might have to work on that on your console... Good luck!

Firebase query can't find email address?

I'm have a key in my Firebase database called "users" which contain keys for email and name. I'm trying to create a simple query that will find a user based on email address, but the query is not returning any results.
let rootRef = FIRDatabase.database().reference()
let email = emailTextField.text
rootRef.child("users").queryEqualToValue(email, childKey: "users").observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
print("user exists")
} else if !snapshot.exists(){
print("user doesn't exist")
}
})
My console always prints "user doesn't exist" even if the email text field is identical to what is shown in my database.
Any ideas as to what's wrong?
Thanks!!
Try this:
let usersRef = self.rootRef.childByAppendingPath("users")
let email = emailTextField.text
usersRef.queryOrderedByChild("email").queryEqualToValue(email)
.observeEventType(.Value, withBlock: { snapshot in
if snapshot.exists() {
print("user exists")
} else {
print("user doesn't exist")
}
})
You need to be careful though... .Value will return all nodes that have an email address equal to the one you are looking for. In general that would be one, however, if there are 5, .Value will contain all 5 so it would be safer to iterate over the snapshot.
for child in snapshot.children {
let email = child.value["whatever key:value you want"] as! String
}

iPhone: Create Custom Field in contacts

i am developing a application in which i add contact to contact List. i am able to basic info like name, address, email, phone, notes etc. but i want to add some custom field like userLabel1, userValue1 , bioPersonal, bioWork, bioOther. so i want to add custom fields to address Book'contacts.
whether it is possible to add custom field to contact? if yes , then please suggest any link or sample code?
Basically, there is no way to do that. Address Book doesn't let you add custom fields. However, what you can do is put your data in the "Notes" field for each contact. That will, however, look weird in apps other than yours.
let store = CNContactStore()
// phoneNumberToEdit is the CNContact which you need to update
guard var mutableCont = phoneNumberToEdit?.mutableCopy() as? CNMutableContact else { return }
let phoneNumber = CNPhoneNumber(stringValue: "0123456789")
var currentContact = CNLabeledValue(label: "MyCustomLabel", value: phoneNumber)
mutableCont.phoneNumbers = [currentContact]
let request2 = CNSaveRequest()
request2.update(mutableCont)
do{
try store.execute(request2)
} catch let error{
print(error)
}