Created
April 6, 2015 19:56
-
-
Save darylrowland/62a5f12981c4669394a8 to your computer and use it in GitHub Desktop.
React Native Contacts API Integration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContactsManager.h | |
// mewants | |
// | |
// Created by Daryl Rowland on 31/03/2015. | |
// | |
#import "RCTBridgeModule.h" | |
@interface ContactsManager: NSObject<RCTBridgeModule> | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ContactsManager.m | |
// mewants | |
// | |
// Created by Daryl Rowland on 31/03/2015. | |
// | |
#import "ContactsManager.h" | |
@import AddressBook; | |
@implementation ContactsManager | |
-(void)requestAccessToContacts:(RCTResponseSenderBlock)callback { | |
RCT_EXPORT(); | |
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error) { | |
if (!granted){ | |
callback(@[@"Access to contacts not granted"]); | |
} | |
callback(@[[NSNull null], @"Access to contacts granted"]); | |
}); | |
} | |
-(void)listAllContacts:(RCTResponseSenderBlock)callback { | |
RCT_EXPORT(); | |
CFErrorRef *error = NULL; | |
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error); | |
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook); | |
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook); | |
NSMutableArray *allContacts = [NSMutableArray new]; | |
for(int i = 0; i < numberOfPeople; i++) { | |
ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i ); | |
NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty)); | |
NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty)); | |
BOOL hasName = false; | |
NSMutableDictionary *personObj = [NSMutableDictionary new]; | |
if (firstName) { | |
[personObj setObject: firstName forKey:@"firstName"]; | |
hasName = true; | |
} | |
if (lastName) { | |
[personObj setObject: lastName forKey:@"lastName"]; | |
hasName = true; | |
} | |
if (hasName) { | |
// Only add to address book if the person has a name and number | |
ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty); | |
NSMutableArray *phoneNumberArray = [NSMutableArray new]; | |
for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) { | |
NSString *phoneNumber = (__bridge_transfer NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i); | |
[phoneNumberArray addObject:phoneNumber]; | |
} | |
if (phoneNumberArray.count > 0) { | |
[personObj setObject: phoneNumberArray forKey:@"phoneNumbers"]; | |
if (hasName && ABPersonHasImageData(person)) { | |
CFDataRef photoData = ABPersonCopyImageData(person); | |
NSData* data = (__bridge NSData*)photoData; | |
// write to temp directory and store URI in photos array | |
// get the temp directory path | |
// Write to docs directory | |
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); | |
NSString *docsPath = [paths objectAtIndex:0]; //Get the docs directory | |
//NSString* docsPath = [NSTemporaryDirectory()stringByStandardizingPath]; | |
NSError* err = nil; | |
NSString* filePath = [NSString stringWithFormat:@"%@/photo_XXXXX", docsPath]; | |
char template[filePath.length + 1]; | |
strcpy(template, [filePath cStringUsingEncoding:NSASCIIStringEncoding]); | |
mkstemp(template); | |
filePath = [[NSFileManager defaultManager] | |
stringWithFileSystemRepresentation:template | |
length:strlen(template)]; | |
// save file | |
if ([data writeToFile:filePath options:NSAtomicWrite error:&err]) { | |
[personObj setObject: filePath forKey:@"photoUrl"]; | |
} | |
CFRelease(photoData); | |
} | |
[allContacts addObject:personObj]; | |
} | |
} | |
} | |
callback(@[[NSNull null], allContacts]); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ContactsManager = require("NativeModules").ContactsManager; | |
module.exports = { | |
getAllContacts: function(callback) { | |
// Request access to the contacts API | |
ContactsManager.requestAccessToContacts(function(err, result) { | |
if (err) { | |
// Access not granted | |
return callback(err); | |
} else { | |
ContactsManager.listAllContacts(function(err, contacts) { | |
callback(err, contacts); | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment