使用呼叫目录分机识别呼叫者ID

问题描述 投票:0回答:1

我已经在我的应用中实现了呼叫目录扩展,以识别被叫ID。但是接到电话后,无法查看我的应用名称或呼叫者详细信息。由于安全原因,Apple不能调试Call Directory Extension。在下面包含了我的代码。

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" ])
 }

应该启用任何配置,我可能会漏掉。请帮助我。

ios swift ios-app-extension caller-id
1个回答
0
投票

我找到了解决方案。由于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.")
        }
    })

在应用程序委托中使用上面的代码来测试您的呼叫目录扩展是否正常。

在“ 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,否则代码将不起作用。

© www.soinside.com 2019 - 2024. All rights reserved.