File tree 3 files changed +86
-0
lines changed
3 files changed +86
-0
lines changed Original file line number Diff line number Diff line change @@ -209,6 +209,27 @@ contacts.getAllContacts(contactFields).then(
209
209
);
210
210
```
211
211
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
+
212
233
#### getGroups: Find groups. Returns an array of group data.
213
234
214
235
``` js
@@ -520,6 +541,17 @@ Those are the system labels but you can also use any custom label you want.
520
541
}
521
542
```
522
543
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
+
523
555
### iOS
524
556
525
557
See apples docs on properties available:
Original file line number Diff line number Diff line change @@ -53,6 +53,59 @@ exports.getContact = function() {
53
53
page . presentModalViewControllerAnimated ( controller , true ) ;
54
54
} ) ;
55
55
} ;
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
+
56
109
exports . getContactsByName = function ( searchPredicate , contactFields ) {
57
110
return new Promise ( function ( resolve , reject ) {
58
111
var worker ;
Original file line number Diff line number Diff line change @@ -111,6 +111,7 @@ declare module "nativescript-contacts" {
111
111
}
112
112
113
113
export function getContact ( ) : Promise < GetContactResult > ;
114
+ export function getContactById ( id : string ) : Promise < GetFetchResult > ; // iOS Only
114
115
export function getContactsByName ( searchPredicate : string , contactFields : string [ ] ) : Promise < GetFetchResult > ;
115
116
export function getAllContacts ( contactFields : string [ ] ) : Promise < GetFetchResult > ;
116
117
export function getContactsInGroup ( group : Group ) : Promise < GetFetchResult > ;
You can’t perform that action at this time.
0 commit comments