Iphone 检索按修改日期排序的联系人
如何检索根据修改日期排序的所有通讯簿联系人?i、 e.与最新修改日期的联系人应在列表中的较早位置。使用ABPerson记录中的Iphone 检索按修改日期排序的联系人,iphone,ios,addressbook,abaddressbook,Iphone,Ios,Addressbook,Abaddressbook,如何检索根据修改日期排序的所有通讯簿联系人?i、 e.与最新修改日期的联系人应在列表中的较早位置。使用ABPerson记录中的kABPersonModificationDateProperty属性 CFDateRef modDate = ABRecordCopyValue(record, kABPersonModificationDateProperty); 提供修改日期。,因为无法直接根据ABPerson的修改日期进行排序 这是我认为有效的方法 - (NSArray *) getSorted
kABPersonModificationDateProperty
属性
CFDateRef modDate = ABRecordCopyValue(record, kABPersonModificationDateProperty);
提供修改日期。,因为无法直接根据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;
}
希望这会对你有所帮助这是我已经做过的。问题是我如何按排序顺序检索它们。没有直接的排序方法可以返回按日期排序的ABPerson。因此,实现这一点的方法是,获取所有修改的日期,并将其作为NSDate存储在数组中。然后按顺序对NSDate数组进行排序,并相应地获取ABPerson。当某个联系人被修改时,我的iOS应用程序是否会收到通知?
ABRecordRef person = ABAddressBookGetPersonWithRecordID(addressbook, (ABRecordID) [[dict valueForKey:@"contactID"] intValue]);