显示Addressbook中的联系人

时间:2015-05-13 10:45:46

标签: ios objective-c abaddressbook

我需要访问用户地址簿,并使用profile image和每个用户的name填充表格视图。

我收到警告'ABAddressBookCreate' is deprecated: first deprecated in iOS 6.0,也没有显示任何联系人。

到目前为止我的代码:

ABAddressBookRef m_addressbook = ABAddressBookCreate();



    if (!m_addressbook) {

        NSLog(@"opening address book");

    }



    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);

    CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);



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

        NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];



        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);



        //For username and surname

        ABMultiValueRef phones =(__bridge ABMultiValueRef)((__bridge_transfer NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty));

        CFStringRef firstName, lastName;

        firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);

        lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);

        [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];



        //For Email ids

        ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);

        if(ABMultiValueGetCount(eMail) < 0) {

            [dOfPerson setObject:(__bridge NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];



        }



        //For Phone number

        NSString* mobileLabel;

        for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {

            mobileLabel = (__bridge NSString*)ABMultiValueCopyLabelAtIndex(phones, i);

            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])

            {

                [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];

            }

            else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])

            {

                [dOfPerson setObject:(__bridge NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];

                break ;

            }



            [contactList addObject:dOfPerson];

            CFRelease(ref);

            CFRelease(firstName);

            CFRelease(lastName);

        }

        NSLog(@"array is %@",contactList);

    }

1 个答案:

答案 0 :(得分:2)

//请求授权给地址簿

ABAddressBookRef addressBookRef = ABAddressBookCreateWithOptions(NULL, NULL);

if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
    ABAddressBookRequestAccessWithCompletion(addressBookRef, ^(bool granted, CFErrorRef error) {
        if (granted) {
            // First time access has been granted, add the contact

           [self fetchFromAddressBook];
        } else {
            // User denied access
            // Display an alert telling user the contact could not be added
        }
    });
}
else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized) {
    // The user has previously given access, add the contact
   [self fetchFromAddressBook];
}
else {
    // The user has previously denied access
    // Send an alert telling user to change privacy setting in settings app
}

//从地址簿中提取

-(void)fetchFromAddressBook{


CFErrorRef *error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

NSInteger actual;



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

    ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

    NSString *firstName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));
    NSString *lastName = (__bridge NSString *)(ABRecordCopyValue(person, kABPersonLastNameProperty));
    ABRecordID recordID = ABRecordGetRecordID(person);
    NSDate *birthday = (__bridge NSDate *)(ABRecordCopyValue(person, kABPersonBirthdayProperty));
    NSData  *imgData = (__bridge_transfer NSData *) ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatOriginalSize);



 ABMultiValueRef phoneNumbers = ABRecordCopyValue(( ABRecordRef)(person), kABPersonPhoneProperty);

NSString* mobile=@"";
for (int i=0; i < ABMultiValueGetCount(phoneNumbers); i++) {

    mobile = (NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i);

    NSCharacterSet *trim = [NSCharacterSet characterSetWithCharactersInString:@"#();$&-+"];
    mobile = [[mobile componentsSeparatedByCharactersInSet: trim] componentsJoinedByString: @""];
    mobile= [mobile stringByReplacingOccurrencesOfString:@"\"" withString:@""];

    mobile=[mobile stringByReplacingOccurrencesOfString:@" " withString:@""];


}

    NSLog(@"Name:%@ %@", firstName, birthday,mobile);

}



}