Matching CNcontact and Digits Find Friends Swift 3 - swift

I am trying to build iPhone App with digits Find a friend feature
I can get list of matching digitUserID from Digits.
Now I am struggling to match UserID and CNContacts.
Please point any examples to deal this.
As update:
do
{
try contactStore.enumerateContactsWithFetchRequest(CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactMiddleNameKey, CNContactEmailAddressesKey,CNContactPhoneNumbersKey])) {
(contact, cursor) -> Void in
self.results.append(contact)
}
}
catch{
print("Handle the error please")
}
The above I have managed to get all contact but I don't know how to pass a phone number filter into this and get exact contact match with CNContact

Ideally, one would have expected predicate of the CNContactFetchRequest to do the job, but that (still; argh) only accepts a narrow list of predicates defined with CNContact (e.g. CNContact predicateForContacts(matchingName:) or predicateForContacts(withIdentifiers:). It doesn't even accept the block-based NSPredicate.
So, you have to enumerate through, looking for matches yourself, e.g.
let request = CNContactFetchRequest(keysToFetch: [
CNContactGivenNameKey as CNKeyDescriptor,
CNContactFamilyNameKey as CNKeyDescriptor,
CNContactMiddleNameKey as CNKeyDescriptor,
CNContactEmailAddressesKey as CNKeyDescriptor,
CNContactPhoneNumbersKey as CNKeyDescriptor
])
do {
try contactStore.enumerateContacts(with: request) { contact, stop in
for phone in contact.phoneNumbers {
// look at `phone.value.stringValue`, e.g.
let phoneNumberDigits = String(phone.value.stringValue.characters.filter { String($0).rangeOfCharacter(from: CharacterSet.decimalDigits) != nil })
if phoneNumberDigits == "8885551212" {
self.results.append(contact)
return
}
}
}
} catch let enumerateError {
print(enumerateError.localizedDescription)
}
Regarding matching "digit UserID", I don't know what that identifier is (is it a Contacts framework identifier or Digits' own identifier?).

Related

Company field when enumerating contacts through CNContactStore

I'm fetching some contact fields using CNContactStore such as first name (CNContactGivenNameKey) and last name (CNContactFamilyNameKey) but I can't find the key for company.
let request = CNContactFetchRequest(keysToFetch: [
CNContactGivenNameKey as NSString,
CNContactFamilyNameKey as NSString,
])
request.sortOrder = .familyName
do {
let store = CNContactStore()
try store.enumerateContacts(with: request) { contact, stop in
// ...
}
} catch {
print(error)
}
The key is CNContactOrganizationNameKey.
The confusion comes from the fact that the Contacts app (in all iOS, iPadOS and macOS) use the word company while the key refers to it as organization, making it hard to find through auto-complete in Xcode, or when searching for it in Google or Stack Overflow.

How can I add these Firestore fields to a Dictionary?

