简体   繁体   English

应用程序和扩展程序之间共享的单例类

[英]singleton class shared between app and extension

When i add value like reference for UIViewController or string Id to singleton class, then try to access them from share extension i could't get this value again the share extension create new singleton with null value当我将UIViewController引用或字符串 Id 之类的值添加到单例类,然后尝试从共享扩展访问它们时,我无法再次获取此值共享扩展创建具有空值的新单例

How i can make this class and the data inside it shared between main app and extension?我怎样才能让这个类和它里面的数据在主应用程序和扩展程序之间共享?

class Gateway:NSObject {

    private var id:String? = nil
    private weak var delegate:GatewayDelegate!

    func set(id:String){
        self.id = id
    }

    func set(gatewayDelegate:GatewayDelegate){
        self.delegate = gatewayDelegate
    }

    func dismiss() {
        self.id = nil
        self.delegate = nil
    }


    func append(str:String,_id:String) {

        if let id = self.id ,id == _id ,self.delegate != nil  {            
            self.delegate!.gatewayAppend(str: str)
        }
    }

    static let shared = Gateway()
    private override init() {}
}

An extension is a completely separate process.扩展是一个完全独立的过程。 It runs in its own sandbox and its own memory.它在自己的沙箱和自己的内存中运行。 Both your main app and your extension can create an instance of the singleton object, but they will be separate instances.您的主应用程序和扩展程序都可以创建单例对象的实例,但它们将是单独的实例。 They won't share any data.他们不会分享任何数据。

To exchange data between your main app and your extension you will need to use an app group with user defaults, the keychain or a file.要在主应用程序和扩展程序之间交换数据,您需要使用具有用户默认值、钥匙串或文件的应用程序组

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

相关问题 应用扩展程序和主应用之间的共享文件 - Shared file between app extension and main app IOS应用程序和手表扩展之间的共享文件 - Shared files between IOS app and watch extension 应用和扩展程序之间共享的UserDefaults无法正常工作 - Shared UserDefaults between app and extension not working correctly iOS Couchbase:应用程序和应用程序扩展之间共享的CBL管理器 - iOS Couchbase: Shared CBL manager between app and app extension 在iOS Extension和app之间使用UIApplication.shared共享CocoaPod - Sharing CocoaPod using UIApplication.shared between iOS Extension and app 如何在App及其扩展之间共享课程? - How to share a class between app and it extension? 通过扩展为每个应用创建单例实例吗? - Create singleton instance via extension per app? 如何创建/使用共享应用程序组容器作为包含应用程序及其在ios中的扩展名之间的缓存存储 - How to create/use a shared app group container as cache storage between containing app and it's extension in ios 在应用程序中的Today扩展和ViewController之间公开类 - make public class between Today extension and ViewController in app 是否有比NSUserDefaults更安全的容器来持久保存应用程序和扩展之间的共享数据? - Is there a more secure container than NSUserDefaults for persisting shared data between an app and an extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM