在通话工具包中屏蔽 phone 个号码

blocking phone number in call kit

我正在尝试使用 CallKit 向我的应用添加一项功能,以将一些 phone 号码添加到黑名单! 下面的代码是我的全部观点!!

class BlacklistViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate {


    var phoneNumbersArrCoreData = [BlockedPhoneNumbers]()
    var listPhoneNumbers:[CXCallDirectoryPhoneNumber] = []
    @IBOutlet weak var TableView: UITableView!
    @IBOutlet weak var BtnAddO: UIButton!
    @IBOutlet weak var EntPhonenumber: UITextField!

    @IBAction func BtnAddA(_ sender: Any) {
        if !(EntPhonenumber.text?.isEmpty)!
        {
            let blackedPhonenumbers_CoreData = BlockedPhoneNumbers(context: PersistanceService.context)
            blackedPhonenumbers_CoreData.phoneNumber = Int64.init(EntPhonenumber.text!)!
            PersistanceService.saveContext()
            getCoreData()
            TableView.reloadData()
        }
    }

    var coreData = [BlockedPhoneNumbers]()

    func getCoreData()
    {
        listPhoneNumbers.removeAll()
        let fetchRequest : NSFetchRequest<BlockedPhoneNumbers> = BlockedPhoneNumbers.fetchRequest()
        do
        {
            let FetchedResultFromDB = try  PersistanceService.context.fetch(fetchRequest)
            coreData = FetchedResultFromDB
            print("============\n===========\n")

            if coreData.count > 0
            {
                for i in 0..<coreData.count
                {
                    listPhoneNumbers.append(coreData[i].phoneNumber)
                }
            }
            print("============\n===========\n")

        }
        catch{
            print("gettin blocked number from db got error")
        }
    }


    override func viewDidLoad() {

        BtnAddO.layer.cornerRadius = 5
        BtnAddO.layer.borderColor = UIColor.white.cgColor
        BtnAddO.layer.borderWidth = 0.8
        EntPhonenumber.attributedPlaceholder = NSAttributedString(string: "Enter a phone number to block",attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightText])
        getCoreData()

        super.viewDidLoad()
        view.backgroundColor = UIColor.init(red: 25/255, green: 28/255, blue: 46/255, alpha: 1)
        TableView.delegate = self
        TableView.dataSource = self
    }
    func beginRequest(with context: CXCallDirectoryExtensionContext) {
        getCoreData()
        let blockedPhoneNumbers: [CXCallDirectoryPhoneNumber] = listPhoneNumbers
        for phoneNumber in blockedPhoneNumbers.sorted(by: <) {
            context.addBlockingEntry(withNextSequentialPhoneNumber: phoneNumber)
        }

        context.completeRequest()
    }


    //MARK: - TableView
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listPhoneNumbers.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BlackListCell") as? BlackListTableViewCell
        cell?.ContactImg.layer.masksToBounds = true
        cell?.mainView.layer.cornerRadius = 10
        cell?.mainView.backgroundColor = UIColor(red: 42/255, green: 48/255, blue: 66/255, alpha: 1)
        cell?.ContactImg.layer.cornerRadius = 5
        cell?.ContactImg.image = UIImage(named: "Blocked")
        cell?.unBlock.imageView?.image = nil
        cell?.unBlock.setTitle("UNBLOCK", for: UIControl.State.normal)
        cell?.unBlock.layer.cornerRadius = (cell?.unBlock.frame.size.height)!/2
        cell?.SetUnblockBtn {

I get the error here,below

            let context:NSManagedObjectContext = PersistanceService.context

            context.delete(self.phoneNumbersArrCoreData[indexPath.row] as NSManagedObject)
            self.phoneNumbersArrCoreData.remove(at: indexPath.row)
            print("data deleted!!!")
               }
        cell?.phoneNumber.text = String(listPhoneNumbers[indexPath.row])



        return cell!
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 85
    }
}

为了解释代码,我将用户将输入的每个号码保存在核心数据(实体名称:BlockedPhoneNumbers)中。我不确定即使这是保存号码的正确方法,他们是否需要被阻止!!

