Got caught a problem today.
I am using DigitsKit + Parse, DigitsKit for User real-Validation and Parse as useful framework to make easy sinUp, login etc for users and me as well.
Here is how it works:
Users type their PhoneNumber and Password in textField
After that DigitsKit validate with SMS and short code, that user is real, sms came to USER and he/she type it in validation textField. IF everything is okay -> username and password store in Parse DB as PFUser.
username(in ParseDB) = user'sMobilePhone(in textField)
Than after signUp and login user push the button 'FindFriends' on the new view and here should be the friend-search system, but I have a problem here... Look the case for Controller below please:
import UIKit
import SwiftAddressBook
import AddressBook
import Parse
class FriendsViewController: UIViewController {
let status : ABAuthorizationStatus = SwiftAddressBook.authorizationStatus()
var addrssbk : ABAddressBook!
var addressBook: SwiftAddressBook? = swiftAddressBook
override func viewDidLoad() {
super.viewDidLoad()
GetContactNumbers()
}
override func viewDidAppear(animated: Bool) {
}
func createAddressBook() -> Bool {
if self.addressBook != nil {
return true
}
var err : Unmanaged<CFError>? = nil
let addrssbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, &err).takeRetainedValue()
if addrssbk == nil {
println(err)
self.addressBook = nil
return false
}
self.addrssbk = addrssbk
return true
}
func determineStatus() -> Bool {
let status = ABAddressBookGetAuthorizationStatus()
switch status {
case .Authorized:
return self.createAddressBook()
case .NotDetermined:
var ok = false
ABAddressBookRequestAccessWithCompletion(nil) {
(granted:Bool, err:CFError!) in
dispatch_async(dispatch_get_main_queue()) {
if granted {
ok = self.createAddressBook()
}
}
}
if ok == true {
return true
}
self.addressBook = nil
return false
case .Restricted:
self.addressBook = nil
return false
case .Denied:
self.addressBook = nil
return false
}
}
func GetContactNumbers() {
swiftAddressBook?.requestAccessWithCompletion({ (success, error) -> Void in
if success {
if let people = swiftAddressBook?.allPeople {
for person in people {
if let numbers = person.phoneNumbers {
let names = person.compositeName
NSLog("%@", numbers.map( {$0.value} ))
NSLog("%@", names!)
if let query = PFUser.query() {
query.whereKey("username", equalTo: "\(person.phoneNumbers)")
var friends = query.findObjects()
println(friends)
}
//the value entry of the multivalue struct contains the data
}
}
}
//do something with swiftAddressBook
}else {
//no success. Optionally evaluate error
}
})
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I can't take the each phoneNumber and compare it to user's name in ParseDB, any suggestions how should i don this? Or any hits/solutions?
P.S, I have kind'a troubles with arrays, dictionaries and other array/key-value types of data and loops as well, so any help will be perfect
Solved the problem with using map and stringFormation:
func GetContactNumbers() {
let query = PFUser.query()
swiftAddressBook?.requestAccessWithCompletion({ (success, error) -> Void in
if success {
if let people = swiftAddressBook?.allPeople {
for person in people {
if let numbers = person.phoneNumbers {
let names = person.compositeName
let formatednumbers = numbers.map { $0.value.componentsSeparatedByCharactersInSet(NSCharacterSet.decimalDigitCharacterSet().invertedSet).reduce("", combine: {$0+$1})}
// NSLog("%@", numbers.map( {$0.value} ))
NSLog("%@", names!)
println(formatednumbers)
query!.whereKey("username", equalTo: "\(formatednumbers)")
if let friends = query!.findObjects() {
println(friends)
}
//the value entry of the multivalue struct contains the data
}
}
}
//do something with swiftAddressBook
}else {
//no success. Optionally evaluate error
}
})
}