简体   繁体   中英

SwiftUI: How to get window in Objective-C

let scene = UIApplication.shared.connectedScenes.first
if let sd: SceneDelegate = (scene?.delegate as? SceneDelegate) {
    let window = sd.window
}

this is in Swift, I want to convert this into Objective-C. I have tried this:

UIScene *scene = [[[[UIApplication sharedApplication]connectedScenes]allObjects]firstObject];

but now there is no SceneDelegate ; in Objective-C there is UIKit's UIWindowSceneDelegate .

also I wasn't able to access UIScenedelegate. Scenedelegate is in Swift and I am trying to get window in objective-c but now I'm not able to access window in swiftUI.

Check scene.delegate to make sure it conforms to the protocol, then cast the protocol on it so the compiler will let you use the protocol properties and/or methods.

UIScene *scene = [[[[UIApplication sharedApplication] connectedScenes] allObjects] firstObject];

if([scene.delegate conformsToProtocol:@protocol(UIWindowSceneDelegate)]){
    UIWindow *window = [(id <UIWindowSceneDelegate>)scene.delegate window];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM