From b0b6f29e994d2019f51bf396cb8dddf0be54311b Mon Sep 17 00:00:00 2001 From: QuentinArguillere Date: Fri, 7 Aug 2020 10:10:51 +0200 Subject: [PATCH] Added example of how to log out, and log back in, in login tutorial --- .../LoginTutorial/ContentView.swift | 26 ++++--- .../LoginTutorial/LoginExample.swift | 71 +++++++++++++------ 2 files changed, 68 insertions(+), 29 deletions(-) diff --git a/swift/LoginTutorial/LoginTutorial/ContentView.swift b/swift/LoginTutorial/LoginTutorial/ContentView.swift index a55ebec..1b76af9 100644 --- a/swift/LoginTutorial/LoginTutorial/ContentView.swift +++ b/swift/LoginTutorial/LoginTutorial/ContentView.swift @@ -29,13 +29,23 @@ struct ContentView: View { .textFieldStyle(RoundedBorderTextFieldStyle()) } VStack { - Button(action: tutorialContext.registrationExample) - { - Text("Login") - .font(.largeTitle) - .foregroundColor(Color.white) - .frame(width: 100.0, height: 42.0) - .background(Color.gray) + HStack { + Button(action: { + if (self.tutorialContext.loggedIn) + { + self.tutorialContext.logoutExample() + } else { + self.tutorialContext.registrationExample() + } + }) + { + Text(tutorialContext.loggedIn ? "Log out" : "Log in") + .font(.largeTitle) + .foregroundColor(Color.white) + .frame(width: 125.0, height: 42.0) + .background(Color.gray) + } + } HStack { Text("Login State : ") @@ -43,7 +53,7 @@ struct ContentView: View { Text(tutorialContext.loggedIn ? "Looged in" : "Unregistered") .font(.footnote) .foregroundColor(tutorialContext.loggedIn ? Color.green : Color.black) - } + }.padding(.top, 10.0) } } Group { diff --git a/swift/LoginTutorial/LoginTutorial/LoginExample.swift b/swift/LoginTutorial/LoginTutorial/LoginExample.swift index c9ad37e..eae4516 100644 --- a/swift/LoginTutorial/LoginTutorial/LoginExample.swift +++ b/swift/LoginTutorial/LoginTutorial/LoginExample.swift @@ -19,7 +19,7 @@ class LoginTutorialContext : ObservableObject @Published var logsEnabled : Bool = true /*------------ Login tutorial related variables -------*/ - var proxy_cfg: ProxyConfig! + var proxy_cfg: ProxyConfig? let mRegistrationDelegate = LinphoneRegistrationDelegate() @Published var id : String = "sip:peche5@sip.linphone.org" @Published var passwd : String = "peche5" @@ -43,35 +43,60 @@ class LoginTutorialContext : ObservableObject // main loop for receiving notifications and doing background linphonecore work: mCore.autoIterateEnabled = true try? mCore.start() - + + mCore.addDelegate(delegate: mRegistrationDelegate) // Add registration specific logs } func registrationExample() { - mCore.addDelegate(delegate: mRegistrationDelegate) // Add registration specific logs - - let factory = Factory.Instance - do { - let proxy_cfg = try mCore.createProxyConfig() - let address = try factory.createAddress(addr: id) - let info = try factory.createAuthInfo(username: address.username, userid: "", passwd: passwd, ha1: "", realm: "", domain: address.domain) - mCore.addAuthInfo(info: info) + if (!loggedIn) + { - try proxy_cfg.setIdentityaddress(newValue: address) - let server_addr = "sip:" + address.domain + ";transport=tls" - try proxy_cfg.setServeraddr(newValue: server_addr) - proxy_cfg.registerEnabled = true - try mCore.addProxyConfig(config: proxy_cfg) - if ( mCore.defaultProxyConfig == nil) - { - // IMPORTANT : default proxy config setting MUST be done AFTER adding the config to the core ! - mCore.defaultProxyConfig = proxy_cfg + do { + + if (proxy_cfg == nil) { + let factory = Factory.Instance + proxy_cfg = try mCore.createProxyConfig() + let address = try factory.createAddress(addr: id) + let info = try factory.createAuthInfo(username: address.username, userid: "", passwd: passwd, ha1: "", realm: "", domain: address.domain) + mCore.addAuthInfo(info: info) + + try proxy_cfg!.setIdentityaddress(newValue: address) + let server_addr = "sip:" + address.domain + ";transport=tls" + try proxy_cfg!.setServeraddr(newValue: server_addr) + proxy_cfg!.registerEnabled = true + try mCore.addProxyConfig(config: proxy_cfg!) + if ( mCore.defaultProxyConfig == nil) + { + // IMPORTANT : default proxy config setting MUST be done AFTER adding the config to the core ! + mCore.defaultProxyConfig = proxy_cfg + } + } + else { + proxy_cfg!.edit() /*start editing proxy configuration*/ + proxy_cfg!.registerEnabled = true /*de-activate registration for this proxy config*/ + try proxy_cfg!.done() + } + } catch { + print(error) } - } catch { - print(error) } } + + func logoutExample() + { + if (loggedIn) { + proxy_cfg!.edit() /*start editing proxy configuration*/ + proxy_cfg!.registerEnabled = false /*de-activate registration for this proxy config*/ + do { + try proxy_cfg!.done() + } catch { + print(error) + } + } + } + } @@ -97,5 +122,9 @@ class LinphoneRegistrationDelegate: CoreDelegate { { tutorialContext.loggedIn = true } + else if (cstate == .Cleared) + { + tutorialContext.loggedIn = false + } } }