簡體   English   中英

使用ABAddressBookRef從電話簿中檢索聯系人時,應用程序崩潰

[英]Application crashes while retrieving contacts from phone book by using ABAddressBookRef

通過設置將Facebook聯系人同步設置為“打開”時,應用程序崩潰。

 While getting normal phone contact ABRecordRef returns "CPRecord: 0x15fd77510 ABPerson".
   While getting facebook contact ABRecordRef returns (ABRecordRef) person = 0x3ff0000000000000.

編碼

     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
            ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
            CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
            CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
    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 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;
            if (![phoneNumber isEqualToString:@""]) {
                [phoneNumbers addObject:phoneNumber];
            }
        }
        if ([phoneNumbers count] != 0) {
            [contacts setNumbers:phoneNumbers];
        }
    }
}

檢查以下代碼:

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
    CFIndex nPeople = CFArrayGetCount(allPeople);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM