4

連絡先情報に誕生日が保存されている連絡先を取得し、電話番号、電子メール、生年月日などのこれらの連絡先の詳細を取得したい.

これが私が試したものです:

ABAddressBookRef myAddressBook = ABAddressBookCreate();
NSArray *allPeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(myAddressBook);
contactList = [[NSMutableArray alloc]init];

    for (id record in allPeople) 
    {
        NSMutableDictionary *newRecord = [[NSMutableDictionary alloc] init];
        CFTypeRef bDayProperty = ABRecordCopyValue((ABRecordRef)record, kABPersonBirthdayProperty);

        if (ABRecordCopyValue((ABRecordRef)record, kABPersonBirthdayProperty)) 
        {
                NSDate *date=(NSDate*)bDayProperty;
                [newRecord setObject:date forKey:@"birthDate"];
                date=nil;
               [date release];  
        }
      CFRelease(myAddressBook);
    }

何か案が?

4

2 に答える 2

3

このコードを試すことができます。

ABAddressBookRef m_addressbook = ABAddressBookCreate();

if (!m_addressbook) {
    NSLog(@"opening address book");
}

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);
NSLog(@"  cfindex %ld",nPeople);

for ( int i=0;i < nPeople;i++) { 
    NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

    //For username and surname      
    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:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];
        [contactList addObject:dOfPerson];
    }

    //For Phone number
    NSString* mobileLabel;
    for(CFIndex j = 0; j < ABMultiValueGetCount(phones); j++)
           {
        mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, j);
        if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
        {
[dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
        }
        else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
        {
[dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
            break ;
        }

        }
    }
于 2012-05-01T08:35:31.627 に答える
0

最後のコードには、多くのメモリ リークが含まれています。AB...Copy.. (ABRecordCopyValue, ABMultiValueCopyValueAtIndex, ABRecordCopyValue,... ) を使用する場合は常に、メモリを解放する必要があります。

于 2012-05-07T12:35:14.580 に答える