简体   繁体   English

将数据从 Capacitor 插件传递到 AppDelegate.swift

[英]Passing data from Capacitor plugin to AppDelegate.swift

I have an Ionic Capacitor app, and I have some data within custom capacitor plugin that I need to get into AppDelegate.swift (./ios/App/AppDelegate.swift).我有一个离子电容器应用程序,我在自定义电容器插件中有一些数据,我需要进入 AppDelegate.swift (./ios/App/AppDelegate.swift)。 How can I call a method in AppDelegate.swift from within the plugin swift code?如何从插件 swift 代码中调用 AppDelegate.swift 中的方法?

You can save data in Userdefaults from your custom capacitor plugin and after that you can fetch this data from AppDelegate.swift as-您可以从自定义电容器插件中将数据保存在 Userdefaults 中,然后您可以从 AppDelegate.swift 获取此数据作为-

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        if let myData = UserDefaults.standard.object(forKey: "YourKey") {
            // decode myData and use as your need
        }
        return true
 }

You can call a function of the app delegate instance using the following pattern:您可以使用以下模式调用应用程序委托实例的 function:

        if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
            appDelegate.test()
        }

AppDelegate must be the name of the delegate class. AppDelegate必须是委托 class 的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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