当用户按下按钮时,我保存了数字并且它工作正常(但我不确定这是否正确!!)。 在 getCoreData 中,我获取核心数据并将它们显示在 table 视图中。这表明核心数据工作正常!但是当用户想要取消阻止联系人并按下 table 视图的 CELL 中的按钮时,我收到错误消息并且应用程序崩溃并显示:

Thread 1: Fatal error: Index out of range

我的问题是:

  1. 为什么会出现此错误?

    2.as 我找不到 callKit 的任何教程,我相信我做错了。

谁能帮我解决这个问题?

你的数组太多了:

  • listPhoneNumbers 其中包含您的整数
  • coreData 其中包含您的核心数据项
  • phoneNumbersArrCoreData 其中 可能 包含您的核心数据项,但您不向其中添加任何内容。

因此,phoneNumbersArrCoreData 为空。当您尝试从空数组中删除一个对象时,您会得到一个数组边界异常。

你应该消除三个数组中的两个。

class BlacklistViewController: UIViewController ,UITableViewDataSource, UITableViewDelegate {


    var blockedNumbers = [BlockedPhoneNumbers]()
    @IBOutlet weak var TableView: UITableView!
    @IBOutlet weak var BtnAddO: UIButton!
    @IBOutlet weak var EntPhonenumber: UITextField!

    @IBAction func BtnAddA(_ sender: Any) {
        if !(EntPhonenumber.text?.isEmpty)!
        {
            let blackedPhonenumbers_CoreData = BlockedPhoneNumbers(context: PersistanceService.context)
            blackedPhonenumbers_CoreData.phoneNumber = Int64.init(EntPhonenumber.text!)!
            PersistanceService.saveContext()
            getCoreData()
            TableView.reloadData()
        }
    }

    func getCoreData()
    {
        let fetchRequest : NSFetchRequest<BlockedPhoneNumbers> = BlockedPhoneNumbers.fetchRequest()
        do
        {
            let FetchedResultFromDB = try  PersistanceService.context.fetch(fetchRequest)
            blockedNumbers = FetchedResultFromDB
            print("============\n===========\n")
        }
        catch{
            print("gettin blocked number from db got error")
        }
    }


    override func viewDidLoad() {

        BtnAddO.layer.cornerRadius = 5
        BtnAddO.layer.borderColor = UIColor.white.cgColor
        BtnAddO.layer.borderWidth = 0.8
        EntPhonenumber.attributedPlaceholder = NSAttributedString(string: "Enter a phone number to block",attributes: [NSAttributedString.Key.foregroundColor: UIColor.lightText])
        getCoreData()

        super.viewDidLoad()
        view.backgroundColor = UIColor.init(red: 25/255, green: 28/255, blue: 46/255, alpha: 1)
        TableView.delegate = self
        TableView.dataSource = self
    }

    //MARK: - TableView
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return blockedNumbers.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BlackListCell") as? BlackListTableViewCell
        cell?.ContactImg.layer.masksToBounds = true
        cell?.mainView.layer.cornerRadius = 10
        cell?.mainView.backgroundColor = UIColor(red: 42/255, green: 48/255, blue: 66/255, alpha: 1)
        cell?.ContactImg.layer.cornerRadius = 5
        cell?.ContactImg.image = UIImage(named: "Blocked")
        cell?.unBlock.imageView?.image = nil
        cell?.unBlock.setTitle("UNBLOCK", for: UIControl.State.normal)
        cell?.unBlock.layer.cornerRadius = (cell?.unBlock.frame.size.height)!/2
        cell?.SetUnblockBtn {
            let context:NSManagedObjectContext = PersistanceService.context

            context.delete(self.blockedNumbers[indexPath.row] as NSManagedObject)
            self.phoneNumbersArrCoreData.remove(at: indexPath.row)
            print("data deleted!!!")
               }
        cell?.phoneNumber.text = blockedNumbers[indexPath.row].phoneNumber

        return cell!
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 85
    }
}

实际将数据加载到 Call Kit 阻止列表中的代码需要进入您应用中的 CallKit 扩展。您将需要使用应用程序组与扩展共享核心数据存储。