我应该在IOS项目中的哪里放置Call​​kit Reload Extension代码

时间:2019-02-25 06:21:09

标签: ios xcode callkit

我正在制作类似TrueCaller的应用程序。我可以先屏蔽某个号码,但是每当我要更新/添加更多号码时,都需要在 Settings-> Phone-> Call Blocking&Identification 中手动禁用并启用呼叫目录插件以获取列表更新。我在StackOverflow上看到了很多答案,人们说要通过代码调用 CXCallDirectoryManager.reloadExtension 来重新加载扩展。但是我不知道在哪里写这段代码,或者在我的项目中应该从哪里调用此reloadExtension。

更新-1 这是我的处理方法。 下面是我的视图控制器的代码。

@IBAction func add(_ sender: Any) {
    //let defaults = UserDefaults.standard
    let userDefaults = UserDefaults(suiteName: "group.com.number1")
    var finArray = userDefaults!.object(forKey: "attribute") as? [Int64] ?? [Int64]()
    let number1 = number.text!
    finArray.append(Int64(number1)!)
    print("from main add")
    print(finArray)

    //let userDefaults = UserDefaults(suiteName: "group.com.number1")
    userDefaults!.set(finArray, forKey: "attribute")
    CXCallDirectoryManager.sharedInstance.reloadExtension(withIdentifier: "com.akshat.Blocking-array.Callblockarray", completionHandler: {(error) -> Void in if let error = error {
        print("akshat"+error.localizedDescription)
        }})

}

这是我的呼叫工具扩展

//  CallDirectoryHandler.swift
import Foundation

import CallKit
import CoreData
class CallDirectoryHandler: CXCallDirectoryProvider {

    override func beginRequest(with context: CXCallDirectoryExtensionContext) {
        context.delegate = self
        print("inside beginrequest")

//        let defaults = UserDefaults(suiteName: "group.com.number1")
//        //(suiteName: "group.tag.number")
//        let array = defaults!.object(forKey: "attribute") as? [Int64] ?? [Int64]()
//        print(array)
        // Check whether this is an "incremental" data request. If so, only provide the set of phone number blocking
        // and identification entries which have been added or removed since the last time this extension's data was loaded.
        // But the extension must still be prepared to provide the full set of data at any time, so add all blocking
        // and identification phone numbers if the request is not incremental.
        if context.isIncremental {
            print("insideif")
            addOrRemoveIncrementalBlockingPhoneNumbers(to: context)
            print("insideif")
            addOrRemoveIncrementalIdentificationPhoneNumbers(to: context)
        } else {
            addAllBlockingPhoneNumbers(to: context)
            print("inside else")
            addAllIdentificationPhoneNumbers(to: context)
        }

        context.completeRequest()
    }

    private func addAllBlockingPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
        // Retrieve all phone numbers to block from data store. For optimal performance and memory usage when there are many phone numbers,
        // consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
        //
        // Numbers must be provided in numerically ascending order.
        print("func addAllBlockingPhoneNumbers")
        let defaults = UserDefaults(suiteName: "group.com.number1")
        //(suiteName: "group.tag.number")
        var array = defaults!.object(forKey: "attribute") as? [Int64] ?? [Int64]()
        array.sort()
        print(array)
        let allPhoneNumbers: [CXCallDirectoryPhoneNumber] = array
        for phoneNumber in allPhoneNumbers {
            context.addBlockingEntry(withNextSequentialPhoneNumber: phoneNumber)
        }
    }

    private func addOrRemoveIncrementalBlockingPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
        // Retrieve any changes to the set of phone numbers to block from data store. For optimal performance and memory usage when there are many phone numbers,
        // consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
        print("addOrRemoveIncrementalBlockingPhoneNumbers")
        let defaults = UserDefaults(suiteName: "group.com.number1")
        //(suiteName: "group.tag.number")
        var array = defaults!.object(forKey: "attribute") as? [Int64] ?? [Int64]()
        array.sort()
        print(array)
        let phoneNumbersToAdd: [CXCallDirectoryPhoneNumber] = array
        for phoneNumber in phoneNumbersToAdd {
            print(phoneNumber)
            context.addBlockingEntry(withNextSequentialPhoneNumber: phoneNumber)
        }

        let phoneNumbersToRemove: [CXCallDirectoryPhoneNumber] = [ 1_877_555_5555 ]
        for phoneNumber in phoneNumbersToRemove {
            context.removeBlockingEntry(withPhoneNumber: phoneNumber)
        }

        // Record the most-recently loaded set of blocking entries in data store for the next incremental load...
    }

    private func addAllIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
        // Retrieve phone numbers to identify and their identification labels from data store. For optimal performance and memory usage when there are many phone numbers,
        // consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
        //
        // Numbers must be provided in numerically ascending order.
        print("addAllIdentificationPhoneNumbers")
        let allPhoneNumbers: [CXCallDirectoryPhoneNumber] = [ 1_877_555_5555, 1_888_555_5555 ]
        let labels = [ "Telemarketer", "Local business" ]

        for (phoneNumber, label) in zip(allPhoneNumbers, labels) {
            context.addIdentificationEntry(withNextSequentialPhoneNumber: phoneNumber, label: label)
        }
    }

    private func addOrRemoveIncrementalIdentificationPhoneNumbers(to context: CXCallDirectoryExtensionContext) {
        print(addOrRemoveIncrementalIdentificationPhoneNumbers)
        // Retrieve any changes to the set of phone numbers to identify (and their identification labels) from data store. For optimal performance and memory usage when there are many phone numbers,
        // consider only loading a subset of numbers at a given time and using autorelease pool(s) to release objects allocated during each batch of numbers which are loaded.
        let phoneNumbersToAdd: [CXCallDirectoryPhoneNumber] = [ 1_408_555_5678 ]
        let labelsToAdd = [ "New local business" ]

        for (phoneNumber, label) in zip(phoneNumbersToAdd, labelsToAdd) {
            context.addIdentificationEntry(withNextSequentialPhoneNumber: phoneNumber, label: label)
        }

        let phoneNumbersToRemove: [CXCallDirectoryPhoneNumber] = [ 1_888_555_5555 ]

        for phoneNumber in phoneNumbersToRemove {
            context.removeIdentificationEntry(withPhoneNumber: phoneNumber)
        }

        // Record the most-recently loaded set of identification entries in data store for the next incremental load...
    }

}

extension CallDirectoryHandler: CXCallDirectoryExtensionContextDelegate {
    func requestFailed(for extensionContext: CXCallDirectoryExtensionContext, withError error: Error) {
        print("CXCallDirectoryExtensionContext")
        // An error occurred while adding blocking or identification entries, check the NSError for details.
        // For Call Directory error codes, see the CXErrorCodeCallDirectoryManagerError enum in <CallKit/CXError.h>.
        //
        // This may be used to store the error details in a location accessible by the extension's containing app, so that the
        // app may be notified about errors which occured while loading data even if the request to load data was initiated by
        // the user in Settings instead of via the app itself.
    }

}

但仍然,这仅在某些时候起作用。每当我添加新号码时,呼叫目录分机花费的时间太长。例如:如果我写了数字“ A”并按添加按钮,则此添加功能将被调用,数字“ A”将被阻止。现在,如果我再加上一个号码,请说“ B”,那么电话目录扩展将无法添加它,并且B也不会被阻止。现在,如果再加上一个数字“ C”,那么所有内容都会被屏蔽。我真的不明白这一点。我希望有人可以帮助我。

0 个答案:

没有答案