I have implemented both the framework for Contact
and AddressBook
in order to fetch contacts from the contact book for users both above and below iOS
9. In order to press a row to access another view
to see the contact information I have used CNLabeledValue
in didSelectRowAt
like this:
if #available(iOS 9.0, *) {
switch((number as! CNLabeledValue<CNPhoneNumber> ).label){
case CNLabelPhoneNumberMobile:phoneContact?.mobilePhone = (number as! CNLabeledValue<CNPhoneNumber>).value.stringValue
case CNLabelPhoneNumberiPhone:phoneContact?.mobilePhone = (number as! CNLabeledValue<CNPhoneNumber>).value.stringValue
case CNLabelPhoneNumberMain:phoneContact?.mobilePhone = (number as! CNLabeledValue<CNPhoneNumber>).value.stringValue
case CNLabelHome:phoneContact?.homePhone = (number as! CNLabeledValue<CNPhoneNumber>).value.stringValue
case CNLabelWork:phoneContact?.workPhone = (number as! CNLabeledValue<CNPhoneNumber>).value.stringValue
default:phoneContact?.mobilePhone = (number as! CNLabeledValue<CNPhoneNumber>).value.stringValue
}
} else {
// Fallback on earlier versions
}
The ContactBook
code works, but what was used before CNLabeledValue
before iOS 9
? Is there a framework to use?
The Contacts framework (CN*
) was added in iOS 9. Before that, we used the AddressBook framework. There is no exact equivalent to CNLabledValue there. The closest is ABMultiValue, which is a CoreFoundation type.
Note that the current docs often suggest that some of these types only exist in macOS 12+, but that's not correct. Many of these did exist on older versions of iOS.