如何在 Swift 中获取 CNContact phone 号码作为字符串?

How to get a CNContact phone number(s) as string in Swift?

我正在尝试检索所有联系人的姓名和 phone 号码,并将它们放入 iOS 中 Swift 的数组中。我已经做到了:

func findContacts() -> [CNContact] {

    marrContactsNumber.removeAllObjects()
    marrContactsName.removeAllObjects()

    let store = CNContactStore()

    let keysToFetch = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]

    let fetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)

    var contacts = [CNContact]()

    do {
        try store.enumerateContactsWithFetchRequest(fetchRequest, usingBlock: { (let contact, let stop) -> Void in
            contacts.append(contact)

            self.marrContactsName.addObject(contact.givenName + " " + contact.familyName)

            self.marrContactsNumber.addObject(contact.phoneNumbers)

            print(contact.phoneNumbers)
    }
    catch let error as NSError {
        print(error.localizedDescription)
    }

    print(marrContactsName.count)
    print(marrContactsNumber.count)

    return contacts
}

完成后,marrContactsName 将完全按照预期包含我所有联系人姓名的数组。即 "John Doe"。但是,marrContactsNumber returns 值数组,如

[<CNLabeledValue: 0x158a19950: identifier=F831DC7E-5896-420F-AE46-489F6C14DA6E,
label=_$!<Work>!$_, value=<CNPhoneNumber: 0x158a19640: countryCode=us, digits=6751420000>>,
<CNLabeledValue: 0x158a19a80: identifier=ECD66568-C6DD-441D-9448-BDEDDE9A68E1,
label=_$!<Work>!$_, value=<CNPhoneNumber: 0x158a199b0: countryCode=us, digits=5342766455>>]

我想知道如何仅检索 phone 个数字作为字符串值,即 "XXXXXXXXXX"。基本上,如何调用数字值。谢谢!

一个CNLabeledValue的定义:

The CNLabeledValue class is a thread-safe class that defines an immutable value object that combines a contact property value with a label. For example, a contact phone number could have a label of Home, Work, iPhone, etc.

CNContact.phoneNumbers 是一个 CNLabeledValues 数组,每个 CNLabeledValue 都有一个标签和一个值。

要打印与 CNContact 对应的电话号码,您可以尝试:

for phoneNumber in contact.phoneNumbers {
    print("The \(phoneNumber.label) number of \(contact.givenName) is: \(phoneNumber.value)")
}

我找到了解决方案:(contact.phoneNumbers[0].value as! CNPhoneNumber).valueForKey("digits") as! String

你可以从 CNLabeledValue:

得到 contact.phoneNumbers
for phoneNumber in contact.phoneNumbers {
  if let number = phoneNumber.value as? CNPhoneNumber,
      let label = phoneNumber.label {
      let localizedLabel = CNLabeledValue.localizedStringForLabel(label)
      print("\(localizedLabel)  \(number.stringValue)")
  }
}
/* Get only first mobile number */

    let MobNumVar = (contact.phoneNumbers[0].value as! CNPhoneNumber).valueForKey("digits") as! String
    print(MobNumVar)

/* Get all mobile number */

    for ContctNumVar: CNLabeledValue in contact.phoneNumbers
    {
        let MobNumVar  = (ContctNumVar.value as! CNPhoneNumber).valueForKey("digits") as? String
        print(MobNumVar!)
    }

 /* Get mobile number with mobile country code */

    for ContctNumVar: CNLabeledValue in contact.phoneNumbers
    {
        let FulMobNumVar  = ContctNumVar.value as! CNPhoneNumber
        let MccNamVar = FulMobNumVar.valueForKey("countryCode") as? String
        let MobNumVar = FulMobNumVar.valueForKey("digits") as? String

        print(MccNamVar!)
        print(MobNumVar!)
    }

在swift3中可以直接得到

 if item.isKeyAvailable(CNContactPhoneNumbersKey){
        let phoneNOs=item.phoneNumbers
        let phNo:String
        for item in phoneNOs{
            print("Phone Nos \(item.value.stringValue)")
        }

Swift 3 "_$!<Mobile>!$_" 写这个项目是为了创造差异,同时也提供了一个依赖各种选择的机会。

for con in contacts
{
    for num in con.phoneNumbers
    {
        if num.label == "_$!<Mobile>!$_"    //Please Don't Change this!
        {
            self.contactNames.append(con.givenName)
            self.contactNums.append(num.value.stringValue)
            break
        }
        else
        {
            continue
        }
    }
}

这里有num.value.stringValue

这是 swift 4

中的操作方法
func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {

    if let phoneNo = contactProperty.value as? CNPhoneNumber{
        txtPhone.text = phoneNo.stringValue
    }else{
        txtPhone.text=""
    }
}

这是 Swift 5 的解决方案。

import Contacts

func sendMessageTo(_ contact: CNContact) {

    let validTypes = [
        CNLabelPhoneNumberiPhone,
        CNLabelPhoneNumberMobile,
        CNLabelPhoneNumberMain
    ]

    let numbers = contact.phoneNumbers.compactMap { phoneNumber -> String? in
        guard let label = phoneNumber.label, validTypes.contains(label) else { return nil }
        return phoneNumber.value.stringValue
    }

    guard !numbers.isEmpty else { return }

    // process/use your numbers for this contact here
    DispatchQueue.main.async {
        self.sendSMSText(numbers)
    }
}

您可以在 CNPhoneNumber 头文件中找到 validTypes 数组的可用值。

他们是:

CNLabelPhoneNumberiPhone
CNLabelPhoneNumberMobile
CNLabelPhoneNumberMain
CNLabelPhoneNumberHomeFax
CNLabelPhoneNumberWorkFax
CNLabelPhoneNumberOtherFax
CNLabelPhoneNumberPager

保持简单:

let phoneNumbers: [String] = contact.phoneNumbers.compactMap { (phoneNumber: CNLabeledValue) in
    guard let number = phoneNumber.value.value(forKey: "digits") as? String else { return nil }
    return number
}

对于Swift 5+

func removeSpecialCharactersFromContactNumberOfUser(_ contactNo : String) -> String? {

    let digits = CharacterSet(charactersIn: "0123456789").inverted
    let modifiedContactNo = contactNo.components(separatedBy: digits).joined(separator: "")

    if modifiedContactNo.count > 9 {

        return modifiedContactNo

    } else {

        return nil
    }
}

var number = phone.value.stringValue
number = number.starts(with: "+91") ? number.replacingOccurrences(of: "+91", with: "") : number

if let formattedNumber = removeSpecialCharactersFromContactNumberOfUser(number)  {
    //use this formattedNumber                 
}

这是为了从您的 phone 号码中删除 +91,它工作正常。

从 phone 联系人中获取不带国家代码的内容,还删除了不需要的文本,例如破折号、空格等。还从 phone 文本字段中删除了 post 导入联系人UI var phoneString:String!

func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
        let numbers = contact.phoneNumbers.first
        let a = (numbers?.value)?.stringValue ?? ""
        let myString = a
        let formattedString = myString.replacingOccurrences(of: " ", with: "")
        let newFormattedString = formattedString.replacingOccurrences(of: "(", with: "")
        let formatstring = newFormattedString.replacingOccurrences(of: ")", with: "")
        let  last10  = formatstring.replacingOccurrences(of: "-", with: "")
        phoneString = String(last10.suffix(10))
        phonetextField.text = phoneString
        
    }
    
    func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
        self.dismiss(animated: true, completion: nil)
    }
   @IBAction func inviteButton(_ sender : Any)
    {
        if phoneString == nil{
            phoneString =  phonetextField.text! //fetching from phonetextfield
            Phone = phoneString
        }
        else  {
            Phone = phoneString //fetching from phone contacts
        
        }
      }