簡體   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