Swift - 'Sharedapplication Is Unavailable.' Use View Controller Based Solutions Where Appropriate Instead

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.

  1. Use a protocol to describe the functionality that is currently provided by UIApplication.shared.

  2. Implement that protocol for the base app, where UIApplication.shared is available and use it there. Implement another implementation without using UIApplication.shared, which would be used for app extensions.

  3. 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.

  4. 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 with UIApplication.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 ...
}
}
  1. So first, a protocol:

    protocol NetworkIndicator {
    func showActivity()
    }
  2. Then two implementations:

    class AppNetworkIndicator: NetworkIndicator {
    func showActivity() {
    UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }
    }

    class ExtensionNetworkIndicator: NetworkIndicator {
    func showActivity() {}
    }
  3. 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 ...
    }
    }
  4. 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.

Image of setting

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



Leave a reply



Submit