Steps:
1. Add Observer to Handle Changes in Contact Book
2. Asking For Permission to grant access to use Iphone AddressBook
3. If Granted then Read Address Book and store
//Observer to handle changes in Contact Boo
- (void)viewDidLoad
{
[super viewDidLoad];
{
[super viewDidLoad];
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(nil, nil);
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, (__bridge void *)(self));
ABAddressBookRegisterExternalChangeCallback(addressBook, addressBookChanged, (__bridge void *)(self));
}
// Address Book Authorization grant place this code in viewDidAppear
-(void)viewDidAppear:(BOOL)animated
{
{
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied ||
ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusRestricted)
{
NSLog(@"AB Denied");
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)
{
NSLog(@"AB Authorized");
[self address_book];
}
else
{
ABAddressBookRequestAccessWithCompletion(ABAddressBookCreateWithOptions(NULL, nil), ^(bool granted, CFErrorRef error)
{
if (!granted) {
NSLog(@"AB Just denied");
return;
}
[self address_book];
NSLog(@"AB Just authorized");
});
NSLog(@"AB Not determined");
}
}
//Called automatically when you made any modification in Contacts
void addressBookChanged(ABAddressBookRef addressBook, CFDictionaryRef info, void *context)
{
NSMutableArray*additions = [[NSMutableArray alloc]init];
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for (int i = 0; i < nPeople; i++)
{
NSMutableDictionary *curContact=[[NSMutableDictionary alloc] init];
ABRecordRef person=CFArrayGetValueAtIndex(people, i);
NSString *contacName;
CFTypeRef contacNameValue = ABRecordCopyValue(person, kABPersonFirstNameProperty);
contacName = [[NSString stringWithFormat:@"%@", contacNameValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (contacNameValue)
CFRelease(contacNameValue);
NSString *firstName ;
NSString *lastName;
//Get FirstName Ref
CFTypeRef firstNameValue = ABRecordCopyValue(person, kABPersonFirstNameProperty);
firstName = [[NSString stringWithFormat:@"%@", firstNameValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (firstNameValue)
CFRelease(firstNameValue);
//Get LastName Ref
CFTypeRef LastNameValue = ABRecordCopyValue(person, kABPersonLastNameProperty);
if(LastNameValue)
{
[contacName stringByAppendingString:[NSString stringWithFormat:@" %@", LastNameValue]];
lastName = [[NSString stringWithFormat:@"%@", LastNameValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (LastNameValue)
CFRelease(LastNameValue);
}
NSData *contactImage;
//Get Contact Image Ref
if (ABPersonHasImageData(person) > 0 )
{
CFTypeRef contactImageValue = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
contactImage = (__bridge NSData *)(contactImageValue);
[curContact setObject:contactImage forKey:@"image"];
if (contactImageValue)
CFRelease(contactImageValue);
}
else {
contactImage = UIImageJPEGRepresentation([UIImage imageNamed:@"profile_picture.png"], 1);
[curContact setObject:contactImage forKey:@"image"];
}
ABMultiValueRef phoneNumber = ABRecordCopyValue(person, kABPersonPhoneProperty);
ABMultiValueRef emailInfo = ABRecordCopyValue(person, kABPersonEmailProperty);
if(contacName != NULL) [curContact setObject:contacName forKey:@"Name"];
if(firstName != NULL) [curContact setObject:firstName forKey:@"FirstName"];
if(lastName != NULL) [curContact setObject:lastName forKey:@"LastName"];
[curContact setObject:@"YES" forKey:@"addressbook"];
NSString *phone,*phone2,*phone3;
if (ABMultiValueGetCount(phoneNumber) > 0)
{
//Get phoneValue Ref
CFTypeRef phoneValue = ABMultiValueCopyValueAtIndex(phoneNumber, 0);
phone = [[NSString stringWithFormat:@"%@", phoneValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (phoneValue)
CFRelease(phoneValue);
}
if (ABMultiValueGetCount(phoneNumber) > 1)
{
CFTypeRef phoneValue = ABMultiValueCopyValueAtIndex(phoneNumber, 1);
phone2 = [[NSString stringWithFormat:@"%@", phoneValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (phoneValue)
CFRelease(phoneValue);
phone2 = [phone2 stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phone2 length])];
[curContact setObject:phone2 forKey:@"phoneNo2"];
}
if (ABMultiValueGetCount(phoneNumber) > 2)
{
CFTypeRef phoneValue = ABMultiValueCopyValueAtIndex(phoneNumber, 2);
phone3 = [[NSString stringWithFormat:@"%@", phoneValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (phoneValue)
CFRelease(phoneValue);
phone3 = [phone3 stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phone3 length])];
[curContact setObject:phone3 forKey:@"phoneNo3"];
}
//Get emailInfo Ref
for (int j = 0; j < ABMultiValueGetCount(emailInfo); j++) {
CFTypeRef emailIdValue = ABMultiValueCopyValueAtIndex(emailInfo, j);
NSString *emailId = [[NSString stringWithFormat:@"%@", emailIdValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(emailId != NULL) {
[curContact setObject:emailId forKey:@"UserName"];
[curContact setObject:emailId forKey:[NSString stringWithFormat:@"emailAdday%d",j]];
[curContact setObject:[NSString stringWithFormat:@"%d",j+1] forKey:@"emailCount"];
}
if (emailIdValue) {
CFRelease(emailIdValue);
}
}
if (emailInfo) {
CFRelease(emailInfo);
}
if( contacName == NULL) {
}
else {
NSString * strippedNumber = [phone stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phone length])];
if([strippedNumber length] == 11) {
strippedNumber = [strippedNumber substringFromIndex:1];
}
if(strippedNumber != NULL)
[curContact setObject:strippedNumber forKey:@"phoneNo"];
}
[additions addObject:curContact];
if (phoneNumber)
CFRelease(phoneNumber);
}
NSLog(@"Address Book Contacts : %@",additions );
{
NSMutableArray*additions = [[NSMutableArray alloc]init];
CFArrayRef people = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
for (int i = 0; i < nPeople; i++)
{
NSMutableDictionary *curContact=[[NSMutableDictionary alloc] init];
ABRecordRef person=CFArrayGetValueAtIndex(people, i);
NSString *contacName;
CFTypeRef contacNameValue = ABRecordCopyValue(person, kABPersonFirstNameProperty);
contacName = [[NSString stringWithFormat:@"%@", contacNameValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (contacNameValue)
CFRelease(contacNameValue);
NSString *firstName ;
NSString *lastName;
//Get FirstName Ref
CFTypeRef firstNameValue = ABRecordCopyValue(person, kABPersonFirstNameProperty);
firstName = [[NSString stringWithFormat:@"%@", firstNameValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (firstNameValue)
CFRelease(firstNameValue);
//Get LastName Ref
CFTypeRef LastNameValue = ABRecordCopyValue(person, kABPersonLastNameProperty);
if(LastNameValue)
{
[contacName stringByAppendingString:[NSString stringWithFormat:@" %@", LastNameValue]];
lastName = [[NSString stringWithFormat:@"%@", LastNameValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (LastNameValue)
CFRelease(LastNameValue);
}
NSData *contactImage;
//Get Contact Image Ref
if (ABPersonHasImageData(person) > 0 )
{
CFTypeRef contactImageValue = ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail);
contactImage = (__bridge NSData *)(contactImageValue);
[curContact setObject:contactImage forKey:@"image"];
if (contactImageValue)
CFRelease(contactImageValue);
}
else {
contactImage = UIImageJPEGRepresentation([UIImage imageNamed:@"profile_picture.png"], 1);
[curContact setObject:contactImage forKey:@"image"];
}
ABMultiValueRef phoneNumber = ABRecordCopyValue(person, kABPersonPhoneProperty);
ABMultiValueRef emailInfo = ABRecordCopyValue(person, kABPersonEmailProperty);
if(contacName != NULL) [curContact setObject:contacName forKey:@"Name"];
if(firstName != NULL) [curContact setObject:firstName forKey:@"FirstName"];
if(lastName != NULL) [curContact setObject:lastName forKey:@"LastName"];
[curContact setObject:@"YES" forKey:@"addressbook"];
NSString *phone,*phone2,*phone3;
if (ABMultiValueGetCount(phoneNumber) > 0)
{
//Get phoneValue Ref
CFTypeRef phoneValue = ABMultiValueCopyValueAtIndex(phoneNumber, 0);
phone = [[NSString stringWithFormat:@"%@", phoneValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (phoneValue)
CFRelease(phoneValue);
}
if (ABMultiValueGetCount(phoneNumber) > 1)
{
CFTypeRef phoneValue = ABMultiValueCopyValueAtIndex(phoneNumber, 1);
phone2 = [[NSString stringWithFormat:@"%@", phoneValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (phoneValue)
CFRelease(phoneValue);
phone2 = [phone2 stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phone2 length])];
[curContact setObject:phone2 forKey:@"phoneNo2"];
}
if (ABMultiValueGetCount(phoneNumber) > 2)
{
CFTypeRef phoneValue = ABMultiValueCopyValueAtIndex(phoneNumber, 2);
phone3 = [[NSString stringWithFormat:@"%@", phoneValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (phoneValue)
CFRelease(phoneValue);
phone3 = [phone3 stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phone3 length])];
[curContact setObject:phone3 forKey:@"phoneNo3"];
}
//Get emailInfo Ref
for (int j = 0; j < ABMultiValueGetCount(emailInfo); j++) {
CFTypeRef emailIdValue = ABMultiValueCopyValueAtIndex(emailInfo, j);
NSString *emailId = [[NSString stringWithFormat:@"%@", emailIdValue] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if(emailId != NULL) {
[curContact setObject:emailId forKey:@"UserName"];
[curContact setObject:emailId forKey:[NSString stringWithFormat:@"emailAdday%d",j]];
[curContact setObject:[NSString stringWithFormat:@"%d",j+1] forKey:@"emailCount"];
}
if (emailIdValue) {
CFRelease(emailIdValue);
}
}
if (emailInfo) {
CFRelease(emailInfo);
}
if( contacName == NULL) {
}
else {
NSString * strippedNumber = [phone stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [phone length])];
if([strippedNumber length] == 11) {
strippedNumber = [strippedNumber substringFromIndex:1];
}
if(strippedNumber != NULL)
[curContact setObject:strippedNumber forKey:@"phoneNo"];
}
[additions addObject:curContact];
if (phoneNumber)
CFRelease(phoneNumber);
}
NSLog(@"Address Book Contacts : %@",additions );
NSLog(@"Additions Count: %d",[additions count]);
NSMutableArray * get_ids_input = [NSMutableArray new];
for (NSDictionary * person in additions)
{
NSMutableDictionary * person_input = [NSMutableDictionary new];
[person_input setObject:@"" forKey:@"memberId"];
if (person[@"phoneNo"]) [person_input setObject:person[@"phoneNo"] forKey:@"phoneNo"];
if (person[@"emailAddy"]) [person_input setObject:person[@"emailAddy"] forKey:@"emailAddy"];
else [person_input setObject:@"" forKey:@"emailAddy"];
if (person[@"phoneNo2"]) [person_input setObject:person[@"phoneNo2"] forKey:@"phoneNo2"];
if (person[@"phoneNo3"]) [person_input setObject:person[@"phoneNo3"] forKey:@"phoneNo3"];
[get_ids_input addObject:person_input];
}
if (people)
CFRelease(people);
if (addressBook)
CFRelease(addressBook);
NSLog(@"Home -> ABChanged: Recevied notification");
}
NSMutableArray * get_ids_input = [NSMutableArray new];
for (NSDictionary * person in additions)
{
NSMutableDictionary * person_input = [NSMutableDictionary new];
[person_input setObject:@"" forKey:@"memberId"];
if (person[@"phoneNo"]) [person_input setObject:person[@"phoneNo"] forKey:@"phoneNo"];
if (person[@"emailAddy"]) [person_input setObject:person[@"emailAddy"] forKey:@"emailAddy"];
else [person_input setObject:@"" forKey:@"emailAddy"];
if (person[@"phoneNo2"]) [person_input setObject:person[@"phoneNo2"] forKey:@"phoneNo2"];
if (person[@"phoneNo3"]) [person_input setObject:person[@"phoneNo3"] forKey:@"phoneNo3"];
[get_ids_input addObject:person_input];
}
if (people)
CFRelease(people);
if (addressBook)
CFRelease(addressBook);
NSLog(@"Home -> ABChanged: Recevied notification");
}
//Method to call when Permission Granted
No comments:
Post a Comment