[英]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.