0

I am loading my tblView with contacts from phone. On tapping a row in that tblView, I can get the name on which it is tapped.

I want to know how to get the contact number on tapping.

After permission, I am storing contacts in array arrContatcs from this:

let allContacts = ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as Array
    for record in allContacts {
        let currentContact: ABRecordRef = record
        let currentContactName = ABRecordCopyCompositeName(currentContact)?.takeRetainedValue()
            as? String
        if(currentContactName != nil) {
            self.arrContacts.append(currentContactName! as String)
        }
     }

Loading the contacts in tblView from this:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {
    var cell:FriendsCustomCell = self.tblView.dequeueReusableCellWithIdentifier("SimpleTableViewIdentifier") as! FriendsCustomCell
    cell.tag = indexPath.row;
    cell.lblName!.text = self.arrContacts[indexPath.row] as? String
}

Am printing the name from this:

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let indexPath = NSIndexPath(forRow: indexPath.row, inSection: 0)
    println("tap name = \(self.arrContacts[indexPath.row])")
}

Now Output is tap name = John

My Expected Output is tap name = John and number = ????????

How can I get it?

AAA
  • 1,957
  • 4
  • 23
  • 42
  • see this link for get contacts http://stackoverflow.com/questions/24752627/accessing-ios-address-book-with-swift-array-count-of-zero – Anbu.Karthik Sep 05 '15 at 10:13
  • @Anbu.Karthik sorry, i still dont understand how can i do that – AAA Sep 05 '15 at 10:22
  • 1
    your coding contains u get only Name, the link contains u get number, mail, etc, if u follow that link you will get contact number , but your coding is correct – Anbu.Karthik Sep 05 '15 at 10:23

1 Answers1

0

I don't know swift syntax but I can give you a general idea.

First of all,you are only adding name in the array. Create a dictionary with 'name' and 'number' keys , assign them name and number, then add it to your array.

let allContacts = ABAddressBookCopyArrayOfAllPeople(addressBookRef).takeRetainedValue() as Array
for record in allContacts {
    let currentContact: ABRecordRef = record
    let currentContactName = ABRecordCopyCompositeName(currentContact)?.takeRetainedValue()
        as? String
    let currentContactNumber = ABRecordCopyCompositeNumber(currentContact)?.takeRetainedValue()
        as? String

    if(currentContactName != nil && currentContactNumber!=nil) {
    //Create an array here with name and number. Objective-C code
        NSDictionary *dict = [[dict alloc]init];
        [dict setValue:currentContactName forKey:@"name"];
        [dict setValue:currentContactNumber forKey:@"number"];

       //Syntax can be wrong here. add dictionary to array
       self.arrContacts.append(dict! as Dictionary)
    }
 }

Now display it in table with corresponding keys:

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {
    var cell:FriendsCustomCell = self.tblView.dequeueReusableCellWithIdentifier("SimpleTableViewIdentifier") as! FriendsCustomCell
    cell.tag = indexPath.row;
//Sorry for syntax
    cell.lblName!.text = self.arrContacts[[indexPath.row] ValueForKey@"name"] as? String
    cell.lblNumber!.text = self.arrContacts[[indexPath.row] ValueForKey@"number"] as? String
}

Now Print the name and number on tap

func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
    let indexPath = NSIndexPath(forRow: indexPath.row, inSection: 0)
    println("tap name = \(self.arrContacts[indexPath.row] ValueForKey:@"name"])")
    println("tap Number = \(self.arrContacts[indexPath.row] ValueForKey:@"number"])")
}

Hope you got the general idea.Someone might convert it to Swift syntax.

Bista
  • 7,869
  • 3
  • 27
  • 55
  • Did you convert the above code to full swift syntax? – Bista Sep 06 '15 at 17:02
  • Try putting break-point after 'let currentContactNumber' line and print 'currentContactNumber' to see if there is some value is in there or not. – Bista Sep 06 '15 at 17:22
  • it is giving few names and then crashing. i think it is due to nil value – AAA Sep 06 '15 at 17:28
  • Yes..may be thats the issue...try putting check for nil value and passing empty string in case for nil value. – Bista Sep 06 '15 at 17:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88942/discussion-between-the-ub-and-aaa). – Bista Sep 06 '15 at 17:49