繁体   English   中英

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

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

我有一个离子电容器应用程序,我在自定义电容器插件中有一些数据,我需要进入 AppDelegate.swift (./ios/App/AppDelegate.swift)。 如何从插件 swift 代码中调用 AppDelegate.swift 中的方法?

您可以从自定义电容器插件中将数据保存在 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
 }

您可以使用以下模式调用应用程序委托实例的 function:

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

AppDelegate必须是委托 class 的名称。

暂无
暂无

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

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