Home > Software design >  Swift Use Custom Variables of AppDelegate in Framework
Swift Use Custom Variables of AppDelegate in Framework

Time:09-08

I have an application which contains some framework. AppDelegate declares some variables, so I want to use these custom variables in framework. but unfortunately not getting it. I use this stack overflow link according to this answer I am able to use app delegate methods like didFinishLaunchingWithOptions and other methods but not able to get the custom variables and methods from app delegate.

 public class SampleManager {


    public static let shared = SampleManager()

    public weak var delegate: UIApplicationDelegate?//UIApplicationDelegate?

    func doWhateverYouWant() {

    }
}

I declared the SampleManager and assign its delegate in the didFinishLaunchingWithOptions like this

SampleManager.shared.delegate = self

now this delegate allow me to access default methods and variables like window but not allowing me custom methods and variables. Kindly guide me how to access custom variables and custom methods declared in app delegate.

CodePudding user response:

That's because UIApplicationDelegate doesn't contain your custom ones, just extend it

protocol Custom: UIApplicationDelegate {
    func yourFunc()
}

class AppDelegate: UIResponder, Custom {
    func yourFunc() {}
    ...
}

class SampleManager {
    static let shared = SampleManager()
    weak var delegate: Custom?

    func doWhateverYouWant() {
        delegate?.yourFunc()
    }
}
  • Related