import UIKit import CallKit import PushKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var provider: CXProvider? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. let registry = PKPushRegistry(queue: nil) registry.delegate = self registry.desiredPushTypes = [PKPushType.voIP] let config = CXProviderConfiguration(localizedName: "My App") config.iconTemplateImageData = UIImagePNGRepresentation(UIImage(named: "pizza")!) config.ringtoneSound = "ringtone.caf" config.includesCallsInRecents = false; config.supportsVideo = true; provider = CXProvider(configuration: config) provider?.setDelegate(self, queue: nil) return true } func applicationWillResignActive(_ application: UIApplication) { // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. } func applicationDidEnterBackground(_ application: UIApplication) { // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. } func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. } func applicationDidBecomeActive(_ application: UIApplication) { // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. } func applicationWillTerminate(_ application: UIApplication) { // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. } } extension AppDelegate: CXProviderDelegate, PKPushRegistryDelegate { func providerDidReset(_ provider: CXProvider) { } func provider(_ provider: CXProvider, perform action: CXAnswerCallAction) { action.fulfill() joinCall() } func provider(_ provider: CXProvider, perform action: CXEndCallAction) { print("End Call") action.fulfill() logCallRejected() } func pushRegistry(_ registry: PKPushRegistry, didUpdate pushCredentials: PKPushCredentials, for type: PKPushType) { print(pushCredentials.token.map { String(format: "%02.2hhx", $0) }.joined()) } func pushRegistry(_ registry: PKPushRegistry, didReceiveIncomingPushWith payload: PKPushPayload, for type: PKPushType, completion: @escaping () -> Void) { func reportFakeCall(completion: @escaping () -> Void) { let callUpdate = CXCallUpdate() let vCallId = UUID() if let provider = provider { print("Fake call reported!") provider.reportNewIncomingCall( with: vCallId, update: callUpdate, completion: { error in completion() CXEndCallAction(call: vCallId) // CXEndCallAction transaction }) } else { print("ERROR REPORTING FAKE CALL") } } if type == .voIP { if let handle = "Test Caller" as? String { let callUpdate = CXCallUpdate() callUpdate.remoteHandle = CXHandle(type: .phoneNumber, value: handle) let callUUID = UUID() provider?.reportNewIncomingCall(with: callUUID, update: callUpdate) { _ in completion() } print("Connection Established") return } else { reportFakeCall(completion: completion) print("NO HANDLE") return } } else { print("Type:", type.rawValue) reportFakeCall(completion: completion) return } } func joinCall () { } func logCallRejected () { } func rejectCall(call: UUID, _ provider: CXProvider) { let endCallAction = CXEndCallAction(call: call) let transaction = CXTransaction(action: endCallAction) let cxCallController = CXCallController() cxCallController.request(transaction) { error in if let error = error { print("EndCallAction transaction request failed: \(error.localizedDescription).") provider.reportCall(with: call, endedAt: Date(), reason: .remoteEnded) return } print("EndCallAction transaction request successful") } } func callUser ( userCallID: String ) { } }