Blake Lockley
Blake Lockley

Reputation: 2961

Access Users Contacts Phone Numbers CNContacts

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

Upvotes: 2

Views: 422

Answers (3)

Ri_
Ri_

Reputation: 662

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 }

Upvotes: 0

Edward Ashak
Edward Ashak

Reputation: 2441

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

Upvotes: 0

Blake Lockley
Blake Lockley

Reputation: 2961

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

Upvotes: 2

Related Questions