I do have a similar problem as protocol extension, does not conform to protocol, but the solution there does not work for me and I can not figure out why.
I want to create a protocol with an extension to handle the login behaviour of my app.
First of all I am not sure if I should put the protocol with its extension in a separate file or just in the AppDelegate above the class declaration. Both ways do not work. The AppDelegate knows the protocol, but it does not know the extension, so it wants me to implement handleLogin()
in the class.
Protocol
protocol LoginFlowHandler {
func handleLogin()
}
Protocol Extension
public extension LoginFlowHandler {
func handleLogin(withWindow window: UIWindow?) {
// Just for testing purpose
let userIsLoggedIn = false
if userIsLoggedIn == true {
self.showMainApp(withWindow: window)
} else {
self.showLogin(withWindow: window)
}
}
func showLogin(withWindow window: UIWindow?) {
window?.subviews.forEach { $0.removeFromSuperview() }
window?.rootViewController = nil
window?.rootViewController = UIStoryboard(name: "Login", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
}
func showMainApp(withWindow window: UIWindow?) {
window?.rootViewController = nil
window?.rootViewController = UIStoryboard(name: "Main", bundle: nil).instantiateInitialViewController()
window?.makeKeyAndVisible()
}
}
AppDelegate.swift
class AppDelegate: UIResponder, UIApplicationDelegate, LoginFlowHandler {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
window = UIWindow.init(frame: UIScreen.main.bounds)
handleLogin(withWindow: window)
return true
}
}
Thank you in advance!