danielsaidi / Sheeeeeeeeet

Sheeeeeeeeet is a Swift library for creating menus, custom action sheets, context menus etc.
MIT License
1.73k stars 109 forks source link

UIApplication.shared.keyWindow is unavailable (Xcode 13 Beta3 / iOS 15) #148

Closed apps4everyone closed 3 years ago

apps4everyone commented 3 years ago

UIApplication.shared.keyWindow:

'shared' is unavailable in application extensions for iOS: Use view controller based solutions where appropriate instead.

Maybe: a UIApplication extension: extension UIApplication { /// The app's key window taking into consideration apps that support multiple scenes. var keyWindowInConnectedScenes: UIWindow? { return windows.first(where: { $0.isKeyWindow }) } } Usage: let myKeyWindow: UIWindow? = UIApplication.shared.keyWindowInConnectedScenes

danielsaidi commented 3 years ago

But you're still using shared on the last line :)

apps4everyone commented 3 years ago

Haven't tried, just found this as a answer to be honest ;-)

danielsaidi commented 3 years ago

Aha ok 👌

apps4everyone commented 3 years ago

here is a PR related to this for CodeScanner: https://github.com/twostraws/CodeScanner/pull/37/files

danielsaidi commented 3 years ago

The problem is that we don't have anything to use as a reference.

I tried this with Xcode beta 1 and everything seems to work fine. How do you run into this problem?

apps4everyone commented 3 years ago

I think it is a breaking change in beta 3

danielsaidi commented 3 years ago

Do you face this problem in the package, demo app or in an app of your own? If so, do you use CocoaPods or SPM?

apps4everyone commented 3 years ago

Sorry for not adding this: in our own app with SPM: url: "https://github.com/danielsaidi/Sheeeeeeeeet.git", .exact("3.1.4")

danielsaidi commented 3 years ago

Thank you! I am downloading Xcode 13 beta 3 now.

If you find a way to solve this without changing the api:s before I do, please let me know.

apps4everyone commented 3 years ago

maybe marking the functions unavailable for iOSApplicationExtension will help?

@available(iOSApplicationExtension, unavailable) func addActionSheet(_ sheet: ActionSheet, to vc: UIViewController)

@available(iOSApplicationExtension, unavailable) open override func present(_ sheet: ActionSheet, in vc: UIViewController, view: UIView? = nil, item: UIBarButtonItem? = nil, completion: @escaping () -> ())

@available(iOSApplicationExtension, unavailable) func addActionSheetToKeyWindow(_ sheet: ActionSheet)

danielsaidi commented 3 years ago

That's a great idea, I will try it :)

danielsaidi commented 3 years ago

@apps4everyone Hmmm, I don't get an error when I build Sheeeeeeeeet with Xcode 13 beta 4, but I do for other open source projects like SwiftUIKit. In these projects, your fix works 🎉

How do you run into the problem in Sheeeeeeeeet? In the package, demo app or in your own app? I will try investigating.

apps4everyone commented 3 years ago

Maybe if the app has an extension? We have notifications extension.

danielsaidi commented 3 years ago

The extension shouldn't add Sheeeeeeeeet as a dependency, though.

danielsaidi commented 3 years ago

I solved this by removing UIApplication.shared instead of using the extension unavailable annotation, since that has many side-effects that I could not get around.

I have released this fix as a 3.2.