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

后端 未结 10 1781
执念已碎
执念已碎 2020-12-13 13:31

I am attempting to retrieve the names and phone number(s) of all contacts and put them into arrays with Swift in iOS. I have made it this far:



        
相关标签:
10条回答
  • 2020-12-13 13:56

    In swift 3 you can get direclty

     if item.isKeyAvailable(CNContactPhoneNumbersKey){
            let phoneNOs=item.phoneNumbers
            let phNo:String
            for item in phoneNOs{
                print("Phone Nos \(item.value.stringValue)")
            }
    
    0 讨论(0)
  • 2020-12-13 13:58

    you can get contact.phoneNumbers from CNLabeledValue:

    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)")
      }
    }
    
    0 讨论(0)
  • 2020-12-13 13:59

    The definition of a 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 is an array of CNLabeledValues and each CNLabeledValue has a label and a value.

    To print the phoneNumbers corresponding to a CNContact you can try:

    for phoneNumber in contact.phoneNumbers {
        print("The \(phoneNumber.label) number of \(contact.givenName) is: \(phoneNumber.value)")
    }
    
    0 讨论(0)
  • 2020-12-13 14:01

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

    This is to remove +91 from your phone number and it's working fine.

    0 讨论(0)
  • 2020-12-13 14:03

    Here is how you do it in swift 4

    func contactPicker(_ picker: CNContactPickerViewController, didSelect contactProperty: CNContactProperty) {
    
        if let phoneNo = contactProperty.value as? CNPhoneNumber{
            txtPhone.text = phoneNo.stringValue
        }else{
            txtPhone.text=""
        }
    }
    
    0 讨论(0)
  • 2020-12-13 14:06
    /* 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!)
        }
    
    0 讨论(0)
提交回复
热议问题