我正在开发一个需要使用 IOS 9s ContactUI 框架的 Swift 2.0 项目。我遇到的问题是从联系人列表中正确选择电话号码。当我从联系人中选择电话号码时,应用程序崩溃。
这是我用来执行此任务的代码。
var delegate: NewLocationViewControllerDelegate!
var contacts = [CNContact]()
override func viewDidLoad() {
super.viewDidLoad()
UIApplication.sharedApplication().delegate as! AppDelegate
// Do any additional setup after loading the view.
}//end
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func openContacts(sender: UIButton) {
let contactPicker = CNContactPickerViewController()
contactPicker.delegate = self;
contactPicker.displayedPropertyKeys = [CNContactPhoneNumbersKey]
self.presentViewController(contactPicker, animated: true, completion: nil)
}//end
func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
let contact = contactProperty.contact
let phoneNumber = contactProperty.value as! CNPhoneNumber
print(contact.givenName)
print(phoneNumber.stringValue)
}//end