Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.facebook.react.bridge.ReadableMapKeySetIterator;
import com.facebook.react.bridge.ReadableType;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableArray;
import com.facebook.react.uimanager.ViewManager;

public class ContactsWrapper extends ReactContextBaseJavaModule implements ActivityEventListener {
Expand Down Expand Up @@ -58,12 +59,15 @@ public class ContactsWrapper extends ReactContextBaseJavaModule implements Activ
add(ContactsContract.CommonDataKinds.Email.TYPE);
add(ContactsContract.CommonDataKinds.Email.LABEL);
}};
private Context context;


public ContactsWrapper(ReactApplicationContext reactContext) {
super(reactContext);
this.contentResolver = getReactApplicationContext().getContentResolver();
reactContext.addActivityEventListener(this);
this.context = reactContext;

}

@Override
Expand Down Expand Up @@ -125,7 +129,12 @@ public void onActivityResult(Activity ContactsWrapper, final int requestCode, fi
//First get ID
String id = null;
int idx;

WritableMap contactData = Arguments.createMap();
WritableArray phonesArray = Arguments.createArray();
WritableArray emailsArray = Arguments.createArray();


Cursor cursor = this.contentResolver.query(contactUri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
idx = cursor.getColumnIndex(ContactsContract.Contacts._ID);
Expand Down Expand Up @@ -155,9 +164,71 @@ public void onActivityResult(Activity ContactsWrapper, final int requestCode, fi
/* Map Any contact data we want returned to the JS object key for React Native */
HashMap<String, String> returnKeys = new HashMap<String, String>();
returnKeys.put(ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE, "name");
returnKeys.put(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE, "phone");
returnKeys.put(ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE, "email");

// loops through all phones

Cursor phones = this.contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null, null);
while (phones.moveToNext())
{
WritableMap phoneObj = Arguments.createMap();
String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
String phoneType = "";
switch (type) {
case ContactsContract.CommonDataKinds.Phone.TYPE_HOME:
// do something with the Home number here..
phoneType = " home";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE:
// do something with the Mobile number here...
phoneType = "mobile";
break;
case ContactsContract.CommonDataKinds.Phone.TYPE_WORK:
phoneType = "work";
// do something with the Work number here...
break;
}
phoneObj.putString("number", number);
phoneObj.putString("number_type", phoneType);
phonesArray.pushMap(phoneObj);
}
phones.close();

// loops through all emails
Cursor emails = this.contentResolver.query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + id, null, null);
while (emails.moveToNext())
{
WritableMap emailObj = Arguments.createMap();
String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));
int type = emails.getInt(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
String emailType = "";
switch (type) {
case ContactsContract.CommonDataKinds.Email.TYPE_HOME:
// do something with the Home number here..
emailType = "home";
break;
case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE:
// do something with the Mobile number here...
emailType = "mobile";
break;
case ContactsContract.CommonDataKinds.Email.TYPE_WORK:
emailType = "work";
// do something with the Work number here...
break;
}
Log.w(">>>>>>>>>>>>>>>>>>>>>>>>>>>>", emailType);
emailObj.putString("address", emailAddress);
emailObj.putString("address_type", emailType);
emailsArray.pushMap(emailObj);
}
emails.close();

contactData.putArray("emails", emailsArray);
contactData.putArray("phones", phonesArray);

// this now only grabs the name of the contact
int dataIdx = cursor.getColumnIndex(ContactsContract.Contacts.Entity.DATA1);
int mimeIdx = cursor.getColumnIndex(ContactsContract.Contacts.Entity.MIMETYPE);
if (cursor.moveToFirst()) {
Expand All @@ -172,6 +243,7 @@ public void onActivityResult(Activity ContactsWrapper, final int requestCode, fi

cursor.close();
if(foundData) {
// send contact back
mContactsPromise.resolve(contactData);
return;
} else {
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>ContactsWrapper.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
<key>RCTContactsWrapper.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>1</integer>
</dict>
</dict>
</dict>
</plist>
32 changes: 27 additions & 5 deletions ios/RCTContactsWrapper/RCTContactsWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -135,19 +135,41 @@ - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(
NSString *fullName = [self getFullNameForFirst:contact.givenName middle:contact.middleName last:contact.familyName ];
NSArray *phoneNos = contact.phoneNumbers;
NSArray *emailAddresses = contact.emailAddresses;

// NSArray *postalAddresses = contact.postalAddresses;
//Return full name
[contactData setValue:fullName forKey:@"name"];

//Return first phone number
//if we have phone numbers
if([phoneNos count] > 0) {
CNPhoneNumber *phone = ((CNLabeledValue *)phoneNos[0]).value;
[contactData setValue:phone.stringValue forKey:@"phone"];
NSMutableArray *jsPhoneNumbers = [NSMutableArray array];
for (CNPhoneNumber* p in phoneNos)
{
NSMutableDictionary* dict = @{}.mutableCopy;
NSLog(@"%@",p);
CNPhoneNumber *phone = ((CNLabeledValue *)p).value;
NSString *label = ((CNLabeledValue *)p).label;
label = [CNLabeledValue localizedStringForLabel:label];
[dict setValue:phone.stringValue forKey:@"number"];
[dict setValue:label forKey:@"number_type"];
[jsPhoneNumbers addObject:dict];
}
[contactData setValue:jsPhoneNumbers forKey:@"phones"];
}

//Return first email address
if([emailAddresses count] > 0) {
[contactData setValue:((CNLabeledValue *)emailAddresses[0]).value forKey:@"email"];
NSMutableArray *jsEmails = [NSMutableArray array];
for (CNLabeledValue* e in emailAddresses)
{
NSMutableDictionary* dict = @{}.mutableCopy;
CNLabeledValue *email = ((CNLabeledValue *)e).value;
NSString *label = ((CNLabeledValue *)e).label;
label = [CNLabeledValue localizedStringForLabel:label];
[dict setValue:email forKey:@"address"];
[dict setValue:label forKey:@"address_type"];
[jsEmails addObject:dict];
}
[contactData setValue:jsEmails forKey:@"emails"];
}

[self contactPicked:contactData];
Expand Down