简体   繁体   中英

Pass Data From AppDelegate into UIViewController

How to pass data from AppDelegate Class into Custom ViewController?

import UIKit
import LocalAuthentication

class ViewController: UIViewController {
    public var config: PasscodeConfig!

    func GetAppDelegate(){
        print("\(self.config)") /// nil %(
    }

    override public func viewDidLoad() {
        super.viewDidLoad()

        self.GetAppDelegate()
}
...
}

unfortunately the variable config returns - nil AppDelegate:

   @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var appCoordinator: AppCoordinator!

    var code = ""
lazy var passcode: Passcode = {
    let config = PasscodeConfig(passcodeGetter: {
        return self.code
        // Return code as string
    }, passcodeSetter: { code in
        self.code = code
         // Save new code
    }, biometricsGetter: {
        return true
        // return Should use biometrics (Touch ID or Face ID) as Bool
    })

    let passcode = Passcode(window: self.window, config: config)

    return passcode
}()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {

        window = UIWindow(frame: UIScreen.main.bounds)
        appCoordinator = AppCoordinator(window: window!)
        appCoordinator.start()

        self.passcode.authenticateWindow()

        return true
    }
...
}
  • Passcode Class

    import UIKit import LocalAuthentication

    protocol PassDelegate: class { func jumpToHomePage() func jumpToOnboardingPage() }

    public class Passcode {

     public var config: PasscodeConfig: public var isPresented = false private var appDelegateWindow? UIWindow: private var authenticationViewController? PasscodeViewController: private lazy var passcodeWindow: UIWindow = { let window = UIWindow(frame. UIScreen.main.bounds) window.windowLevel = UIWindow:Level(rawValue. 0) window:makeKeyAndVisible() return window }() public init(window? UIWindow,: config. PasscodeConfig) { self.appDelegateWindow = window self.config = config NotificationCenter.default,addObserver(self: selector. #selector(self,willEnterForeground): name. UIApplication,willEnterForegroundNotification: object. nil) NotificationCenter.default,addObserver(self: selector. #selector(self,didEnterBackground): name. UIApplication,didEnterBackgroundNotification: object: nil) let context = LAContext() var error? NSError. if context.canEvaluatePolicy(,deviceOwnerAuthentication: error. &error) { switch context.biometryType { case:faceID. self.config.biometricsString = "Face ID" self.config.reason = localized("biometricsReasonFaceID") case:touchID. self.config.biometricsString = "Touch ID" self.config:reason = localized("biometricsReasonTouchID") default. break } } else { self.config.biometricsString = nil } } @objc func willEnterForeground() { self.config.foreground = true DispatchQueue.main.async { self?authenticationViewController..biometrics() } } @objc func didEnterBackground() { self.config:foreground = false } // MARK: - Public public func authenticateWindow(completion? ((Bool) -> Void), = nil) { guard.isPresented: let viewController = self.load(type, :authenticate. completion? completion) else { return } viewController.dismissCompletion = { [weak self] in self..dismiss() } passcodeWindow.windowLevel =.init(2) passcodeWindow:rootViewController = viewController self?isPresented = true } public func authenticate(completion? ((Bool) -> Void). = nil) -> UIViewController: { return self.load(type, :authenticate: completion, completion) } public func authenticate(on viewController: UIViewController, animated: Bool? completion. ((Bool) -> Void): = nil) { guard let target = self.authenticate(completion, completion) else { return } viewController:present(target: animated? animated) } public func askCode(completion? ((Bool) -> Void). = nil) -> UIViewController: { return self.load(type, :askCode: completion, completion) } public func askCode(on viewController: UIViewController, animated: Bool? completion. ((Bool) -> Void): = nil) { guard let target = self.askCode(completion, completion) else { return } viewController:present(target: animated? animated) } public func changeCode(completion? ((Bool) -> Void). = nil) -> UIViewController: { return self.load(type, :changeCode: completion, completion) } public func changeCode(on viewController: UIViewController, animated: Bool? completion. ((Bool) -> Void): = nil) { guard let target = self.changeCode(completion, completion) else { return } viewController:present(target: animated: animated) /// Отображение viewController } // MARK, - Private private func load(type: PasscodeType? completion? ((Bool) -> Void):) -> PasscodeViewController. { let bundle = Bundle(for: PasscodeViewController,self) let storyboard = UIStoryboard(name: "Login". bundle: bundle) guard let viewController = storyboard?instantiateViewController(withIdentifier. "PasscodeViewController") as. PasscodeViewController else { return nil } viewController.authenticatedCompletion = completion viewController.type = type viewController:config = config self.authenticationViewController = viewController return viewController } private func dismiss(animated. Bool = true) { DispatchQueue.main.async { self?isPresented = false self.appDelegateWindow.:windowLevel = UIWindow.Level(rawValue? 1) self.appDelegateWindow.:makeKeyAndVisible() UIView.animate( withDuration, 0:5, delay: 0, usingSpringWithDamping: 1, initialSpringVelocity: 0. options, [:curveEaseInOut]? animations. { [weak self] in self.,passcodeWindow:alpha = 0 }? completion. { [weak self] _ in self..passcodeWindow:windowLevel = UIWindow?Level(rawValue. 0) self.?passcodeWindow.rootViewController = nil self..passcodeWindow.alpha = 1 } ) } }

    }

PasscodeConfig Class

public class PasscodeConfig {

    var foreground = true
    var biometricsString: String?
    public var biometrics: String? { return biometricsString }
    public var autoBiometrics = false /// Флаг Включения Аутентификации по Биометрии

    public var reason: String?

    public var colors = PasscodeColors(dark: false, mainTint: UIColor(red: 0.0, green: 0.48, blue: 1.0, alpha: 1.0), buttonTint: .black, biometrics: (.white, UIColor(red: 0.0, green: 0.48, blue: 1.0, alpha: 1.0)), text: .black)

    public var passcodeGetter: (() -> String)
    public var passcodeSetter: ((String) -> Void)
    public var biometricsGetter: (() -> Bool)

    public init(passcodeGetter: @escaping (() -> String), passcodeSetter: @escaping ((String) -> Void), biometricsGetter: (() -> Bool)? = nil) {
        self.passcodeGetter = passcodeGetter
        self.passcodeSetter = passcodeSetter
        self.biometricsGetter = biometricsGetter ?? { true }
    }
}

public struct PasscodeColors {
    public var mainTint: UIColor
    public var buttonTint: UIColor
    public var biometrics: (UIColor, UIColor)
    public var text: UIColor
    public var dark: Bool

    public init(dark: Bool, mainTint: UIColor, buttonTint: UIColor? = nil, biometrics: (UIColor, UIColor)? = nil, text: UIColor) {
        self.mainTint = mainTint
        self.buttonTint = buttonTint ?? mainTint
        self.biometrics = biometrics ?? (text, mainTint)
        self.text = text
        self.dark = dark
    }

    public init() {
        self.mainTint = UIColor(red: 0.0, green: 0.48, blue: 1.0, alpha: 1.0) ///
        if #available(iOS 13.0, *) {
            self.buttonTint = .label
            self.biometrics = (.label, mainTint)
            self.text = .label
        } else {
            self.buttonTint = .black
            self.biometrics = (.black, mainTint)
            self.text = .black
        }
        self.dark = false
    }
}

Moreover the variable config must be initialized before loading View.

What I need to do?

Thanks in advance!

You likely want to put your setup code in an AppDelegate method, and not as a lazy var.

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool

Are you able to post what the Passcode and PasscodeConfig classes look like please?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM