从地址簿中选择电子邮件 - iOS

时间:2012-10-08 10:52:50

标签: ios xcode addressbook abpeoplepickerview

我正在尝试从地址簿中选择用户想要的电子邮件ID。当用户选择工作电子邮件或家庭电子邮件时,应将相应的值添加到变量中。有什么建议吗?下面是我的代码:

-(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;
}

1 个答案:

答案 0 :(得分: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;
}

}