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

标签 iphone ios addressbook abaddressbook

<分区>

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

最佳答案

由于没有办法直接按照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]);

希望对你有帮助

关于iphone - 检索按修改日期 iOS 排序的联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18504438/

相关文章:

ios - 推送通知 token 是 Mac 和 iOS 独有的吗?

string - 解析具有不明确数据的地址

iphone - iOS电话簿中的竖排字母叫什么?

iphone - 当用户做出选择时如何隐藏 UIPickerView

iphone - 在 iPhone 上的静音模式和响铃模式之间切换

ios - 在不同的表中使用单元格设计

iphone - 将网络应用程序添加到主屏幕

iphone - ABAddressBookSave 随机崩溃

iphone - XCode 4拍摄通用应用(iPhone/iPad)的屏幕截图

iphone - NSDateFormatter dateFromString 返回 nil