是否可以检索iPhone联系人记录的创建时间?

时间:2010-09-24 10:23:33

标签: iphone objective-c addressbook

我想问一个关于iPhone的问题。是否可以从每条记录的iPhone联系人中检索创建时间?谢谢。

2 个答案:

答案 0 :(得分:4)

是。你想要kABPersonCreationDateProperty。请参阅reference

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];    

ABAddressBookRef addressBook = ABAddressBookCreate();

NSArray* allPeople = (NSArray*) ABAddressBookCopyArrayOfAllPeople( addressBook );
for( id aPerson in allPeople ) {
    NSString* firstName = (NSString*) ABRecordCopyValue( aPerson, kABPersonFirstNameProperty );
    NSString* lastName = (NSString* ) ABRecordCopyValue( aPerson, kABPersonLastNameProperty );
    NSDate* createDate = (NSDate*) ABRecordCopyValue( aPerson, kABPersonCreationDateProperty );
    NSString* formattedDate = [dateFormatter stringFromDate:createDate];

    NSLog( @"%@ %@ created %@", firstName, lastName, formattedDate );

    [firstName release];
    [lastName release];
    [createDate release];
}

[allPeople release];
[dateFormatter release];
CFRelease(addressBook);

哪些输出......

AddressBookTest[6202:207] Zacharias Pasternack created 9/24/10 9:03:34 AM PDT
AddressBookTest[6202:207] Some Other Guy created 9/24/10 9:07:18 AM PDT

答案 1 :(得分:0)

对于iOS6 +,它有点不同:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

/* -> I left out the ask for permission code here */

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, nil);

if (addressBook != nil)
{        
    NSArray *allPeople = (__bridge_transfer NSArray
                            *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    for( id contactPerson in allPeople )
    {
        NSString *firstName = (__bridge_transfer NSString
                               *)ABRecordCopyValue((__bridge ABRecordRef)(contactPerson), kABPersonFirstNameProperty);

        NSDate* createDate = (__bridge_transfer NSDate*) ABRecordCopyValue((__bridge ABRecordRef)(contactPerson), kABPersonCreationDateProperty );
        NSString* formattedDate = [dateFormatter stringFromDate:createDate];

        NSLog( @"%@ created %@", firstName, formattedDate );
    }
}

CFRelease(addressBook);