How to fetch only mobile numbers in swift using CNContacts?

*爱你&永不变心* 提交于 2019-12-04 05:17:51

Check if the number's label is mobile like this:

var mobiles = [CNPhoneNumber]()

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

Then you have an array of mobile phone numbers for that person.

Harsh

A better way of doing it is mentioned in this post where you use flatMap and contains. Swift nested filter optimization?

Another Method for more visitors:

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
            }
         }

  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!