streem / react-native-select-contact

A cross-platform contact selection library for react-native
MIT License
114 stars 50 forks source link

Retrieving a Contact's Phone Number Country Code and Digits (or in E.164 format) #52

Closed Simon-ent closed 2 years ago

Simon-ent commented 2 years ago

Firstly thank you for the great package.

We would really like to continue using it however have been unable to retrieve a number that can be used to receive sms's, for that we would need the country code to construct a number in the E.164 format. The type and number returned currently isn't enough information to do this with.

Therefore would it be possible for someone to extend the package to also return the phone numbers "countryCode" and "digits" values? From this information it would then be simple to construct a number in the required format.

I'd offer to submit a PR myself however have no clue what I'd need to change.

Simon-ent commented 2 years ago

For anyone else facing a similar issue I moved to using the libphonenumber-js package to parse the returned phone number from this package and made an assumption about the users default locale and therefore country code.

I did try updating the package and I was able to make the changes for iOS to return country code and digits but wasn't able to find anything similar for Android. For those interested in a minor change to get the iOS data I changed the following in ios/RCTSelectContact/RCTSelectContact.m //Return phone numbers NSMutableArray* phoneEntries = [contactData valueForKey:@"phones"]; for (CNLabeledValue<CNPhoneNumber*> *phone in contact.phoneNumbers) { CNPhoneNumber* phoneNumber = [phone value]; NSString* phoneLabel = [phone label]; NSMutableDictionary<NSString*, NSString*>* phoneEntry = [[NSMutableDictionary alloc] initWithCapacity:4]; [phoneEntry setValue:[phoneNumber stringValue] forKey:@"number"]; [phoneEntry setValue:[CNLabeledValue localizedStringForLabel:phoneLabel] forKey:@"type"]; [phoneEntry setValue:[phoneNumber valueForKey:@"digits"] forKey:@"digits"]; [phoneEntry setValue:[phoneNumber valueForKey:@"countryCode"] forKey:@"countryCode"]; [phoneEntries addObject:phoneEntry]; }