I am looking to add all my "usernames" into a dictionary. I am having some trouble doing this. I am sure it's very obvious, but I am very new to coding.
I am stuck at, right now and can't seem to find a clear answer anywhere:
func fetchUser() {
let db = Firestore.firestore()
let usernameSearch = db.collection("users")
usernameSearch.getDocuments { (snapshot, error) in
if error != nil {
print("Error obtaining usernames")
} else {
for field in snapshot!.documents {
let field = field.get("username")
print(field!)
}
}
}
}
I would really appreciate it if somebody could help me out. I am sure it's very obvious, or I'm just doing it totally wrong.
First, get into the habit of safely unwrapping over force unwrapping. And choose more accurate names for your objects (i.e. usersCollection over usernameSearch). However, in this case, there's no need to instantiate individual properties for the database and the collection since they're not being used anywhere else but here (so be efficient and omit them).
var usersDictionary = [String: [String]]()
func fetchUser() {
Firestore.firestore().collection("users").getDocuments { (snapshot, error) in
if let snapshot = snapshot { // unwrap the snapshot safely
var usernames = [String]()
for doc in snapshot.documents {
if let username = doc.get("username") as? String {
usernames.append(username)
}
}
usersDictionary["usernames"] = usernames
} else {
if let error = error {
print(error)
}
}
}
}
Or if you actually meant an array of users:
var usersArray = [String]()
func fetchUser() {
Firestore.firestore().collection("users").getDocuments { (snapshot, error) in
if let snapshot = snapshot { // don't force unwrap with !
for doc in snapshot.documents {
if let username = doc.get("username") as? String {
usersArray.append(username)
}
}
} else {
if let error = error {
print(error)
}
}
}
}
I'm assuming that what you're looking for is an Array, not a Dictionary. I'll also assume that you are indeed getting the correct value that you'd expect out of field.get("username"), e.g. a string such as "Bob." Therefore, what you are trying to do is map the list of document objects to a list of strings.
If you scroll to the Topics section of the Array documentation from Apple, you can find some of the operations they provide for arrays such as snapshot!.documents.
One of those operations is actually map, and its description is:
Returns an array containing the results of mapping the given closure over the sequence’s elements.
https://developer.apple.com/documentation/swift/array/3017522-map
In other words, you provide a transformation to perform for each instance of a document belonging to the snapshot!.documents Array and get back a new Array containing the resultant values of that transformation.
In this case I will use a more specific operation; compactMap. We have to try and cast the returned value from Any to String. If that does not succeed, it will return nil, and we'll want to filter that out. I expect it to be an unlikely case due to the type requirements made by the Firebase Console, but it's good to be aware of it. Here is the example:
func fetchUsernames(from usernameCollection: String, completion: #escaping ([String]) -> Void) {
let db = Firestore.firestore()
let collection = db.collection(usernameCollection)
collection.getDocuments { snapshot, error in
guard error != nil,
let usernames = snapshot?.documents.compactMap { $0.get("username") as? String }
else { return print("Error obtaining usernames") }
completion(usernames)
}
}
The key line here being let usernames = snapshot?.documents.compactMap { $0.get("username") }. We are passing the map function a closure. This closure is passed an argument itself; each value from the snapshot?.documents array. You may refer to this passed in value with $0.

Is it possible to create an Array of specific Objects in Parse?

I have made a QR scanner App, I have manually put some QR codes into parse for it to recognise, any QR codes scanned that I haven't put into parse don't get recognised.
The only thing to tell them apart is their (Info) i.e "restaurant", "nail salon" etc.
I am after a way to be able to record an Integer of how many times the chosen QRCode has been scanned, to then place on a label in the app.
I can (.count) ALL of the qrCodes saved and scanned by the user but can't seem to figure out how I can then either put all "Nail Salons" into their own array on parse or run a For loop matching the ones I need.
// The code below will retrieve everything in the "info" column and print it to console
// This prints "Nails Salon" x 5, "Restaurant" x3 and "Coffee Shop" x 7 in the order that they were scanned (Unorganised)
// What block of code could I make to display what PFuser.current currently has in their parse?
// E.g. PFUser has scanned "Nail Salon" 5 Times, "Restaurant" 3 time etc etc
let infoCheck = PFQuery(className: "UserQRCodes")
infoCheck.whereKey("info", contains: "")
infoCheck.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
if let error = error {
print(error.localizedDescription)
} else if let objects = objects {
print(objects)
}
}
// To retrieve everything the USER has scanned and display it as String on the APP
let query = PFQuery(className: "UserQRCodes")
query.whereKey("userName", equalTo: PFUser.current()!)
query.findObjectsInBackground { (objects: [PFObject]?, error: Error?) in
if let error = error {
//log details of the failure
print(error.localizedDescription)
} else if let objects = objects {
let stampees: Int = objects.count
let totalStampees = String(stampees)
self.stampeesCollectedLabel.text = totalStampees
print(objects.count)
}
}
// Do any additional setup after loading the view.
}
You want to filter elements in your array of scans. For each code type, call something like
// '$0' is your PFObject. Replace 'name' with whatever `PFObject` property
// represents the object's type
let nailSalons = objects.filter { $0.name == "Nail Salon" }
You can then use this filtered array to get your count.
Note that the filter { $0... } syntax is a shorthand for
objects.filter { (object) throws -> Bool) in
return object.name == "Nail Salon"
}
You'll need to use the full version if your condition is anything more complicated than a simple one-line expression. Note that in the short version, the return is implied.

