Можно ли получить время создания записи контактов iPhone? - PullRequest
1 голос
/ 24 сентября 2010

Я хочу задать вопрос об iPhone. Можно ли получить время создания из контактов iPhone каждой записи? Спасибо.

Ответы [ 2 ]

4 голосов
/ 24 сентября 2010

Да. Вы хотите kABPersonCreationDateProperty. См. справочник .

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
0 голосов
/ 30 апреля 2014

Для 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);
...