首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UNNotificationServiceExtension退出调用CNContactStore enumerateContacts

UNNotificationServiceExtension退出调用CNContactStore enumerateContacts
EN

Stack Overflow用户
提问于 2018-04-09 21:49:46
回答 1查看 281关注 0票数 0

我正在使用UNNotificationServiceExtension替换通知中的一个电话号码。我试图在CNContactStore中查找电话号码,并将Ph#替换为联系人名称。

我的问题是,当我调用CNContactStore enumerateContacts(with: keysToFetch:)时,这个扩展名就会退出,而不会从CNContactStore调用返回。

另一方面,如果我调用CNContactStore的unifiedContacts(匹配:谓词,keysToFetch:键),它将按预期返回。但不幸的是,这个电话找不到电话号码。我唯一能查到电话号码的方法就是打电话给enumerateContacts.

我使用相同的代码在我的应用程序中查找电话号码,它运行良好。我还可以替换通知扩展w/o问题中的文本。只有当我试图在扩展中调用enumerateContacts时,问题才会发生。

代码语言:javascript
复制
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    self.contentHandler = contentHandler
    bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

    let searchPhoneNumber = "5555551234"

    let keys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey] as [CNKeyDescriptor]
    let contactsStore = CNContactStore()
    do {
        try contactsStore.enumerateContacts(with: CNContactFetchRequest(keysToFetch: keys)) {
            (contact, error) -> Void in

            print("We never get here!!!")
            if (!contact.phoneNumbers.isEmpty) {

                for phoneNumber in contact.phoneNumbers {
                    if phoneNumber.value.stringValue == searchPhoneNumber {
                        // swap number for name
                        self.bestAttemptContent?.body = contact.givenName
                        contentHandler(self.bestAttemptContent!)
                        return
                    }
                }
            }
        }
    }
    catch {
        print("And we never get here.")
        contentHandler(bestAttemptContent!)
        return
    }
    contentHandler(bestAttemptContent!)
}
EN

回答 1

Stack Overflow用户

发布于 2018-10-15 11:54:06

来自UNNotificationServiceExtension https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension

该方法执行其任务和执行所提供的完成块的时间有限。如果您的方法没有及时完成,系统将调用serviceExtensionTimeWillExpire()方法,给您最后一次提交更改的机会。如果在过期之前不更新通知内容,系统将显示原始内容。

我猜循环遍历联系人超过了分配给方法完成的时间。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49742192

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档