代码之家  ›  专栏  ›  技术社区  ›  matt

iOS 13中与历史相关的联系人框架类是什么?

  •  0
  • matt  · 技术社区  · 5 年前

    ios13中新增了一系列与历史相关的类,如CNChangeHistoryEvent和CNChangeHistoryFetchRequest。没有任何文件,也没有提到他们在任何WWDC 2019年的视频,我可以找到。它们是干什么用的?我怎么用它们?

    0 回复  |  直到 5 年前
        1
  •  3
  •   Z S    4 年前

    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 下一次。