ios - 使用调用目录扩展识别调用者 ID

标签 ios swift ios-app-extension caller-id

我在我的应用程序中实现了调用目录扩展来识别被叫 ID。但接到电话时无法查看我的应用程序名称或来电者详细信息。无法调试调用目录扩展,Apple 出于安全原因提起诉讼。下面包含了我的代码。

override func beginRequest (with context: CXCallDirectoryExtensionContext) {
// --- 1
guard let phoneNumbersToBlock = retrievePhoneNumbersToBlock () else {
NSLog ( "Unable to retrieve phone numbers to block" )
let error = NSError (domain: "CallDirectoryHandler" , code: 1 , userInfo: nil )
context.cancelRequest (withError: error)
return
}

// --- 2
for phoneNumber in phoneNumbersToBlock {
    let phoneNumberr : CXCallDirectoryPhoneNumber = CXCallDirectoryPhoneNumber(phoneNumber)!
context.addBlockingEntry (withNextSequentialPhoneNumber: phoneNumberr)
}

// --- 3
guard let (phoneNumbersToIdentify, phoneNumberIdentificationLabels) = retrievePhoneNumbersToIdentifyAndLabels () else {
NSLog ( "Unable to retrieve phone numbers to identify and their labels" )
let error = NSError (domain: "CallDirectoryHandler" , code: 2 , userInfo: nil )
context.cancelRequest (withError: error)
return
}

// --- 4
for (phoneNumber, label) in zip (phoneNumbersToIdentify, phoneNumberIdentificationLabels) {
    let phoneNumberrr : CXCallDirectoryPhoneNumber = CXCallDirectoryPhoneNumber(phoneNumber)!
context.addIdentificationEntry (withNextSequentialPhoneNumber: phoneNumberrr, label: label)
}

// --- 5
context.completeRequest ()
}

private func retrievePhoneNumbersToBlock () -> [ String ]? {
 // retrieve list of phone numbers to block
 return [ "+ 919488775260" ]
 }

 private func retrievePhoneNumbersToIdentifyAndLabels () -> (phoneNumbers: [ String ], labels: [ String ])? {
 // retrieve list of phone numbers to identify, and their labels
 return ([ "+ 919385338505" ], [ "test" ])
 }

是否应该启用任何配置,我可能错过了。请帮助我。

最佳答案

我找到了解决方案。 由于 Apple 的安全原因,基本上不可能调试 Call 目录扩展。但您可以通过应用程序中的重新加载功能进行测试。请找到下面的代码。

CXCallDirectoryManager.sharedInstance.reloadExtension(withIdentifier: "com.harman.CallerIDTestApp.CallerExtension", completionHandler: { (result: Error?)->Void in
        guard let errorChk = result else { return }
        print(errorChk)
        let errorCode = (errorChk as NSError).code
        switch errorCode {
        case CXErrorCodeCallDirectoryManagerError.Code.unknown.rawValue:
            print("Extension could not be load for an unknown reason.")
        case CXErrorCodeCallDirectoryManagerError.Code.noExtensionFound.rawValue:
            print("Could not load extension.  Extension not found")
        case CXErrorCodeCallDirectoryManagerError.Code.loadingInterrupted.rawValue:
            print("Could not load extension.  Extension was interrupted while loading")
        case CXErrorCodeCallDirectoryManagerError.Code.entriesOutOfOrder.rawValue:
            print("Could not load extension.  Call entries are out of order")
        case CXErrorCodeCallDirectoryManagerError.Code.duplicateEntries.rawValue:
            print("Could not load extension.  Duplicate entries")
        case CXErrorCodeCallDirectoryManagerError.Code.maximumEntriesExceeded.rawValue:
            print("Could not load extension.  Maximum entries exceeded")
        case CXErrorCodeCallDirectoryManagerError.Code.extensionDisabled.rawValue:
            print("Extension not enabled in Settings")
        case CXErrorCodeCallDirectoryManagerError.Code.unexpectedIncrementalRemoval.rawValue:
            print("Unexpected incremental removal")
        case CXErrorCodeCallDirectoryManagerError.Code.currentlyLoading.rawValue:
            print("Could not load extension.  The extension is currently loading")
        default:
            print("Could not load extension.")
        }
    })

在应用程序委托(delegate)中使用上述代码来测试您的通话目录扩展是否正常工作。

在“CallDirectoryHandler”中,beginRequest 方法应如下所示。

override func beginRequest(with context: CXCallDirectoryExtensionContext) {
    context.delegate = self
    if context.isIncremental {
        addOrRemoveIncrementalBlockingPhoneNumbers(to: context)

        addOrRemoveIncrementalIdentificationPhoneNumbers(to: context)
    } else {
        addAllBlockingPhoneNumbers(to: context)

        addAllIdentificationPhoneNumbers(to: context)
    }

    context.completeRequest()
}

请检查上下文是否为isIncremental,否则代码将无法运行。

关于ios - 使用调用目录扩展识别调用者 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59541326/

相关文章:

arrays - 如果 NSDecimalNumber 大于 Uint64,如何将 NSDecimalNumber 转换为字节数组?

ios - 执行点击关闭后将结果传递给 UITableViewCell 内的 Button?

ios - 如何创建新的控制中心命令?

ios - iOS 13通知服务扩展程序似乎忽略了应用程序特定的首选语言。是 bug 吗?

ios - 导航栏中的 pageControl 未居中

ios - 乘法没有给出正确答案

ios - 如果数据源计数可能发生变化,是否可以同时使用 reloadItemsAtIndexPaths 和 reloadData?

ios - 在我的一个 iOS 应用程序 (Swift) 中需要一个时间对象

ios 8 键盘扩展设置包

ios - 使用 stripe 实现 APPLE PAY