阻止通话工具包中的电话号码

时间:2018-12-07 04:19:04

标签: ios swift uitableview callkit

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

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 {
  

我在下面收到错误

            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中,我获取核心数据并将其显示在表格视图中。这表明核心数据运行良好!但是当用户想要取消阻止联系人并在表格视图的CELL中按下按钮时,出现错误,应用崩溃,并显示:

  

线程1:致命错误:索引超出范围

我的问题是:

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

    2.as我找不到callKit的任何教程,我认为我做错了这项工作。

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

您的数组太多:

  • 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扩展。您将需要使用应用程序组与扩展名共享Core Data存储。