检索按修改日期iOS排序的联系人

时间:2013-08-29 07:11:35

标签: iphone ios addressbook abaddressbook

如何检索根据修改日期排序的所有地址簿联系人?即,最新修改日期的联系人应该在列表的前面。

2 个答案:

答案 0 :(得分: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]);

希望这会对你有所帮助

答案 1 :(得分:0)

使用ABPerson记录中的kABPersonModificationDateProperty属性。

CFDateRef modDate = ABRecordCopyValue(record, kABPersonModificationDateProperty);

为您提供修改日期。