Skip to content

Commit 0110fe7

Browse files
authored
Merge pull request #65 from jzgoda/add-get-contact-by-id
Adds `getContactById` search method
2 parents e21a71a + 7f2bddc commit 0110fe7

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

README.md

+32
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,27 @@ contacts.getAllContacts(contactFields).then(
209209
);
210210
```
211211

212+
#### getContactById: Finds the contact with the matching identifier. Returns GetFetchResult. *(iOS Only)*
213+
```js
214+
var app = require("application");
215+
var contacts = require("nativescript-contacts");
216+
217+
var contactId = '[Contact Identifier]'; // Assumes this is a valid contact identifier (Contact.id)
218+
219+
contacts.getContactById(contactId).then(
220+
function(args) {
221+
console.log("getContactById Complete");
222+
console.log(JSON.stringify(args));
223+
/// Returns args:
224+
/// args.data: Generic cross platform JSON object, null if no contacts were found.
225+
/// args.reponse: "fetch"
226+
},
227+
function(err) {
228+
console.log("Error: " + err);
229+
}
230+
);
231+
```
232+
212233
#### getGroups: Find groups. Returns an array of group data.
213234

214235
```js
@@ -520,6 +541,17 @@ Those are the system labels but you can also use any custom label you want.
520541
}
521542
```
522543

544+
### `GetFetchResult` Data Structure
545+
546+
The object returned by contact fetch requests.
547+
548+
```js
549+
{
550+
data: Contact[];
551+
response: string;
552+
}
553+
```
554+
523555
### iOS
524556

525557
See apples docs on properties available:

index.ios.js

+53
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,59 @@ exports.getContact = function() {
5353
page.presentModalViewControllerAnimated(controller, true);
5454
});
5555
};
56+
57+
exports.getContactById = function(id) {
58+
return new Promise(function(resolve, reject) {
59+
if (!id) {
60+
reject('Missing Contact Identifier');
61+
}
62+
const store = new CNContactStore();
63+
const searchPredicate = CNContact.predicateForContactsWithIdentifiers([id]);
64+
const keysToFetch = [
65+
"givenName",
66+
"familyName",
67+
"middleName",
68+
"namePrefix",
69+
"nameSuffix",
70+
"phoneticGivenName",
71+
"phoneticMiddleName",
72+
"phoneticFamilyName",
73+
"nickname",
74+
"jobTitle",
75+
"departmentName",
76+
"organizationName",
77+
"note",
78+
"phoneNumbers",
79+
"emailAddresses",
80+
"postalAddresses",
81+
"urlAddresses",
82+
"imageData",
83+
"imageDataAvailable"
84+
]; // All Properties that we are using in the Model
85+
let error;
86+
const foundContacts = store.unifiedContactsMatchingPredicateKeysToFetchError(searchPredicate, keysToFetch, error);
87+
88+
if (error) {
89+
reject(error.localizedDescription);
90+
}
91+
92+
if (foundContacts && foundContacts.count > 0) {
93+
let contactModel = new Contact();
94+
contactModel.initializeFromNative(foundContacts[0]);
95+
96+
resolve({
97+
data: [contactModel],
98+
response: "fetch"
99+
});
100+
} else {
101+
resolve({
102+
data: null,
103+
response: "fetch"
104+
});
105+
}
106+
});
107+
}
108+
56109
exports.getContactsByName = function(searchPredicate, contactFields) {
57110
return new Promise(function(resolve, reject) {
58111
var worker;

nativescript-contacts.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ declare module "nativescript-contacts" {
111111
}
112112

113113
export function getContact(): Promise<GetContactResult>;
114+
export function getContactById(id: string): Promise<GetFetchResult>; // iOS Only
114115
export function getContactsByName(searchPredicate: string, contactFields: string[]): Promise<GetFetchResult>;
115116
export function getAllContacts(contactFields: string[]): Promise<GetFetchResult>;
116117
export function getContactsInGroup(group: Group): Promise<GetFetchResult>;

0 commit comments

Comments
 (0)