gpt4 book ai didi

ios - 检查用户是否在 CNContactStore 中有电话号码并显示错误弹出窗口

转载 作者:行者123 更新时间:2023-11-28 07:57:19 25 4
gpt4 key购买 nike

我正在编写一个程序,允许用户使用 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)")
}

最佳答案

为什么不限制用户只能选择电话号码?您甚至可以隐藏所有其他字段,这样他们要么会看到电话号码,要么在联系人没有电话号码的情况下什么也看不到(这会导致他们转到其他联系人...)在这里查看答案,它还提到仅过滤电话号码: Contact Address book crash on iOS 10 beta在这种情况下,只有在选择电话号码时才会调用 didSelect 函数。

关于ios - 检查用户是否在 CNContactStore 中有电话号码并显示错误弹出窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47699837/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com