swift2 Swift 2.0联系人UI框架

vbopmzt1  于 2022-11-06  发布在  Swift
关注(0)|答案(1)|浏览(197)

我正在Swift 2.0中进行一个项目,该项目要求我使用IOS 9s ContactUI框架。我遇到的问题是如何从联系人列表中正确选择电话号码。当我从联系人列表中选择电话号码时,应用程序崩溃。
下面是我用来执行此任务的代码。

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
6ie5vjzr

6ie5vjzr1#

问题是,电话号码上的轻敲是试图拨打电话-而在模拟器上,没有电话。
永远不会呼叫您的contactPicker:didSelectContactProperty:,因为永远不会选取任何电话号码。相反地,单击电话号码会尝试拨打该号码。这是因为您尚未提供predicateForSelectionOfProperty。您必须将predicateForSelectionOfProperty设定为NSPredicate,当key是电话号码时,该NSPredicate会评估为true

相关问题