1

I'm trying to select the email id the user wants from the address book. When the user selects the work email or home email, then the corresponding value should be added to the variable. Any suggestion on this? Attached below is my code:

-(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier{
if(property == kABPersonEmailProperty){
    CFStringRef value, label;
    ABMutableMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
    CFIndex count = ABMultiValueGetCount(multi);
    if (count == 1)
    {
        value = ABMultiValueCopyValueAtIndex(multi, 0);
        email = (__bridge NSString*) value;
        NSLog(@"self.emailID   %@",email);
        CFRelease(value);
    }
    else
    {

        for (CFIndex i = 0; i < count; i++)
        {
            label = ABMultiValueCopyLabelAtIndex(multi, i);
            value = ABMultiValueCopyValueAtIndex(multi, i);
                            // check for Work e-mail label
            if (CFStringCompare(label, kABWorkLabel, 0) == 0)
            {
                email = (__bridge NSString*) value;
                NSLog(@"self.emailID   %@",email);
                NSLog(@"%@",(__bridge NSString *)label);

            }

            if(CFStringCompare(label, kABHomeLabel, 0) == 0)
            {
                email = (__bridge NSString*) value;
                NSLog(@"self.emailID   %@",email);
            }

            CFRelease(label);
            CFRelease(value);
        }
    }
    CFRelease(multi);
    [self displayPerson:person];

}else{
    UIAlertView *testAlert = [[UIAlertView alloc] initWithTitle:@"alert" message:@"Please choose a valid email" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel", nil];
    testAlert.tag = 100;
    [testAlert show];
}


  return NO;
}
DesperateLearner
  • 1,115
  • 3
  • 19
  • 45

1 Answers1

0
-(NSArray *)getAllContacts

{ CFErrorRef *error = nil;

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

}
else { // we're on iOS 5 or older
    accessGranted = YES;
}

if (accessGranted) {

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];


    for (int i = 0; i < nPeople; i++)
    {

        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

        ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

        //get First Name and Last Name

        NSString *firstNames = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        if (!firstNames)
            firstNames=@"";


        NSString *lastNames =  (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
        if (!lastNames)
            lastNames=@"";

        // get contacts picture, if pic doesn't exists, show standart one

        NSData  *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
        UIImage *img = [UIImage imageWithData:imgData];
        if (!img) {
            img = [UIImage imageNamed:@"noImage.png"];
        }

        //get 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);
            NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
            [phoneNumbers addObject:phoneNumber];

            //NSLog(@"All numbers %@", phoneNumbers);

        }

        //get contact address

        NSMutableArray *contactAddress = [NSMutableArray new];
        ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
        // Individual Person Address From Contacts

        for (CFIndex j = 0; j < ABMultiValueGetCount(address); j++) {

            CFDictionaryRef addressRef = ABMultiValueCopyValueAtIndex(address, j);

            NSDictionary *addressDict = (__bridge NSDictionary*)addressRef;

            NSString *Street = ([[addressDict objectForKey:@"Street"] length] == 0) ?
            @"" : [addressDict objectForKey:@"Street"];

            NSString *city = ([[addressDict objectForKey:@"City"] length] == 0) ? @"" :
            [addressDict objectForKey:@"City"];

            NSString *State = ([[addressDict objectForKey:@"State"] length] == 0)
            ? @"": [addressDict
                    objectForKey:@"State"];

            NSString *zipCode = ([[addressDict objectForKey:@"ZIP"] length] == 0) ?
            @"" : [addressDict objectForKey:@"ZIP"];

            NSString *country = ([[addressDict objectForKey:@"Country"] length] == 0) ?
            @"" : [addressDict objectForKey:@"Country"];

            NSString *countryCode = ([[addressDict objectForKey:@"CountryCode"] length]
                                     == 0) ? @"" : [addressDict objectForKey:@"CountryCode"];

            NSLog(@"%@ %@ %@ %@ %@ %@", Street, city, State, zipCode, country,
                  countryCode);
            [contactAddress addObject:addressDict];

        }

        //get Contact email

        NSMutableArray *contactEmails = [NSMutableArray new];
        ABMultiValueRef multiEmails = ABRecordCopyValue(person, kABPersonEmailProperty);

        for (CFIndex i=0; i<ABMultiValueGetCount(multiEmails); i++) {
            CFStringRef contactEmailRef = ABMultiValueCopyValueAtIndex(multiEmails, i);
            NSString *contactEmail = (__bridge NSString *)contactEmailRef;

            [contactEmails addObject:contactEmail];
            // NSLog(@"All emails are:%@", contactEmails);

        }
        if ([contactEmails count] != 0) {
            NSData* pictureData = UIImagePNGRepresentation(img);
            [dict setObject:firstNames forKey:@"firstname"];
            [dict setObject:lastNames forKey:@"lastname"];
            [dict setObject:pictureData forKey:@"image"];
            [dict setObject:phoneNumbers forKey:@"number"];
            [dict setObject:contactEmails forKey:@"email"];
            [dict setObject:contactAddress forKey:@"address"];

            [items addObject:dict];
        }
    }
    return items;

} else {

    return NO;
}

}