3

如何检索根据修改日期排序的所有通讯录联系人?ie 与最近修改日期的联系应该在列表的前面。

4

2 回答 2

3

由于无法直接根据 ABPerson 的修改日期进行排序,我认为这是可行的

- (NSArray *) getSortedContacts
{

    NSMutableArray * modificationDates = [[NSMutableArray alloc] init];
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    ABAddressBookRef addressBook = ABAddressBookCreate();
    if(addressBook != nil)
    {
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
        if(nPeople > 0)
        {
            CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
            for (int index = 0; index < nPeople; ++index)
            {
                ABRecordRef person = CFArrayGetValueAtIndex(allPeople, index);
                NSNumber *contactID = [NSNumber numberWithInt:ABRecordGetRecordID(person)];
                NSDate *modificationDate = (NSDate*) ABRecordCopyValue(person, kABPersonModificationDateProperty);
                [modificationDates addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:contactID,modificationDate, nil] forKeys:[NSArray arrayWithObjects:@"contactID",@"modificationDate", nil]]];
            }
            if(allPeople)
                CFRelease(allPeople);
            allPeople = nil;
        }
    }
    [pool drain];

    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"modificationDate" ascending:TRUE];
    [modificationDates sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];

    return modificationDates;
}

当你得到排序数组时,从数组中获取字典并使用contactID并使用它来获取ABPerson对象

ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressbook, (ABRecordID) [[dict valueForKey:@"contactID"] intValue]);

希望对你有帮助

于 2013-08-29T08:15:58.713 回答
0

使用kABPersonModificationDatePropertyABPerson 记录中的属性。

CFDateRef modDate = ABRecordCopyValue(record, kABPersonModificationDateProperty);

给你修改日期。

于 2013-08-29T07:18:26.003 回答