2013-08-29 33 views

回答

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并使用它使用这种

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

希望这将帮助你

0

使用kABPersonModificationDateProperty属性来自ABPerson记录。

CFDateRef modDate = ABRecordCopyValue(record, kABPersonModificationDateProperty); 

给你修改日期。

+0

这就是我已经做了让ABPerson对象。问题是如何以排序顺序检索它们。 –

+0

没有可用的直接排序方法来返回按日期排序的ABPerson。所以,做到这一点的方法是,获取所有修改日期并将其作为NSDate存储在数组中。然后按顺序对NSDate数组进行排序并相应地获取ABPerson。 – iOS

+0

有什么方法可以让我的iOS应用在某些联系人被修改时得到通知? –