Skip to content

Commit 8f5334c

Browse files
committed
fix: enable correct phoneNumber/emailAddress parsing
1 parent 986203c commit 8f5334c

File tree

3 files changed

+62
-18
lines changed

3 files changed

+62
-18
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,8 @@ This method will return an empty array (`[]`) if access to Contacts has not been
104104
* `lastName` String (optional) - The last name of the contact.
105105
* `nickname` String (optional) - The nickname for the contact.
106106
* `birthday` String (optional) - The birthday for the contact in `YYYY-MM-DD` format.
107-
* `phoneNumbers` Array<String> (optional) - The phone numbers for the contact, as strings in [E.164 format](https://en.wikipedia.org/wiki/E.164): `+14155552671` or `+442071838750`.
108-
* `emailAddresses` Array<String> (optional) - The email addresses for the contact, as strings.
109-
* `postalAddresses` Array<String> (optional) - The postal addresses for the contact, as strings.
107+
* `phoneNumbers` Array\<String\> (optional) - The phone numbers for the contact, as strings in [E.164 format](https://en.wikipedia.org/wiki/E.164): `+14155552671` or `+442071838750`.
108+
* `emailAddresses` Array\<String\> (optional) - The email addresses for the contact, as strings.
110109

111110
Returns `Boolean` - whether the contact information was created successfully.
112111

contacts.mm

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,49 @@
7777
return contact;
7878
}
7979

80+
NSArray* ParsePhoneNumbers(Napi::Array phone_number_data) {
81+
NSMutableArray *phone_numbers = [[NSMutableArray alloc] init];
82+
83+
int data_length = static_cast<int>(phone_number_data.Length());
84+
for (int i = 0; i < data_length; i++) {
85+
std::string number_str = phone_number_data.Get(i).As<Napi::String>().Utf8Value();
86+
NSString *number = [NSString stringWithUTF8String:number_str.c_str()];
87+
CNPhoneNumber *phone_number = [CNPhoneNumber phoneNumberWithStringValue:number];
88+
CNLabeledValue *labeled_value = [CNLabeledValue labeledValueWithLabel:@"Home" value:phone_number];
89+
[phone_numbers addObject:labeled_value];
90+
}
91+
92+
return phone_numbers;
93+
}
94+
95+
NSArray* ParseEmailAddresses(Napi::Array email_address_data) {
96+
NSMutableArray *email_addresses = [[NSMutableArray alloc] init];
97+
98+
int data_length = static_cast<int>(email_address_data.Length());
99+
for (int i = 0; i < data_length; i++) {
100+
std::string email_str = email_address_data.Get(i).As<Napi::String>().Utf8Value();
101+
NSString *email = [NSString stringWithUTF8String:email_str.c_str()];
102+
CNLabeledValue *labeled_value = [CNLabeledValue labeledValueWithLabel:@"Home" value:email];
103+
[email_addresses addObject:labeled_value];
104+
}
105+
106+
return email_addresses;
107+
}
108+
109+
NSDateComponents* ParseBirthday(std::string birth_day) {
110+
NSString *bday = [NSString stringWithUTF8String:birth_day.c_str()];
111+
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
112+
[formatter setDateFormat:@"yyyy-MM-dd"];
113+
114+
NSDate *bday_date = [formatter dateFromString:bday];
115+
116+
NSCalendar *cal = [NSCalendar currentCalendar];
117+
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
118+
NSDateComponents *birthday_components = [cal components:unitFlags fromDate:bday_date];
119+
120+
return birthday_components;
121+
}
122+
80123
CNAuthorizationStatus AuthStatus() {
81124
CNEntityType entityType = CNEntityTypeContacts;
82125
return [CNContactStore authorizationStatusForEntityType:entityType];
@@ -172,36 +215,40 @@ CNAuthorizationStatus AuthStatus() {
172215
if (AuthStatus() != CNAuthorizationStatusAuthorized)
173216
return Napi::Boolean::New(env, false);
174217

175-
// Parse Contact object data
176218
CNMutableContact *contact = [[CNMutableContact alloc] init];
177219
Napi::Object contact_data = info[0].As<Napi::Object>();
178-
if(contact_data.Has("firstName")) {
220+
221+
if (contact_data.Has("firstName")) {
179222
std::string first_name = contact_data.Get("firstName").As<Napi::String>().Utf8Value();
180223
[contact setGivenName:[NSString stringWithUTF8String:first_name.c_str()]];
181224
}
182225

183-
if(contact_data.Has("lastName")) {
226+
if (contact_data.Has("lastName")) {
184227
std::string last_name = contact_data.Get("lastName").As<Napi::String>().Utf8Value();
185228
[contact setFamilyName:[NSString stringWithUTF8String:last_name.c_str()]];
186229
}
187230

188-
if(contact_data.Has("nickname")) {
231+
if (contact_data.Has("nickname")) {
189232
std::string nick_name = contact_data.Get("nickname").As<Napi::String>().Utf8Value();
190233
[contact setFamilyName:[NSString stringWithUTF8String:nick_name.c_str()]];
191234
}
192235

193-
if(contact_data.Has("birthday")) {
236+
if (contact_data.Has("birthday")) {
194237
std::string birth_day = contact_data.Get("birthday").As<Napi::String>().Utf8Value();
195-
NSString *bday = [NSString stringWithUTF8String:birth_day.c_str()];
196-
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
197-
[formatter setDateFormat:@"yyyy-MM-dd"];
238+
NSDateComponents *birthday_components = ParseBirthday(birth_day);
239+
[contact setBirthday:birthday_components];
240+
}
198241

199-
NSDate *bday_date = [formatter dateFromString:bday];
242+
if (contact_data.Has("phoneNumbers")) {
243+
Napi::Array phone_number_data = contact_data.Get("phoneNumbers").As<Napi::Array>();
244+
NSArray *phone_numbers = ParsePhoneNumbers(phone_number_data);
245+
[contact setPhoneNumbers:[NSArray arrayWithArray:phone_numbers]];
246+
}
200247

201-
NSCalendar *cal = [NSCalendar currentCalendar];
202-
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
203-
NSDateComponents *birthday_components = [cal components:unitFlags fromDate:bday_date];
204-
[contact setBirthday:birthday_components];
248+
if (contact_data.Has("emailAddresses")) {
249+
Napi::Array email_address_data = contact_data.Get("emailAddresses").As<Napi::Array>();
250+
NSArray *email_addresses = ParseEmailAddresses(email_address_data);
251+
[contact setEmailAddresses:[NSArray arrayWithArray:email_addresses]];
205252
}
206253

207254
CNSaveRequest *request = [[CNSaveRequest alloc] init];

index.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@ function addNewContact(contact) {
1515
const hasNickname = contact.hasOwnProperty('nickname')
1616
const hasBirthday = contact.hasOwnProperty('birthday')
1717
const hasPhoneNumbers = contact.hasOwnProperty('phoneNumbers')
18-
const hasPostalAddresses = contact.hasOwnProperty('postalAddresses')
1918
const hasEmailAddresses = contact.hasOwnProperty('emailAddresses')
2019

2120
if (hasFirstName && typeof contact.firstName !== 'string') throw new TypeError('firstName must be a string')
2221
if (hasLastName && typeof contact.lastName !== 'string') throw new TypeError('lastName must be a string')
2322
if (hasNickname && typeof contact.nickname !== 'string') throw new TypeError('nickname must be a string')
2423
if (hasPhoneNumbers && !Array.isArray(contact.phoneNumbers)) throw new TypeError('phoneNumbers must be an array')
25-
if (hasPostalAddresses && !Array.isArray(contact.postalAddresses)) throw new TypeError('postalAddresses must be an array')
2624
if (hasEmailAddresses && !Array.isArray(contact.emailAddresses)) throw new TypeError('emailAddresses must be an array')
2725

2826
if (hasBirthday) {

0 commit comments

Comments
 (0)