简体   繁体   中英

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

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

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

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 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM