import ContactsUI
class ViewController: UIViewController, CNContactPickerDelegate {
@IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func selectContacts(_ sender: AnyObject) {
let contactPicker = CNContactPickerViewController()
//设置代理
contactPicker.delegate = self
self.present(contactPicker, animated: true, completion: nil)
}
//单选联系人
func contactPicker(_ picker: CNContactPickerViewController,
didSelect contact: CNContact) {
let lastName = contact.familyName
let firstName = contact.givenName
print("姓:\(lastName)")
print("名:\(firstName)")
let phones = contact.phoneNumbers
for phone in phones {
let phoneLabel = CNLabeledValue<NSString>.localizedString(forLabel: phone.label!)
let phoneValue = phone.value.stringValue
print("\(phoneLabel):\(phoneValue)")
}
testLabel.text = lastName + firstName
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
这篇博客介绍了如何在Swift中实现访问iOS设备的通讯录功能。通过CNContactPickerViewController展示联系人选择界面,并且实现了当选定联系人后,获取并显示联系人的姓氏、名字以及电话号码。
2236

被折叠的 条评论
为什么被折叠?



