在tableview中导入ios联系人

时间:2014-03-27 09:58:06

标签: iphone objective-c arrays ios7

我在互联网上搜索导入iOS中的UITableViewController个联系人,因为我有一个UITableViewController作为UINavigationController的根,我称之为{{1}的联系人我和我发现的是如何使用模态segue导入联系人选择器,我不希望我想在自定义单元格中导入它们并对它们进行编辑。那我怎么能这样做呢?

1 个答案:

答案 0 :(得分:2)

尝试此功能,这对我有用。

 -(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) {

#ifdef DEBUG
        NSLog(@"Fetching contact info ----> ");
#endif


        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++)
        {
            ContactsData *contacts = [ContactsData new];

            ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

            //get First Name and Last Name

            contacts.firstNames = (__bridge NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);

            contacts.lastNames =  (__bridge NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);

            if (!contacts.firstNames) {
                contacts.firstNames = @"";
            }
            if (!contacts.lastNames) {
                contacts.lastNames = @"";
            }

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

            NSData  *imgData = (__bridge NSData *)ABPersonCopyImageData(person);
            contacts.image = [UIImage imageWithData:imgData];
            if (!contacts.image) {
                contacts.image = [UIImage imageNamed:@"NOIMG.png"];
            }
            //get Phone Numbers

            NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];

            ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
            if (ABMultiValueGetCount(multiPhones)<0)
            {
                contacts.phonenumber=@"";
            }
            for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++) {

                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
                NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
                contacts.phonenumber=phoneNumber;
                [phoneNumbers addObject:phoneNumber];

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

            }


            [contacts setNumbers:phoneNumbers];

            //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);

            }

            [contacts setEmails:contactEmails];



            [items addObject:contacts];

            [Contactarray addObject:contacts];

#ifdef DEBUG
            NSLog(@"Person is: %@", contacts.firstNames);
            //            NSLog(@"Phones are: %@", contacts.numbers);
            //            NSLog(@"Email is:%@", contacts.emails);
#endif




        }
        NSLog(@"%@",items);
        return items;



    } else {
#ifdef DEBUG
        NSLog(@"Cannot fetch Contacts :( ");
#endif
        return NO;


    }

}

我希望这段代码对你有用。