1

I am working on iMessage app extension, I need to get app window in this extension for some reasons. Previously I was doing like this as mentioned bellow :- guard let currentWindow : UIWindow = UIApplication.shared.keyWindow else {return}

in xcode 12 it was working perfectly. On the other hand after updating to 12.5.1 , this started giving me error for the same code. As follow :- enter image description here

Secondly, I am not able to call UIApplication.shared.canOpenURL(settingsUrl) enter image description here

  • That seems to be a frequently asked question. Have you checked these Q&As? https://www.google.de/search?q=shared%27+is+unavailable+in+application+extensions+for+ios+site:stackoverflow.com – Martin R Jul 23 '21 at 07:32
  • Yes I checked, but don't get any luck. – varinder singh Jul 23 '21 at 07:41

1 Answers1

2

You can do something like this to test access to UIApplication.shared from your extension:

guard UIApplication.responds(to: NSSelectorFromString("sharedApplication")) else {
    return
}
let shared = UIApplication.perform(NSSelectorFromString("sharedApplication"))?.takeUnretainedValue() as? UIApplication

By using perform, you ignore the compiler and determine your call at runtime.

Be aware that this might not work in your extension. There is a reason why the API doesn't allow you to access it by default from your extension, even when it works fine from a normal app.

Same goes for your call to UIApplication.shared.open.

So in the end, you probably can't use UIApplication.shared. The key difference is extension vs. app.

wakel
  • 336
  • 2
  • 7