Swift library that supports both app and extension
1st solution
I recommend you to use UIApplication injection into your lib. It can be achieved like this.
Your lib instance can look similar to this:
class DarrenLib {
static var shared = DarrenLib()
// User application instance everywhere where needed in your lib.
private var application: UIApplication?
func setup(_ app: UIApplication) {
self.application = app
}
}
As example injection can be done in the did finish launching function:
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
DarrenLib.shared.setup(application)
return true
}
I would not recommend use preprocessor macros, because it becomes depending on additional setup requirements which can not be done throw source code.
2nd solution
Also as you are currently using app and extension targets. You could create these frameworks:
- DJSwiftCommonHelpers. In this framework will contain source code which will work on an app and extension target.
- DJSwiftAppHelpers. This framework will contain source code which will contain UIKit specific cases like UIApplication shared instance.
- DJSwiftExtensionHelpers. In this framework will contain source code specific only for extensions if here are any.
DJApp will be embedding DJSwiftCommonHelpers and DJSwiftAppHelpers frameworks.
DJNotificationExtension will be embedding DJSwiftCommonHelpers and DJSwiftExtensionHelpers frameworks.