I have added the ABPeoplePickerNavigationController
into my first view controller. I want that when I select a contact show the info to show in other view controller, but I'm trying use my code and this not show never when I click in a contact. This only open the contact into native app ABPeoplePickerNavigationController
.
var people = ABPeoplePickerNavigationController()
var addressBook: ABAddressBookRef?
func extractABAddressBookRef(abRef: Unmanaged<ABAddressBookRef>!) -> ABAddressBookRef? {
if let ab = abRef {
self.view.addSubview(people.view)
return Unmanaged<NSObject>.fromOpaque(ab.toOpaque()).takeUnretainedValue()
}
return nil
}
I tried this function
func peoplePickerNavigationController(peoplePicker: ABPeoplePickerNavigationController!,didSelectPerson person: ABRecordRef!) {
var unmanagedEmails = ABRecordCopyValue(people, kABPersonEmailProperty)
let emailObj: ABMultiValueRef = Unmanaged.fromOpaque(unmanagedEmails.toOpaque()).takeUnretainedValue() as NSObject as ABMultiValueRef
var index = 0 as CFIndex
var unmanagedEmail = ABMultiValueCopyValueAtIndex(emailObj, index)
var emailAddress:String = Unmanaged.fromOpaque(unmanagedEmail.toOpaque()).takeUnretainedValue() as NSObject as String
println(emailAddress)
}
Thanks!
SWIFT3 IOS10 Working version of Jon Vogel for Swift 3 and IOS 10 and support to multiple contacts selection.
A couple of thoughts:
Have you set the
peoplePickerDelegate
property of thepeople
picker controller? If you don't do that, it won't know to try to call these methods in your class. Thus:Your example method is referencing
people
when you callABRecordCopyValue
. That's your picker controller. I assume you meant to referenceperson
, theABRecordRef!
that was passed as a parameter.You might also want to make sure you actually have an email address before trying to access it. You can use
ABMultiValueGetCount
.I also think you can also eliminate that
fromOpaque
/toOpaque
dance.This yields:
If you need to support iOS 7, too, use:
You might, though, rather than assuming the user only wanted the first email address, instead, let them click through and pick one of the possible multiple email addresses the contact had. So, first, you might want to eliminate some of the "noise", by telling the picker that you only want to see email addresses:
And then, remove the prior method we've been discussing, and instead implement:
And to support iOS 7,0, too, you'd add:
By the way, iOS 8 offers a feature to control whether a contact is enabled or not. Since you're supporting iOS 7 and 8, you'd want to employ that conditionally, such as:
This gives the user visual indication whether there is even an email address for the individual, and prevents them from selecting entry without email address.
Obviously, if using iOS 9 and later, you should retire all of this and use the
ContactsUI
framework, which simplifies the code further.Here is the latest framework for iOS 9 - ContactsUI
import ContactsUI
Conform to the CNContactPickerDelegate (No required methods)
Create a contacts picker object and present it:
Dismiss the CNContactPickerViewController in the contactPickerDidCancel delegate function
Here is how I Accessed a contacts name, phone numbers, phone number labels, and photo using the didSelectContact delegate function: