API的运行方式应该是,您可以将一个“token”传递给change history请求,它将为您提供自该token之后添加/删除/更新的联系人(或组)。
到目前为止,我只能“运行”历史获取请求,如下所示:
CNChangeHistoryFetchRequest *fetchHistory = [[CNChangeHistoryFetchRequest alloc] init];
fetchHistory.startingToken = [[NSUserDefaults standardUserDefaults] dataForKey:@"CNContactChangeHistoryToken"];
NSError *error = nil;
CNContactStore *store = [[CNContactStore alloc] init];
CNFetchResult *fetchResult = [store enumeratorForChangeHistoryFetchRequest:fetchHistory error:&error];
NSEnumerator *enumerator = [fetchResult value];
id object;
while ((object = [enumerator nextObject])) {
// do something with object
NSLog(@"change history enumerator object = %@", object);
CNChangeHistoryEvent *historyEvent = (CNChangeHistoryEvent *) object;
if ([historyEvent isKindOfClass:[CNChangeHistoryDropEverythingEvent class]]) {
NSLog(@"change history - DROP EVERYTHING!");
[historyEvent acceptEventVisitor: self];
} else {
if ([historyEvent isKindOfClass:[CNChangeHistoryAddContactEvent class]]) {
CNChangeHistoryAddContactEvent *addContactEvent = (CNChangeHistoryAddContactEvent *) object;
NSLog(@"change history - AddContact event container %@ - %@", addContactEvent.containerIdentifier, addContactEvent.contact);
} else if ([historyEvent isKindOfClass:[CNChangeHistoryUpdateContactEvent class]]) {
CNChangeHistoryUpdateContactEvent *updateContactEvent = (CNChangeHistoryUpdateContactEvent *) object;
NSLog(@"change history - UpdateContact event - %@", updateContactEvent.contact);
} else if ([historyEvent isKindOfClass:[CNChangeHistoryDeleteContactEvent class]]) {
CNChangeHistoryDeleteContactEvent *deleteContactEvent = (CNChangeHistoryDeleteContactEvent *) object;
NSLog(@"change history - DeleteContact event - %@", deleteContactEvent.contactIdentifier);
}
}
}
枚举将运行,并且始终是“CNChangeHistoryDropEverythingEvent”事件,然后是整个联系人列表的“Add Contact”和“Add Group”事件。这是因为我找不到任何地方获取当前令牌的方法。“fetchResult”对象
应该
currentHistoryToken
但它总是零;CNContactStore的
当前历史标记
startingToken
下一次。