From 0f086498e489e9dcc2f81b2e0c483deaaa6be141 Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Wed, 12 Aug 2020 16:43:26 +0200 Subject: [PATCH] =?UTF-8?q?Move=20CallExampleContext=20ownership=20to=20th?= =?UTF-8?q?e=20AppDelegate.=20When=20receiving=20a=20PushIncoming=20call,?= =?UTF-8?q?=20register=20(else=20call=20won=E2=80=99t=20work)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CallKitTutorial/AppDelegate.swift | 3 ++- .../CallKitTutorial/CallExample.swift | 15 +++++++++++++-- .../CallKitTutorial/ContentView.swift | 16 +++++++++++++--- .../CallKitTutorial/SceneDelegate.swift | 6 ++++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/swift/CallKitTutorial/CallKitTutorial/AppDelegate.swift b/swift/CallKitTutorial/CallKitTutorial/AppDelegate.swift index bfb6f6e..a6e7898 100644 --- a/swift/CallKitTutorial/CallKitTutorial/AppDelegate.swift +++ b/swift/CallKitTutorial/CallKitTutorial/AppDelegate.swift @@ -7,11 +7,12 @@ // import UIKit +import SwiftUI @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { - + @ObservedObject var tutorialContext = CallExampleContext() func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Override point for customization after application launch. diff --git a/swift/CallKitTutorial/CallKitTutorial/CallExample.swift b/swift/CallKitTutorial/CallKitTutorial/CallExample.swift index 8b94331..16cb7b3 100644 --- a/swift/CallKitTutorial/CallKitTutorial/CallExample.swift +++ b/swift/CallKitTutorial/CallKitTutorial/CallExample.swift @@ -43,6 +43,7 @@ class CallExampleContext : ObservableObject let outgoingCallName = "Outgoing call example" let incomingCallName = "Incoming call example" + init() { mProviderDelegate = CallKitProviderDelegate(context : self) @@ -60,13 +61,12 @@ class CallExampleContext : ObservableObject // Initialize Linphone Core - try? mCore = factory.createCore(configPath: "MyConfig", factoryConfigPath: "", systemContext: nil) + try? mCore = factory.createCore(configPath: "", factoryConfigPath: "", systemContext: nil) // main loop for receiving notifications and doing background linphonecore work: mCore.autoIterateEnabled = true mCore.callkitEnabled = true mCore.pushNotificationEnabled = true - let pushConfig = mCore.pushNotificationConfig! pushConfig.provider = "apns.dev" @@ -106,6 +106,12 @@ class CallExampleContext : ObservableObject } } + func clearRegistrations() + { + mCore.clearProxyConfig() + loggedIn = false + } + func createCallParams() throws -> CallParams { @@ -213,6 +219,11 @@ class CallStateDelegate: CoreDelegate { if (cstate == .PushIncomingReceived) { + if (!tutorialContext.loggedIn) + { + // Cannot properly answer call if not registered. If the app was launched from a push notification, register. + tutorialContext.registrationExample() + } // We're being called by someone (and app is in background) initIncomingCall() } diff --git a/swift/CallKitTutorial/CallKitTutorial/ContentView.swift b/swift/CallKitTutorial/CallKitTutorial/ContentView.swift index d19cfe0..4ea0eba 100644 --- a/swift/CallKitTutorial/CallKitTutorial/ContentView.swift +++ b/swift/CallKitTutorial/CallKitTutorial/ContentView.swift @@ -10,7 +10,7 @@ import SwiftUI struct ContentView: View { - @ObservedObject var tutorialContext = CallExampleContext() + @ObservedObject var tutorialContext : CallExampleContext func getCallButtonText() -> String { @@ -62,11 +62,21 @@ struct ContentView: View { .frame(width: 90.0, height: 42.0) .background(Color.gray) } - Text("Login State : ") + Spacer() + Text("Login State :") .font(.footnote) Text(tutorialContext.loggedIn ? "Logged in" : "Unregistered") .font(.footnote) .foregroundColor(tutorialContext.loggedIn ? Color.green : Color.black) + Spacer() + Button(action: tutorialContext.clearRegistrations) + { + Text("Clear") + .font(.largeTitle) + .foregroundColor(Color.white) + .frame(width: 90.0, height: 42.0) + .background(Color.gray) + } } } VStack(spacing: 0.0) { @@ -150,6 +160,6 @@ struct ContentView: View { struct ContentView_Previews: PreviewProvider { static var previews: some View { - ContentView() + ContentView(tutorialContext: CallExampleContext()) } } diff --git a/swift/CallKitTutorial/CallKitTutorial/SceneDelegate.swift b/swift/CallKitTutorial/CallKitTutorial/SceneDelegate.swift index c34a71f..cc62bec 100644 --- a/swift/CallKitTutorial/CallKitTutorial/SceneDelegate.swift +++ b/swift/CallKitTutorial/CallKitTutorial/SceneDelegate.swift @@ -20,8 +20,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). // Create the SwiftUI view that provides the window contents. - let contentView = ContentView() - + let delegate = UIApplication.shared.delegate as! AppDelegate + + let contentView = ContentView(tutorialContext: delegate.tutorialContext) + // Use a UIHostingController as window root view controller. if let windowScene = scene as? UIWindowScene { let window = UIWindow(windowScene: windowScene)