<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 7 年前。
<分区>
编辑问题以包含 desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem .这将有助于其他人回答问题。
关闭 7 年前。
我已经获取了联系电话,但我只得到一个号码,要么是家庭电话号码,要么是手机号码。如何获取它们?有什么具体的方法吗? 任何帮助将不胜感激。
最佳答案
您可以使用 CNContactPickerViewController。
import UIKit
import Contacts
import ContactsUI
protocol AddContactViewControllerDelegate {
@available(iOS 9.0, *)
func didFetchContacts(contacts: [CNContact])
}
class ViewController: UIViewController,CNContactPickerDelegate {
var delegate: AddContactViewControllerDelegate!
override func viewDidLoad() {
super.viewDidLoad()
if #available(iOS 9.0, *) {
let contactPickerViewController = CNContactPickerViewController()
contactPickerViewController.predicateForSelectionOfContact = NSPredicate(format: "phoneNumbers.@count > 0", argumentArray: nil)
contactPickerViewController.delegate = self
presentViewController(contactPickerViewController, animated: true, completion: nil)
} else {
// Fallback on earlier versions
}
}
// MARK: CNContactPickerDelegate function
@available(iOS 9.0, *)
func contactPicker(picker: CNContactPickerViewController, didSelectContact contact: CNContact) {
print(contact.phoneNumbers)
if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
for phoneNumber:CNLabeledValue in contact.phoneNumbers {
let a = phoneNumber.value as! CNPhoneNumber
print("\(a.stringValue)")
}
}
// delegate.didFetchContacts([contact])
navigationController?.popViewControllerAnimated(true)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
关于ios - 如何在iOS中以不同于联系人的方式获取手机号码和家庭号码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36952214/