iOS 9之后,我们将使用Contacts框架,在其中phoneNumbers
是一个CNLabeledValue<CNPhoneNumber>
:
let status = CNContactStore.authorizationStatus(for: .contacts)
if status == .denied || status == .restricted {
presentSettingsAlert()
return
}
let store = CNContactStore()
store.requestAccess(for: .contacts) { granted, error in
guard granted else {
self.presentSettingsAlert()
return
}
let request = CNContactFetchRequest(keysToFetch: [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactPhoneNumbersKey as CNKeyDescriptor])
do {
try store.enumerateContacts(with: request) { contact, stop in
let name = CNContactFormatter.string(from: contact, style: .fullName)
print(name)
for phone in contact.phoneNumbers {
var label = phone.label
if label != nil {
label = CNLabeledValue<CNPhoneNumber>.localizedString(forLabel: label!)
}
print(" ", label, phone.value.stringValue)
}
}
} catch {
print(error)
}
}
在哪里
private func presentSettingsAlert() {
let settingsURL = URL(string: UIApplicationOpenSettingsURLString)!
DispatchQueue.main.async {
let alert = UIAlertController(title: "Permission to Contacts", message: "This app needs access to contacts in order to ...", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Go to Settings", style: .default) { _ in
UIApplication.shared.openURL(settingsURL)
})
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
self.present(alert, animated: true)
}
}
iOS 9之前,您会使用AddressBook框架,在该框架中电话号码是一个ABMultiValueRef
类型,因此获取该引用并迭代通过电话号码:
let status = ABAddressBookGetAuthorizationStatus()
if status == .denied || status == .restricted {
presentSettingsAlert()
return
}
var error: Unmanaged<CFError>?
guard let addressBook: ABAddressBook? = ABAddressBookCreateWithOptions(nil, &error)?.takeRetainedValue() else {
print(String(describing: error?.takeRetainedValue()))
return
}
ABAddressBookRequestAccessWithCompletion(addressBook) { granted, error in
if !granted {
self.presentSettingsAlert()
return
}
guard let people = ABAddressBookCopyArrayOfAllPeople(addressBook)?.takeRetainedValue() as [ABRecord]? else {
print("unable to get contacts")
return
}
for person in people {
let name = ABRecordCopyCompositeName(person)?.takeRetainedValue() as String?
print(name)
if let phoneNumbers: ABMultiValue = ABRecordCopyValue(person, kABPersonPhoneProperty)?.takeRetainedValue() {
for index in 0 ..< ABMultiValueGetCount(phoneNumbers) {
let number = ABMultiValueCopyValueAtIndex(phoneNumbers, index)?.takeRetainedValue() as? String
let label = ABMultiValueCopyLabelAtIndex(phoneNumbers, index)?.takeRetainedValue()
print(" ", self.localizedLabel(label), number)
}
}
}
}
MacOS有一个现有的例程来本地化该标签,但我不知道iOS的AddressBook框架中是否存在这样的公共函数,因此您可能需要自行转换它(或为NSLocalizedString
填充本地化表):
private func localizedLabel(_ label: CFString?) -> String? {
guard let label = label else {
return nil
}
if CFStringCompare(label, kABHomeLabel, []) == .compareEqualTo {
return "Home"
} else if CFStringCompare(label, kABWorkLabel, []) == .compareEqualTo {
return "Work"
} else if CFStringCompare(label, kABOtherLabel, []) == .compareEqualTo {
return "Other"
} else if CFStringCompare(label, kABPersonPhoneMobileLabel, []) == .compareEqualTo {
return "Mobile"
} else if CFStringCompare(label, kABPersonPhoneIPhoneLabel, []) == .compareEqualTo {
return "iPhone"
} else if CFStringCompare(label, kABPersonPhoneMainLabel, []) == .compareEqualTo {
return "Main"
} else if CFStringCompare(label, kABPersonPhoneHomeFAXLabel, []) == .compareEqualTo {
return "Home fax"
} else if CFStringCompare(label, kABPersonPhoneWorkFAXLabel, []) == .compareEqualTo {
return "Work fax"
} else if CFStringCompare(label, kABPersonPhoneOtherFAXLabel, []) == .compareEqualTo {
return "Other fax"
} else if CFStringCompare(label, kABPersonPhonePagerLabel, []) == .compareEqualTo {
return "Pager"
} else {
return label as String
}
}
如需 Swift 2 版本,请参见 此答案的先前版本。