Tuesday, January 6, 2015

Addressbook Reading in iOS

To get the basic fields, below few code can be used. 

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, error);

Now get the permission if we have not already

 if (ABAddressBookRequestAccessWithCompletion != NULL) {
                dispatch_semaphore_t sema = dispatch_semaphore_create(0);
                ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
                    dispatch_semaphore_signal(sema);
                });
                dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
}

//Now get the array of all people. 
CFArrayRef allPeople =
            ABAddressBookCopyArrayOfAllPeople(addressBookRef);
            CFIndex nPeople = ABAddressBookGetPersonCount(addressBookRef);
if (allPeople != nil)
{
    for ( int i = 0; i < nPeople; i++ )
    {
         ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i);
         ABRecordID recordId = ABRecordGetRecordID(ref);
         CFStringRef lastNameMultiValueCF = ABRecordCopyValue(ref, kABPersonLastNameProperty);
         CFStringRef firstNmaeMultiValueCF = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
         CFStringRef companyNameMultiValueC = ABRecordCopyValue(ref, kABPersonOrganizationProperty);
         CFStringRef nickNameMultiValueC = ABRecordCopyValue(ref, kABPersonNicknameProperty);
     }
}

Each of these CFStringRef can be converted to NSString using the type casting. Given below 

NSString *firstName =  (__bridge NSString *)firstNmaeMultiValueCF;

Getting all phone numbers:

ABMultiValueRef phoneNumsRef= ABRecordCopyValue(ref,kABPersonPhoneProperty);
if(phoneNumsRef)
{
NSArray *phNumbers = (__bridge_transfer NSArray*)ABMultiValueCopyArrayOfAllValues(phoneNumsRef);
if(phNumbers)
{
for(int j = 0; j < [phNumber count]; j++)
        {
//get the phone number
NSString *ph = [phNumber objectAtIndex:j];
//Now get the label for this address book item
NSString *label = nil;
CFStringRef unlocalizedLabel = ABMultiValueCopyLabelAtIndex(phoneNumbarsRef, j);
           if (unlocalizedLabel)
           {
               label = CFBridgingRelease(ABAddressBookCopyLocalizedLabel(unlocalizedLabel));
                                    CFRelease(unlocalizedLabel);
           }

    }
}
}

Getting all email values
Reading all emails is similar way as reading the phone number and the code is like below 

ABMultiValueRef emailRef= ABRecordCopyValue(ref,kABPersonEmailProperty);
NSArray *emails = (__bridge_transfer NSArray*)ABMultiValueCopyArrayOfAllValues(emailRef);
//individual emails can be iterated and found out like in code below.
for(int j = 0; j < [emailCount count]; j++)
{
NSString *ph = [emails objectAtIndex:j];
}

Similar to the Phone numbers, we can get the email labels using the below lines
CFStringRef unlocalizedLabel = ABMultiValueCopyLabelAtIndex(emailRef, j);
if (unlocalizedLabel)
{
     label = CFBridgingRelease(ABAddressBookCopyLocalizedLabel(unlocalizedLabel));
     CFRelease(unlocalizedLabel);
}

Getting Address components for a person contact
ABMultiValueRef addessRef= ABRecordCopyValue(ref,kABPersonAddressProperty);
NSArray *addressArray = (__bridge_transfer NSArray*)ABMultiValueCopyArrayOfAllValues(addessRef);

Now, we can iterate through the address array items and we can get the individual components for it. 


for(int j = 0; j < [addressArray count]; j++)
{
     Address *add = [[Address alloc] init];
     NSDictionary *addressDic = [addressArray objectAtIndex:j];
     if([addressDic objectForKey:@"City"])
         add.city =[addressDic objectForKey:@"City"];
     if([addressDic objectForKey:@"CountryCode"])
         add.country = [addressDic objectForKey:@"CountryCode"];
     if([addressDic objectForKey:@"State"])
         add.state = [addressDic objectForKey:@"State"];
     if([addressDic objectForKey:@"Street"])
         add.street = [addressDic objectForKey:@"Street"];
     if([addressDic objectForKey:@"ZIP"])
         add.zipCode = [addressDic objectForKey:@"ZIP"];
      contact.addresses addObject:add];
}


Reading Image Data: 

To know whether the person item having the image data, the ABPersonHasImageData API can be used. 
NSData *imageData = (__bridge NSData*)ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail);
image = [UIImage imageWithData:imageData];
if(image)
{
//do something with the image
}    
CFRelease((__bridge CFTypeRef)(imageData)); 


No comments:

Post a Comment