0

I need some help using APAddressBook.

I can't understand how to use APAddressBook to load and show contacts.

I think this function gets all contacts from address book

func loadContacts() {
    self.addressBook.loadContacts({
        (contacts: [APContact]?, error: NSError?) in
        if let unwrappedContacts = contacts {
            print(unwrappedContacts)

        } else {
            // show error...

And this function extracts names

func contactName(contact :APContact) -> String {
    if let firstName = contact.name?.firstName, lastName = contact.name?.lastName {
        return "\(firstName) \(lastName)"
    }
    else if let firstName = contact.name?.firstName {
        return "\(firstName)"
    }
    else if let lastName = contact.name?.lastName {
        return "\(lastName)"
    }
    else {
        return "Unnamed contact"
    }
}

What I don't understand how to display those names here:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("contacts", forIndexPath: indexPath) as! ContactsTableViewCell

    // Configure the cell...
    cell.contactsNumberLabel.text = contactsListNumbers[indexPath.row]
    cell.contactsNameLabel.text = contactListNames[indexPath.row]

    return cell
}
Xernox
  • 1,706
  • 1
  • 23
  • 37

2 Answers2

0

I am not too sure about this library however we could see that unwrappedContacts is an array which contains all the informations about each user from your contactBook. so our best bet will be to iterate through this array, decompose it then assign it to new array.

According to some reseach I did it says that this library contains fews fields such as

Available fields: APContactFieldFirstName - contact first name APContactFieldMiddleName - contact middle name APContactFieldLastName - contact last name APContactFieldCompany - contact company (organization) APContactFieldPhones - contact phones array

struct ContactStructure
{
 var firstname:String?
 var lastname:String?
}

    var arrayOfContacts = [ContactStructure]()

      for each in unwrappedContacts
     {
        var firstname = each.APContactFieldFirstName as! String   
        var lastname  =  each.APContactFieldLastName as! String
        var SingleContact = ContactStructure()
        singleContact.firstname = firstname
        singleContact.lastname = lastname 
        arrayOfContacts.append(SingleContact)
        // Then reloadData of the tableView
     }

Note each is a single object from the array so when you do each. you should look to see the method and properties that this class or struct holds

Then you will used that arrayOfContacts to populate your cells of the tableview

Lamour
  • 3,002
  • 2
  • 16
  • 28
  • Thanks, at first it didn't worker, but if instead of `each.APContactFieldFirstName as! String` I used `each.name?.firstName` and now I have an array of contact names. – Xernox Dec 03 '15 at 08:14
  • However I have problems populating cells. What I did was declared `ContactStructure ` and `arrayOfContacts` at the beginning of my class, inserted `for each in unwrappedContacts` in `loadContacts()` and for `cell.contactsNameLabel.text` tried to assign `arrayOfContacts[indexPath.row]`. What I got was an error: `Cannot subscript a value of type '[ContactsTableViewController.ContactStructure]'` I guess I am using `struct` wrong? – Xernox Dec 03 '15 at 08:23
0

Here is my pretty much my full code:

class ContactsTableViewController: UITableViewController {

let addressBook = APAddressBook()

struct ContactStructure {

    var firstname:String?
    var lastname:String?
}

var arrayOfContacts = [ContactStructure]()

override func viewDidLoad() {
    super.viewDidLoad()

    loadContacts()        
}

func loadContacts() {
    self.addressBook.loadContacts({
        (contacts: [APContact]?, error: NSError?) in
        if let unwrappedContacts = contacts {

            for each in unwrappedContacts {

                let firstname = each.name?.firstName
                let lastname  =  each.name?.lastName
                var singleContact = ContactStructure()
                singleContact.firstname = firstname
                singleContact.lastname = lastname
                self.arrayOfContacts.append(singleContact)
            }

        } else if let unwrappedError = error {
            let alert = UIAlertView(title: "Error", message: unwrappedError.localizedDescription,
                delegate: nil, cancelButtonTitle: "OK")
            alert.show()
        }
    })
}

override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
    return 1
}

override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // for now 1 should be fine
    return 1
}

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! ContactsTableViewCell

    //Here I get error Cannot subscript a value of type '[ContactsTableViewController.ContactStructure]'

    cell.nameLabel.text = arrayOfContacts[indexPath.row]

    return cell
}
Xernox
  • 1,706
  • 1
  • 23
  • 37