How to store selected contacts into an array in IOS 9 using swift

vijju

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")
    }



}
vijju

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.

edited at
0

Comments

0 comments
Login to comment

Related

iOS: Using Swift 4 when targeting iOS 9

How to fetch all contacts record in iOS 9 using Contacts Framework

Fetching all contacts in ios Swift?

iOS Swift - How to store array with Core Data?

Swift iOS9 New Contacts Framework - How to retrieve only CNContact that has a valid email address?

How to fetch iOS Contacts more quickly using Apple's Contacts framework having long list of contacts?

How to sort contacts using Contacts with Swift

How to extract pixel data for processing from CMSampleBuffer using Swift in iOS 9?

How to store long in a Swift array?

How to count number of contacts in swift?

iOS Swift: How to store the index of all duplicate values in array?

How to store closures in a Swift array

How to persist email and password textfield info using UserDefaults if checkbox is selected Swift iOS

How to store ios push notifications when the app is killed using swift

How to save selected tableView cell into array in iOS Swift?

How to store selected datepicker javascript using PHP

How to get contacts in iOS?

iOS 9 | Contacts framework

How to delete record in iPhone address book using CONTACTS Framework ios 9?

how to create jabberd xmpp client in ios9 using swift2

Error in Exporting Contacts Using Address Book Framework iOS 9?

IOS Swift Segue using data from selected Cell rather than the supporting array

iOS9 + Swift: How To Set The Body of A Post Request Using a JSON Value?

How to store an array of images in Firebase Storage all under 1 path using iOS Swift

How to Display Collection View inside tableview with custom header action like app store in IOS using Swift

Store captured image in iOS using swift 3

iOS - Print and store array of dictionary in swift 3

how can i store selected rows of tableview in nsuserdefaults in swift 3

How to store selected information in array?