shared' is unavailable: Use view controller based solutions where appropriate instead on App Extension
Use dependency injection pattern to refactor out the code depending on UIApplication.shared
.
Use a protocol to describe the functionality that is currently provided by
UIApplication.shared
.Implement that protocol for the base app, where
UIApplication.shared
is available and use it there. Implement another implementation without usingUIApplication.shared
, which would be used for app extensions.Refactor your current class to use the protocol implementing objects and inject the proper implementation at the initialization (dependency injection pattern) - initializer of that class would accept a protocol implementation.
Wire up a proper implementation when creating an instance of the refactored class - in app extension pass it the implementation without
UIApplication.shared
, in base app the one withUIApplication.shared
.
So e.g., Let's have this big class here:
class Big {
... a lot of code ...
func a() {
... again some code ...
// here the code that needs shared
UIApplication.shared.isNetworkActivityIndicatorVisible = true
... more code ...
}
}
So first, a protocol:
protocol NetworkIndicator {
func showActivity()
}Then two implementations:
class AppNetworkIndicator: NetworkIndicator {
func showActivity() {
UIApplication.shared.isNetworkActivityIndicatorVisible = true
}
}
class ExtensionNetworkIndicator: NetworkIndicator {
func showActivity() {}
}Refactor
Big
to support dependency injection:class Big {
private let networkIndicator: NetworkIndicator
init(networkIndicator: NetworkIndicator) {
self.networkIndicator = networkIndicator
}
... rest of code ...
func a() {
... again some code ...
// use injected protocol implementation
networkIndicator.showActivity()
... more code ...
}
}Finally, in the app, where you use the
Big
class, initiate it with:let big = Big(networkIndicator: AppNetworkIndicator())
And in the extension, use the other implementation:
let big = Big(networkIndicator: ExtensionNetworkIndicator())
IQKeyboardManagerSwift 'shared' is unavailable: Use view controller based solutions where appropriate instead
Seems like the problem is that you are using extensions in your app. Try setting Require Only App-Extension_safe API
to No inside your Pods
project for the IQKeyboardManagerSwift target.
shared' is unavailable use view controller based solutions where appropriate instead Swift 3
If you only want the main screen's bounds you can get them by using the following code.
let mainScreenSize = UIScreen.main.bounds.size
Facebook cocoapods 'sharedApplication' is unavailable: not available on iOS (App Extension)
Try to comment OneSignalNotificationServiceExtension
target & pod , clean , install and run again
UIApplication.sharedApplication() is unavailable
The classes in containing app somehow went in to the compile resources list in the extension Build Phases. I deleted them, it is ok now.
I did not do that. Obviously upgrade to Xcode 7 GM process somehow did it.
Related Topics
How to Use Key-Value Coding in Swfit 4.0
Uibutton Subclass @Ibaction Not Working
Unusernotificationcenter.Current().Getdeliverednotifications Only Ever Returns an Empty Array
Fill Uiview with Color Based on Percentage
Swift: Have Searchbar Search Through Both Sections and Not Combine Them
Why Protocol Is Better Than Class in Swift
Make a Uiwebview as Height as Its Content with Auto Layout
iOS Swift Detecting Image Rotation
Custom Uitoolbar Too Close to the Home Indicator on iPhone X
How to Use Realm.Addnotificationblock
Read Logs Using the New Swift Os_Log API
Nsnotificationcenter Addobserver in Swift While Call a Private Method
How to Use Type Erasure with a Protocol Using Associated Type