我正在编写一个程序,允许用户使用CNContactPickerViewController
选择联系人。如果所选的联系人 NOT
具有关联的电话号码,则我想将其弹出错误,并在他们命中时返回ContactPickerViewController
。我已经通过断点完成了代码,并正确执行,但是它没有显示弹出的错误。
我一生都不能弄清楚为什么...这是我的代码:
func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
if contact.phoneNumbers.first?.value.stringValue != nil{
//@TODO: check for repeats in people array
// do something with contact
let newPerson = Person(firstName: contact.givenName,
lastName: contact.familyName,
profileImage: #imageLiteral(resourceName: "capitalizing_on_the_economic_potential_of_foreign_entrepreneurs_feature.png") )
if contact.imageDataAvailable == true{
newPerson.profileImage = UIImage(data: contact.imageData!)!
}
// this is for the full name
let fullname = "(contact.givenName) (contact.familyName)"
print("The selected name is: (fullname)")
let phoneNum = contact.phoneNumbers.first?.value.stringValue
print("The selected phone num is: (phoneNum!)")
//appends data to new activity model for prep to send back to home vc
newActivity.people.append(newPerson)
print("the people in the new activity array are: (newActivity.people)")
peopleCollection.reloadData()
} else {
print("error has no number")
let alertController = UIAlertController(title: "Error: Person has no number!", message: "", preferredStyle: .alert)
let confirmAction = UIAlertAction(title: "Ok", style: .default, handler: {
alert -> Void in
})
//add actions to alert sheet
alertController.addAction(confirmAction)
self.present(alertController, animated: true, completion: nil)
//the code executes here correctly, but it does not present the alertController
}
//this is for phone number without dashes
//print("the selected phone number is: ((contact.phoneNumbers[0].value ).value(forKey: "digits") as! String)")
}
为什么不限制用户只选择电话号码?您甚至可以隐藏所有其他字段,因此他们要么会看到电话号码,要么在没有一个接触的情况下没有任何字段(这会导致他们转到其他联系人...)在此处检查答案,它还提到过滤到电话号码:在iOS 10 Beta上的联系地址簿崩溃在这种情况下,只有在选择电话号码时才调用didSelect函数。