diff --git a/README.md b/README.md index 641bd0c..907645d 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ dependencies { in `MainApplication.java` add package to getPacakges() -``` +```java import com.lynxit.contactswrapper.ContactsWrapperPackage; ... @@ -57,15 +57,15 @@ protected List getPackages() { in `AndroidManifest.xml` make sure you have the following setting even if you have done `react-native upgrade` -``` +```xml ``` @@ -84,22 +84,22 @@ Also add 10. Clean and Rebuild your Xcode project -##API +## API `getContact` (Promise) - returns basic contact data as a JS object. Currently returns name, first phone number and first email for contact. `getEmail` (Promise) - returns first email address (if found) for contact as string. -##Usage +## Usage Methods should be called from React Native as any other promise. Prevent methods from being called multiple times (on Android). -###Example +### Example An example project can be found in this repo: https://github.com/LynxITDigital/react-native-contacts-wrapper-example/tree/master -``` +```js import ContactsWrapper from 'react-native-contacts-wrapper'; ... if (!this.importingContactInfo) { diff --git a/android/app/src/main/java/com/lynxit/contactswrapper/ContactsWrapper.java b/android/app/src/main/java/com/lynxit/contactswrapper/ContactsWrapper.java index 487ab81..af53f0c 100644 --- a/android/app/src/main/java/com/lynxit/contactswrapper/ContactsWrapper.java +++ b/android/app/src/main/java/com/lynxit/contactswrapper/ContactsWrapper.java @@ -31,9 +31,8 @@ import com.facebook.react.uimanager.ViewManager; public class ContactsWrapper extends ReactContextBaseJavaModule implements ActivityEventListener { - - private static final int CONTACT_REQUEST = 1; - private static final int EMAIL_REQUEST = 2; + private static final int CONTACT_REQUEST = 34222; + private static final int EMAIL_REQUEST = 68489; public static final String E_CONTACT_CANCELLED = "E_CONTACT_CANCELLED"; public static final String E_CONTACT_NO_DATA = "E_CONTACT_NO_DATA"; public static final String E_CONTACT_NO_EMAIL = "E_CONTACT_NO_EMAIL"; diff --git a/ios/RCTContactsWrapper/RCTContactsWrapper.m b/ios/RCTContactsWrapper/RCTContactsWrapper.m index 72243ca..35ee445 100644 --- a/ios/RCTContactsWrapper/RCTContactsWrapper.m +++ b/ios/RCTContactsWrapper/RCTContactsWrapper.m @@ -19,8 +19,8 @@ @interface RCTContactsWrapper() @implementation RCTContactsWrapper int _requestCode; -const int REQUEST_CONTACT = 1; -const int REQUEST_EMAIL = 2; +const int REQUEST_CONTACT = 34222; +const int REQUEST_EMAIL = 68489; RCT_EXPORT_MODULE(ContactsWrapper); @@ -31,10 +31,10 @@ @implementation RCTContactsWrapper self._resolve = resolve; self._reject = reject; _requestCode = REQUEST_CONTACT; - + [self launchContacts]; - - + + } /* Get ontact email as string */ @@ -43,10 +43,10 @@ @implementation RCTContactsWrapper self._resolve = resolve; self._reject = reject; _requestCode = REQUEST_EMAIL; - + [self launchContacts]; - - + + } @@ -54,7 +54,7 @@ @implementation RCTContactsWrapper Launch the contacts UI */ -(void) launchContacts { - + UIViewController *picker; if([CNContactPickerViewController class]) { //iOS 9+ @@ -74,7 +74,7 @@ -(void) launchContacts { } else { [root presentViewController:picker animated:YES completion:nil]; } - + } @@ -131,25 +131,25 @@ - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:( This could also be extended to return arrays of phone numbers, email addresses etc. instead of jsut first found */ NSMutableDictionary *contactData = [self emptyContactDict]; - + NSString *fullName = [self getFullNameForFirst:contact.givenName middle:contact.middleName last:contact.familyName ]; NSArray *phoneNos = contact.phoneNumbers; NSArray *emailAddresses = contact.emailAddresses; - + //Return full name [contactData setValue:fullName forKey:@"name"]; - + //Return first phone number if([phoneNos count] > 0) { CNPhoneNumber *phone = ((CNLabeledValue *)phoneNos[0]).value; [contactData setValue:phone.stringValue forKey:@"phone"]; } - + //Return first email address if([emailAddresses count] > 0) { [contactData setValue:((CNLabeledValue *)emailAddresses[0]).value forKey:@"email"]; } - + [self contactPicked:contactData]; } break; @@ -160,7 +160,7 @@ - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:( [self pickerNoEmail]; return; } - + CNLabeledValue *email = contact.emailAddresses[0].value; [self emailPicked:email]; } @@ -170,8 +170,8 @@ - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:( [self pickerError]; break; } - - + + } @@ -188,7 +188,7 @@ - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)p switch(_requestCode) { case(REQUEST_CONTACT): { - + /* Return NSDictionary ans JS Object to RN, containing basic contact data This is a starting point, in future more fields should be added, as required. This could also be extended to return arrays of phone numbers, email addresses etc. instead of jsut first found @@ -198,26 +198,26 @@ - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)p fNameObject = (__bridge NSString *) ABRecordCopyValue(person, kABPersonFirstNameProperty); mNameObject = (__bridge NSString *) ABRecordCopyValue(person, kABPersonMiddleNameProperty); lNameObject = (__bridge NSString *) ABRecordCopyValue(person, kABPersonLastNameProperty); - + NSString *fullName = [self getFullNameForFirst:fNameObject middle:mNameObject last:lNameObject]; - + //Return full name [contactData setValue:fullName forKey:@"name"]; - + //Return first phone number ABMultiValueRef phoneMultiValue = ABRecordCopyValue(person, kABPersonPhoneProperty); NSArray *phoneNos = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(phoneMultiValue); if([phoneNos count] > 0) { [contactData setValue:phoneNos[0] forKey:@"phone"]; } - + //Return first email ABMultiValueRef emailMultiValue = ABRecordCopyValue(person, kABPersonEmailProperty); NSArray *emailAddresses = (__bridge NSArray *)ABMultiValueCopyArrayOfAllValues(emailMultiValue); if([emailAddresses count] > 0) { [contactData setValue:emailAddresses[0] forKey:@"email"]; } - + [self contactPicked:contactData]; } @@ -231,7 +231,7 @@ - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)p [self pickerNoEmail]; return; } - + [self emailPicked:emailAddresses[0]]; } break; @@ -241,7 +241,7 @@ - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)p [self pickerError]; return; } - + } - (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {