Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change Request Constant into Some Random Numbers #47

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ dependencies {
in `MainApplication.java`
add package to getPacakges()

```
```java
import com.lynxit.contactswrapper.ContactsWrapperPackage;
...

Expand All @@ -57,15 +57,15 @@ protected List<ReactPackage> getPackages() {

in `AndroidManifest.xml`
make sure you have the following setting even if you have done `react-native upgrade`
```
```xml
<application
android:name=".MainApplication"

```

Also add
Also add

```
```xml
<uses-permission android:name="android.permission.READ_CONTACTS" />
```

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
52 changes: 26 additions & 26 deletions ios/RCTContactsWrapper/RCTContactsWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -31,10 +31,10 @@ @implementation RCTContactsWrapper
self._resolve = resolve;
self._reject = reject;
_requestCode = REQUEST_CONTACT;

[self launchContacts];


}

/* Get ontact email as string */
Expand All @@ -43,18 +43,18 @@ @implementation RCTContactsWrapper
self._resolve = resolve;
self._reject = reject;
_requestCode = REQUEST_EMAIL;

[self launchContacts];


}


/**
Launch the contacts UI
*/
-(void) launchContacts {

UIViewController *picker;
if([CNContactPickerViewController class]) {
//iOS 9+
Expand All @@ -74,7 +74,7 @@ -(void) launchContacts {
} else {
[root presentViewController:picker animated:YES completion:nil];
}

}


Expand Down Expand Up @@ -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;
Expand All @@ -160,7 +160,7 @@ - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(
[self pickerNoEmail];
return;
}

CNLabeledValue *email = contact.emailAddresses[0].value;
[self emailPicked:email];
}
Expand All @@ -170,8 +170,8 @@ - (void)contactPicker:(CNContactPickerViewController *)picker didSelectContact:(
[self pickerError];
break;
}


}


Expand All @@ -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
Expand All @@ -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];
}
Expand All @@ -231,7 +231,7 @@ - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)p
[self pickerNoEmail];
return;
}

[self emailPicked:emailAddresses[0]];
}
break;
Expand All @@ -241,7 +241,7 @@ - (void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)p
[self pickerError];
return;
}

}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker {
Expand Down