gpt4 book ai didi

ios - 如果应用程序在 iOS 上未激活,我怎么知道联系人已被修改?

转载 作者:搜寻专家 更新时间:2023-11-01 07:17:07 25 4
gpt4 key购买 nike

我需要在联系人更改时运行一个函数。如果应用程序处于事件状态,您可以使用 NotificationCenter 执行此操作,如 post 中所述(有时当我向现有联系人添加新号码时它会起作用)。我如何知道应用程序启动后联系人(或多个联系人)已更改?

最佳答案

我为我的任务做了以下功能

  @objc private func matchingContacts() {
if isSuccessContactUploading {
contactManager.matchingContacts(notMatch: { [weak self] in
guard let _self = self else { return }
debugPrint("matchingContacts != equals")
_self.isSuccessContactUploading = false
_self.syncContacts()
})
}
}

这些功能在ContactManager中

   func matchingContacts(notMatch: (() -> Void)?) {
getContacts { (contacts, error) in
if error == nil {
debugPrint("contacts count", contacts.count)
self.getContactsDictionaryFromCache(contacts, notMatch: {
notMatch?()
})
}
}
}

private func getContactsDictionaryFromCache(_ contacts: [CNContact], notMatch: (() -> Void)?) {
var isMatching = true
for contact in contacts {
let key = contact.identifier

do {
let cache = try Cache<NSDictionary>(name: "Contacts")
if let contactDictionary = cache[key] {
if !contactDictionary.isEqual(to: contact.dictionary) {
debugPrint("contactDictionary not matching")
isMatching = false
}
} else {
debugPrint("contactDictionary isn't here")
isMatching = false
}
} catch {
debugPrint(error.localizedDescription)
isMatching = false
}
}

if !isMatching {
notMatch?()
}

cacheContacts(contacts)
}

private func cacheContacts(_ contacts: [CNContact]) {
for contact in contacts {
let contactDictionary = contact.dictionary as NSDictionary
let key = contact.identifier

do {
let cache = try Cache<NSDictionary>(name: "Contacts")
cache[key] = contactDictionary
} catch {
debugPrint(error.localizedDescription)
}
}
}

关于ios - 如果应用程序在 iOS 上未激活,我怎么知道联系人已被修改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41544641/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com