2

I am trying to get to phone numbers of my contacts using CNContact, i want to have the number as a simple string of its didgits such as "04xxxxxxxx" but the closest I can get to is the following. ("contact" is of type CNContact)

contact.phoneNumbers[0].value
\\Which prints: <CNPhoneNumber: 0x13560a550: countryCode=au, digits=04xxxxxxxx>

ive tried all the obvious things and not so obvious things, thanks

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Blake Lockley
  • 2,931
  • 1
  • 17
  • 30

3 Answers3

2

If anyone has a more legitimate solution please do post it, otherwise this rather hacky approach works:

let value = String(contact.phoneNumbers[0].value)

let start = value.rangeOfString("digits=")!.endIndex
let end = value.endIndex.predecessor()

let number = value.substringWithRange(Range<String.Index>(start: start, end: end))
Blake Lockley
  • 2,931
  • 1
  • 17
  • 30
  • Yup, works for me as well. Surprised there isn't a less "hacky" approach. Apple should have a subclass of "digits"! – Lukesivi Dec 13 '15 at 15:09
0

Using this category you can now access the phone number via swift. don't forget to include this file in the bridging header

@implementation  CNPhoneNumber (SwiftSupport)
- (NSString*)toString {
    return self.stringValue;
}

@end
Edward Ashak
  • 2,411
  • 2
  • 23
  • 38
0

Fetch the number value as follows:

let number = value.valueForKey("digits") as! String

From iOS9:

let number = value.stringValue

According to Apple's documentation on CNPhoneNumber:

stringValue Property The string value of the phone number. (read-only)

Declaration SWIFT var stringValue: String { get }

Community
  • 1
  • 1
Ri_
  • 662
  • 8
  • 13