【问题标题】:How to fetch only mobile numbers in swift using CNContacts?如何使用 CNContacts 快速获取手机号码?
【发布时间】:2016-05-04 22:26:29
【问题描述】:

我有一些代码可以检索用户联系人中的所有电话号码,但我想只过滤掉手机号码。目前,我只是将第一个数字为“+”或第二个数字为“7”的数字添加到数组中,如下所示:

    func findContacts () -> [CNContact]{
    let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName),CNContactPhoneNumbersKey]
    let fetchRequest: CNContactFetchRequest = CNContactFetchRequest(keysToFetch: keysToFetch)
    var contacts = [CNContact]()
    CNContact.localizedStringForKey(CNLabelPhoneNumberiPhone)
    fetchRequest.mutableObjects = false
    fetchRequest.unifyResults = true
    fetchRequest.sortOrder = .UserDefault

    let contactStoreID = CNContactStore().defaultContainerIdentifier()

    do {

        try CNContactStore( ).enumerateContactsWithFetchRequest(fetchRequest) { (let contact, let stop) -> Void in
            if contact.phoneNumbers.count > 0 {
                contacts.append(contact)
            }
            if (contact.isKeyAvailable(CNContactPhoneNumbersKey)) {
                for phoneNumber:CNLabeledValue in contact.phoneNumbers {
                    let number = phoneNumber.value as! CNPhoneNumber
                    print(number.stringValue)
                    let index = number.stringValue.startIndex.advancedBy(1)
                    let indexPlus = number.stringValue.startIndex.advancedBy(0)
                    if number.stringValue[index] == Character(String(7)) || number.stringValue[indexPlus] == Character("+"){
                        self.allNumbers.append("\(number.stringValue)")
                    }
                }
            }
        }

由于联系人存储在带有“移动”标签的 iPhone 上,我想知道是否只能将这些数字添加到数组中。谢谢:)

【问题讨论】:

    标签: ios arrays swift cncontact


    【解决方案1】:

    检查号码的标签是否像这样移动:

    var mobiles = [CNPhoneNumber]()
    
    for num in contact.phoneNumbers {
        let numVal = num.value as! CNPhoneNumber
        if num.label == CNLabelPhoneNumberMobile {
            mobiles.append(numVal)
        }
    }
    

    然后,您就有了该人的一系列手机号码。

    【讨论】:

    • 不要硬编码"mobile"。使用CNLabelPhoneNumberMobileCNLabelPhoneNumberiPhone。但请记住,这不会获取所有手机号码。人们可以在添加电话号码时使用自定义标签。有人可能会错误地使用“家”标签或任何其他可能性。或者用户可能会在固定电话上使用“移动”标签。最后,没有办法确定任何给定的号码真的是手机号码。
    【解决方案2】:

    在这篇文章中提到了一种更好的方法,您可以使用flatMapcontainsSwift nested filter optimization?

    【讨论】:

      【解决方案3】:

      另一种吸引更多访问者的方法:

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

      【讨论】:

        猜你喜欢
        • 2020-02-13
        • 2019-12-07
        • 2020-02-09
        • 2021-09-26
        • 2023-03-09
        • 1970-01-01
        • 2021-06-29
        • 1970-01-01
        • 2020-03-01
        相关资源
        最近更新 更多