Unique usernames in Firebase

I have been trying to implement Chris’ answer here: Can I make Firebase use a username login process? for the Facebook login but I can’t seem to get my head around it.
So far I’ve tried to set conditions on the textField but as Firebase observer works asynchronously, the conditions to check if the username exists in the database won’t work.
let usernameString = usernameTextField.text
let uid = FIRAuth.auth()?.currentUser?.uid
ref.runTransactionBlock({ (currentData: FIRMutableData) -> FIRTransactionResult in
if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid {
let usernamesDictionary = post["usernames"] as! NSDictionary
for (key, _) in usernamesDictionary {
if key as? String == usernameString {
print("username not available: \(key)")
}
else if usernameString == "" {
print("Uh oh! Looks like you haven't set a username yet.")
}
else if key as? String != usernameString {
print("username available: \(key)")
print("All set to go!")
let setValue: NSDictionary = [usernameString!: uid]
post["usernames"] = setValue
currentData.value = post
}
}
return FIRTransactionResult.successWithValue(currentData)
}
return FIRTransactionResult.successWithValue(currentData)
}
Then I tried creating /usernames/ node in the database and set up rules as:
{
"rules": {
"usernames": {
".read": "auth != null",
".write": "newData.val() === auth.uid && !data.exists()"
}
}
}
Now that won’t let me set any username to the database. I get confused in creating rules but my whole point is that I need a sign up flow with the username data that’s unique for each user in the database.
While trying every answer I found in related posts, what worked for me the easy way i.e. without making Firebase rules play a part in it or creating a separate usernames node in the database was to not put an if/else condition inside the Firebase observer but instead to use the exists() method of FIRDataSnapshot.
Now here’s the trick, while I did try only the exists() method with a simple observer but that did not help me. What I did was first query usernames in order, then match the username with queryEqualToValue to filter the query:
refUsers.queryOrderedByChild("username").queryEqualToValue(usernameString).observeSingleEventOfType(.Value , withBlock: {
snapshot in
if !snapshot.exists() {
if usernameString == "" {
self.signupErrorAlert("Uh oh!", message: "Looks like you haven't set a username yet.")
}
else {
// Update database with a unique username.
}
}
else {
self.signupErrorAlert("Uh oh!", message: "\(usernameString!) is not available. Try another username.")
}
}) { error in
print(error.localizedDescription)
}
}
This is the first time out of most of the answers here that worked for me. But for now, I don’t know if this would scale. Post your experiences and best practices. They’ll be appreciated.

How to know where Optional Chaining is breaking?

So in iOS Swift we can do optional chaining to simplify the nil checking like in the official documentation
let johnsAddress = Address()
johnsAddress.buildingName = "The Larches"
johnsAddress.street = "Laurel Street"
john.residence!.address = johnsAddress
if let johnsStreet = john.residence?.address?.street {
println("John's street name is \(johnsStreet).")
} else {
println("Unable to retrieve the address.")
}
// prints "John's street name is Laurel Street."
I understand about the usage of the optional chaining in john.residence?.address?.street but how can we know where is actually the chain is breaking (if either residence or address is nil). Can we determine if residence or address is nil, or we need to check it again with if-else?
Don't do the chain then. The chain is only interesting if you don't intend to stop. If you do, break it up at interesting spots - otherwise, where would you put in the code for the bad cases?
if let residence = john.residence {
if let address = residence.address {
...
} else {
println("missing address")
}
} else {
println("missing residence")
}
I don't think so. Optional chaining is a convenient shortcut syntax and you are giving up a bit of control along with a couple of keypresses.
If you really need to know where exactly the chain breaks, you have to go link by link into multiple variables.
There is no way to tell where optional chaining stopped. However you can use:
if let johnsStreet = john.residence?.address?.street {
println("John's street name is \(johnsStreet).")
} else if let johnAddress = john.residence?.address {
println("Unable to retreive street, but John's address is \(johnAddress).")
} else {
println("Unable to retrieve the address.")
}