3
如何检索根据修改日期排序的所有地址簿联系人?即与最近修改日期的联系应该在列表中提前。检索按修改日期排序的联系人iOS
如何检索根据修改日期排序的所有地址簿联系人?即与最近修改日期的联系应该在列表中提前。检索按修改日期排序的联系人iOS
至于有没有办法直接排序根据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并使用它使用这种
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressbook, (ABRecordID) [[dict valueForKey:@"contactID"] intValue]);
希望这将帮助你
使用kABPersonModificationDateProperty
属性来自ABPerson记录。
CFDateRef modDate = ABRecordCopyValue(record, kABPersonModificationDateProperty);
给你修改日期。
这就是我已经做了让ABPerson对象。问题是如何以排序顺序检索它们。 –
没有可用的直接排序方法来返回按日期排序的ABPerson。所以,做到这一点的方法是,获取所有修改日期并将其作为NSDate存储在数组中。然后按顺序对NSDate数组进行排序并相应地获取ABPerson。 – iOS
有什么方法可以让我的iOS应用在某些联系人被修改时得到通知? –