如何使用Callkit Swift阻止电话号码的动态数组

h22fl7wq  于 2023-04-19  发布在  Swift
关注(0)|答案(1)|浏览(113)

我正在开发一个应用程序,用户可以阻止电话号码。最初,我已经尝试了静态电话号码数组的工作,但当我试图阻止我从TextField添加到数组中的号码时,它在这种情况下不会阻塞。
我使用的代码CallDirectory扩展

class CallDirectoryHandler: CXCallDirectoryProvider, CXCallDirectoryExtensionContextDelegate {
    func requestFailed(for extensionContext: CXCallDirectoryExtensionContext, withError error: Error) {
        print("Errorrrrrrrrr",error)
    }
    override func beginRequest(with context: CXCallDirectoryExtensionContext) {
        context.delegate = self
//        let numList = NumberList()
        var numList = NumberList()
        for num in numList.array{
            let pNum :  CXCallDirectoryPhoneNumber = CXCallDirectoryPhoneNumber(Int64(num)!)
            context.addBlockingEntry(withNextSequentialPhoneNumber: pNum)

        }
        context.completeRequest()
    }
}

以及电话号码数组

class NumberList : ObservableObject{
    @Published var array:[String] = []
    
    //more code will goes there
}

视图在这段代码中,我使用了两个按钮,一个用于将TextFeild中的数字添加到数组中,另一个用于添加到块中

struct AddNumber: View {
    @StateObject var numberList = NumberList()
    @State var number = ""
    var body: some View {
        VStack{
            HStack{
                TextField("Enter Number", text: $number)
                    .padding()
            }
            .frame(width: Constants.width*0.7, height: Constants.height*0.07)
            .background(Color("Button"))
            .cornerRadius(30)
            Button {
                numberList.array.append(number)
            } label: {
                Text("Save")
                    .foregroundColor(.white)
            }
            Button{
                CXCallDirectoryManager.sharedInstance.reloadExtension(withIdentifier: "TheAppMedia.com.BlockContacts.CallBlocker") { (error) in
                    print("reloaded extension: \(String(describing: error))")
                }
            } label: {
                Text("Block")
                    .font(.custom("Montserrat-Regular", size: 24))
                    .frame(width: Constants.width*0.6, height: Constants.height*0.06)
                    .background(Color.red)
                    .foregroundColor(.white)
                    .cornerRadius(30)
            }
        }
    }
}

更新保存按钮

Button {
                        if var data = UserDefaults.standard.array(forKey: "phoneNumber") as? [String]{
                            data.append(number)
                            UserDefaults.standard.set(data,forKey: "phoneNumber")
                        }else{
                            let ar = [number]
                            UserDefaults.standard.set(ar,forKey: "phoneNumber")
                        }
                    } label: {
                        Text("Save")
                            .foregroundColor(.white)
                    }

和CallDirectoryHandler.swift文件

class CallDirectoryHandler: CXCallDirectoryProvider, CXCallDirectoryExtensionContextDelegate {
    func requestFailed(for extensionContext: CXCallDirectoryExtensionContext, withError error: Error) {
        print("Errorrrrrrrrr",error)
    }
    override func beginRequest(with context: CXCallDirectoryExtensionContext) {
        context.delegate = self
        let data = UserDefaults.standard.array(forKey: "phoneNumber") as? [String]
        print(data)
        for num in data ?? []{
            let pNum :  CXCallDirectoryPhoneNumber = CXCallDirectoryPhoneNumber(Int64(num)!)
            context.addBlockingEntry(withNextSequentialPhoneNumber: pNum)

        }
        context.completeRequest()
    }
}
qv7cva1a

qv7cva1a1#

我重新创建了你的代码和UserDefaults工作。你的问题将在你的CallDirectoryHandler中。添加断点和打印,单步执行每一行。
这是可行的:

class NumberList : ObservableObject{
    @Published var array:[String] = [] {
        didSet {
            print(array)
        }
    }
}

struct ContentView: View {
    @StateObject var numberList = NumberList()
    @State var number = ""
    var body: some View {
        VStack{
            HStack{
                TextField("Enter Number", text: $number)
                    .padding()
            }
            .frame(width: 200, height: 200)
            .background(Color("Button"))
            .cornerRadius(30)
            Button {
                if var data = UserDefaults.standard.array(forKey: "phoneNumber") as? [String]{
                                            data.append(number)
                                            UserDefaults.standard.set(data,forKey: "phoneNumber")
                                        }else{
                                            let ar = [number]
                                            UserDefaults.standard.set(ar,forKey: "phoneNumber")
                                        }
            } label: {
                Text("Save")
                    .foregroundColor(.white)
            }
            .background(Color.red)
            Button{
               beginRequestTest()
            } label: {
                Text("Block")
                    .font(.custom("Montserrat-Regular", size: 24))
                    .frame(width: 200, height: 200)
                    .background(Color.red)
                    .foregroundColor(.white)
                    .cornerRadius(30)
            }
        }
    }

    private func beginRequestTest() {
        let savedNumbers = UserDefaults.standard.array(forKey: "phoneNumber") as? [String]
        print(savedNumbers)
    }
}

相关问题