ABAddressBookGetPersonWithRecordID Crashes my App

270 Views Asked by At

The app I'm currently working on has to retrieve the ABRecord of a contact using a delegate, since the table for all the associated contacts of an object is in a container view. So far everything works, the only problem is that when I try to retrieve the record again after moving backwards from the AddressBook View Controller the app crashes. Here's a screenshot of the error and the code:

Error on ABRecord

The Code

func showContactInterface(contacto: AnyObject) {
    print(self.addressBookRef)

    let peopleViewController = ABPersonViewController()

    let recordID:ABRecordID = (((contacto as! Contacto).valueForKey("recordRef")?.intValue) as ABRecordID?)!

    var recordRef:ABRecordRef? = ABAddressBookGetPersonWithRecordID(self.addressBookRef, recordID).takeRetainedValue()

    peopleViewController.displayedPerson = recordRef!
    self.navigationController?.pushViewController(peopleViewController, animated: true)
}
2

There are 2 best solutions below

0
dimpiax On

It seems to be bugged Swift taking data of ABAddressBook, so better solution to fetch some specific data on ObjectiveC and communicate through ObjectiveC_Bridge.

Also look at my article about this issue https://medium.com/@dimpiax/swift-invitephonebook-9a6ef2636124, you can find there link to InvitePhoneBook source also.

0
Elijah On

I ran into the same problem. I'm not entirely sure why, but changing takeRetainedValue() to takeUnretainedValue() solved it.

var recordRef:ABRecordRef? ABAddressBookGetPersonWithRecordID(self.addressBookRef, recordID).takeUnretainedValue()