Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

singleton class shared between app and extension

Tags:

ios

swift

iphone

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

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() {}
}
like image 553
Mohamed Farghal Avatar asked Apr 13 '26 08:04

Mohamed Farghal


1 Answers

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.

like image 117
Paulw11 Avatar answered Apr 14 '26 22:04

Paulw11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!