blocking phone number in call kit

2019-08-19 16:19发布

问题:

I'm trying to using CallKit to add a feature to my app to add some phone numbers to blacklist! the code below is my whole view!!

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
    }
}

to explain the code, I save each number that user will enter in a core data(entityName: BlockedPhoneNumbers). I'm not sure even if this is the right way to save numbers that they need to be blocked or not!!

when the user presses the button I save the number and it works fine( but I'm not sure if this is the right way or not!!). and in getCoreData I get the core data and show them in a table view. which shows that core data works fine! but when user wanna unblock the contact and presses the button in CELL of the table view, I get an error and app crash and it says:

Thread 1: Fatal error: Index out of range

my problems are:

  1. why do I get this error?

    2.as I can not find any tutorial for callKit I believe that I'm doing this job wrong.

could anyone help me with this?

回答1:

You have too many arrays:

  • listPhoneNumbers which contains your integer numbers
  • coreData which contains your Core Data items
  • phoneNumbersArrCoreData which could contain your Core Data items, but you don't add anything to it.

As a result, phoneNumbersArrCoreData is empty. When you try and remove an object from the empty array you get an array bounds exception.

You should eliminate two of the three arrays.

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
    }
}

The code to actually load data into the Call Kit block list needs to go into a CallKit extension in your app. You will need to use an application group to share the Core Data store with the extension.