-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRCTAddressBook.m
More file actions
177 lines (149 loc) · 6.09 KB
/
RCTAddressBook.m
File metadata and controls
177 lines (149 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// AddressBook.m
//
// Created by mattotodd on 4/6/15.
// Copyright (c) 2015 Facebook. All rights reserved.
//
@import AddressBook;
#import <UIKit/UIKit.h>
#import "RCTAddressBook.h"
@implementation RCTAddressBook
RCT_EXPORT_MODULE(@"AddressBook");
- (NSDictionary *)constantsToExport
{
return @{
@"Denied": @"denied",
@"Authorized": @"authorized",
@"Undetermined": @"undetermined"
};
}
- (void)hasABAuth:(RCTResponseSenderBlock)callback {
RCT_EXPORT(checkPermissions);
NSMutableDictionary *permissions = [[NSMutableDictionary alloc] init];
ABAuthorizationStatus currentStatus = ABAddressBookGetAuthorizationStatus();
permissions[@"contacts"] = @"undetermined";
if (currentStatus== kABAuthorizationStatusDenied || currentStatus == kABAuthorizationStatusRestricted){
permissions[@"contacts"] = @"denied";
} else if (currentStatus == kABAuthorizationStatusAuthorized){
permissions[@"contacts"] = @"authorized";
}
callback(@[[NSNull null], permissions]);
}
- (void)requestABAuth:(RCTResponseSenderBlock)callback {
RCT_EXPORT(requestPermissions);
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) {
NSMutableDictionary *permissions = [[NSMutableDictionary alloc] init];
permissions[@"contacts"] = (!granted) ? @"denied" : @"authorized";
callback(@[[NSNull null], permissions]);
});
}
-(void)getAllContacts: (RCTResponseSenderBlock)callback {
RCT_EXPORT();
ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, nil);
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBookRef, NULL, kABPersonSortByLastName);
int totalContacts = (int)[allContacts count];
int currentIndex = 0;
int maxIndex = --totalContacts;
NSMutableArray *serializedContacts = [NSMutableArray new];
while (currentIndex <= maxIndex){
NSDictionary *contact = [self dictionaryRepresentationForABPerson: (ABRecordRef)[allContacts objectAtIndex:(long)currentIndex]];
if(contact){
[serializedContacts addObject:contact];
}
currentIndex++;
}
callback(@[[NSNull null], serializedContacts]);
}
-(NSDictionary*) dictionaryRepresentationForABPerson:(ABRecordRef) person
{
NSMutableDictionary* contact = [NSMutableDictionary dictionary];
NSString *firstName = (__bridge_transfer NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
NSString *lastName = (__bridge_transfer NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
NSString *middleName = (__bridge_transfer NSString *)(ABRecordCopyValue(person, kABPersonMiddleNameProperty));
BOOL hasName = false;
if (firstName) {
[contact setObject: firstName forKey:@"givenName"];
hasName = true;
}
if (lastName) {
[contact setObject: lastName forKey:@"familyName"];
hasName = true;
}
if(middleName){
[contact setObject: (middleName) ? middleName : @"" forKey:@"middleName"];
}
if(!hasName){
//nameless contact, do not include in results
return nil;
}
//handle phone numbers
NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];
ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {
CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
CFStringRef phoneLabelRef = ABMultiValueCopyLabelAtIndex(multiPhones, i);
NSString *phoneNumber = (__bridge_transfer NSString *) phoneNumberRef;
NSString *phoneLabel = (__bridge_transfer NSString *) ABAddressBookCopyLocalizedLabel(phoneLabelRef);
if(phoneNumberRef){
CFRelease(phoneNumberRef);
}
if(phoneLabelRef){
CFRelease(phoneLabelRef);
}
NSMutableDictionary* phone = [NSMutableDictionary dictionary];
[phone setObject: phoneNumber forKey:@"phoneNumber"];
[phone setObject: phoneLabel forKey:@"phoneLabel"];
[phoneNumbers addObject:phone];
}
[contact setObject: phoneNumbers forKey:@"phoneNumbers"];
//end phone numbers
//handle emails
NSMutableArray *emailAddreses = [[NSMutableArray alloc] init];
ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);
for(CFIndex i=0;i<ABMultiValueGetCount(multiEmails);i++) {
CFStringRef emailAddressRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
CFStringRef emailLabelRef = ABMultiValueCopyLabelAtIndex(multiEmails, i);
NSString *emailAddress = (__bridge_transfer NSString *) emailAddressRef;
NSString *emailLabel = (__bridge_transfer NSString *) ABAddressBookCopyLocalizedLabel(emailLabelRef);
if(emailAddressRef){
CFRelease(emailAddressRef);
}
if(emailLabelRef){
CFRelease(emailLabelRef);
}
NSMutableDictionary* email = [NSMutableDictionary dictionary];
[email setObject: emailAddress forKey:@"emailAddress"];
[email setObject: emailLabel forKey:@"emailLabel"];
[emailAddreses addObject:email];
}
//end emails
[contact setObject: emailAddreses forKey:@"emailAddresses"];
[contact setObject: [self getABPersonThumbnailFilepath:person] forKey:@"thumbnailPath"];
return contact;
}
-(NSString *) getABPersonThumbnailFilepath:(ABRecordRef) person
{
if (ABPersonHasImageData(person)){
CFDataRef photoDataRef = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
if(!photoDataRef){
return nil;
}
NSData* data = (__bridge_transfer NSData*)photoDataRef;
NSString* tempPath = [NSTemporaryDirectory()stringByStandardizingPath];
NSError* err = nil;
NSString* tempfilePath = [NSString stringWithFormat:@"%@/thumbimage_XXXXX", tempPath];
char template[tempfilePath.length + 1];
strcpy(template, [tempfilePath cStringUsingEncoding:NSASCIIStringEncoding]);
mkstemp(template);
tempfilePath = [[NSFileManager defaultManager]
stringWithFileSystemRepresentation:template
length:strlen(template)];
[data writeToFile:tempfilePath options:NSAtomicWrite error:&err];
CFRelease(photoDataRef);
if(!err){
return tempfilePath;
}
}
return @"";
}
@end