I fetched the contacts details and I want to store them into an array .I have tried but it stores like single contact as one array. but I want to store all of them into one array. how to store them.please guide me. First it display all the contacts from our phone and I have selected particular contact.That selected contacts should be stored in an array Thank you
import UIKit
import ContactsUI
class ViewController: UIViewController ,CNContactPickerDelegate {
let contactStore = CNContactStore()
let amount = 200
//var selectedContact:CNContact = CNContact()
//var results:[CNContact] = []
var res:CNContact = CNContact()
var a = [String]()
override func viewDidLoad() {
super.viewDidLoad()
perform(#selector(contactPicker(_:didSelect:)), with: nil, afterDelay: 0)
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
}
@IBAction func contact(_ sender: Any) {
let cnPicker = CNContactPickerViewController()
cnPicker.delegate = self
self.present(cnPicker, animated: true, completion: nil)
do {
try contactStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: [CNContactGivenNameKey as CNKeyDescriptor, CNContactFamilyNameKey as CNKeyDescriptor, CNContactMiddleNameKey as CNKeyDescriptor, CNContactEmailAddressesKey as CNKeyDescriptor,CNContactPhoneNumbersKey as CNKeyDescriptor])) {
(contact, cursor) -> Void in
self.res = contact
///let data = Data(name: contact.givenName)
//self.dataArray?.addObject(data)
}
}
catch
{
print("Handle the error please")
}
}
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
contacts.forEach { contact in
//let storyboard = UIStoryboard(name:"Main",bundle:nil)
a = [contact.givenName]
print("name of contacts in array \(a)")
let n = a.flatMap({$0}).count
print(n)
let res = amount/n
print(res)
for c in a {
print ("split money \(c):\(res)")
}
//print(a.count)
let displayViewController = self.storyboard?.instantiateViewController(withIdentifier: "DisplayViewControllerr")as! DisplayViewControllerr
displayViewController.b = a
let appDelegate:AppDelegate = UIApplication.shared.delegate as! AppDelegate
appDelegate.window?.rootViewController = displayViewController
self.navigationController?.pushViewController(displayViewController, animated: true)
//self.present(displayViewController,animated:false,completion: nil)
}
}
func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
print("Cancel Contact Picker")
}
}
I got the answer and successfully stored contacts into array. just modify one line in the program.That is
func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
contacts.forEach { contact in
a+= [contact.givenName]//I changed at this place
}
print(a)
It displays all the selected contacts into array.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments