简体   繁体   中英

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). How can I call a method in AppDelegate.swift from within the plugin swift code?

You can save data in Userdefaults from your custom capacitor plugin and after that you can fetch this data from AppDelegate.swift as-

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:

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

AppDelegate must be the name of the delegate class.

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