我对 Swift 编程完全陌生,我不知道如何进行这项工作。 基本上,我有一个 iOS 应用程序,它可以观察来电,如果来电者号码等于用户定义的要拒绝的号码,则自动拒绝它,而不向用户显示任何通知(该应用程序仅供我私人使用)。
目前我有这段代码可以观察来电。它的工作原理就像一个魅力,但我不知道如何让它拒绝来电(目前是所有电话号码)。
import CallKit
class CallTest: NSObject, CXCallObserverDelegate, ObservableObject {
@Published var incomingCallNumber: String?
let callObserver = CXCallObserver()
override init() {
super.init()
callObserver.setDelegate(self, queue: nil)
}
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
if (call.isOutgoing == false && call.hasConnected == false) {
print("Incoming call")
}
}
}
我尝试过这样拒绝来电。
import CallKit
class CallTest: NSObject, CXCallObserverDelegate, ObservableObject {
@Published var incomingCallNumber: String?
let callObserver = CXCallObserver()
override init() {
super.init()
callObserver.setDelegate(self, queue: nil)
}
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
if (call.isOutgoing == false && call.hasConnected == false) {
print("Incoming call")
let callController = CXCallController()
let endCallAction = CXEndCallAction(call: call.uuid)
let transaction = CXTransaction(action: endCallAction)
callController.request(transaction) { error in
if let error = error {
print("Error declining call: \(error)")
} else {
print("Call declined successfully")
}
}
}
}
}
但我不断收到错误:
错误域=com.apple.CallKit.error.requesttransaction代码=2“(空)”
这意味着“unknownCallProvider”,但我不知道如何为此设置提供程序。
最终类CallKitSerivce:NSObject,CXProviderDelegate{
var provider: CXProvider?
let callController = CXCallController()
override init() {
super.init()
let providerConfiguration = CXProviderConfiguration(localizedName: "Calling App")
providerConfiguration.supportsVideo = true
providerConfiguration.maximumCallsPerCallGroup = 1
providerConfiguration.supportedHandleTypes = [.generic]
self.provider = CXProvider(configuration: providerConfiguration)
self.provider?.setDelegate(self, queue: nil)
}
func reportIncomingCall(uuid: UUID, handle: String, hasVideo: Bool = false){
let update = CXCallUpdate()
update.remoteHandle = CXHandle(type: .generic, value: handle)
update.hasVideo = hasVideo
provider?.reportNewIncomingCall(with: uuid, update: update) { error in
if let error = error{
print(error)
}else{
print("incoming call reported successfully")
}
}
}
func startCall(uuid: UUID, handle: String, hasVideo: Bool = false){
let handle = CXHandle(type: .generic, value: handle)
let transaction = CXTransaction(action: CXStartCallAction(call: uuid, handle: handle))
callController.request(transaction) { error in
if let error = error {
print(error)
}else{
print("call started successfully")
}
}
}
}
与您的观察者一起包括提供商,那么我希望错误会消失