HAMZA ZULFQAR
HAMZA ZULFQAR

Reputation: 37

Getting AppDelegate reference while creating framework swift

I am creating a custom framework. I need a reference to the AppDelegate to perform operations e.g get window or some other delegates.

example code:

static var safeArea: UIEdgeInsets {
    var padding: UIEdgeInsets = UIEdgeInsets.zero
    if #available(iOS 11.0, *) {
        if let b = appDel.window?.safeAreaInsets {
            padding = b
        }
    }
    return padding
}

The main issue I have is that I need the AppDelegate reference on multiple levels.

Any help regarding this should be appreciated.

Upvotes: 0

Views: 1258

Answers (1)

erkutbas
erkutbas

Reputation: 303

Sample Source for comments below :). Create a sampleManager inside framework. Import the framework into appdelegate and set SampleManager.shared.delegate to self as weak reference.

public class SampleManager {

    public static let shared = SampleManager()
    
    public weak var delegate: UIApplicationDelegate?
    
    func doWhateverYouWant() {
    
    }
}
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    private let dependencyManager = DependencyManager()

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        
        SampleManager.shared.delegate = self
        
        startAppCoordinator()
        return true
    }
}

Upvotes: 2

Related Questions