From 5f24307ac8b4ab90c2da2ab2a9e67ba6c0c170be Mon Sep 17 00:00:00 2001 From: Andrey Filipenkov Date: Sun, 7 Aug 2022 09:31:56 +0300 Subject: [PATCH] fix observing SDL window's view creation on iOS 15 also improves observers handling fix kambala-decapitator/vcmi#38 --- client/ios/startSDL.mm | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/client/ios/startSDL.mm b/client/ios/startSDL.mm index ba0f0fd96..f6969c384 100644 --- a/client/ios/startSDL.mm +++ b/client/ios/startSDL.mm @@ -32,6 +32,8 @@ double ios_screenScale() { return UIScreen.mainScreen.nativeScale; } @implementation SDLViewObserver - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { + [object removeObserver:self forKeyPath:keyPath]; + UIView * view = [object valueForKeyPath:keyPath]; UITextField * textField; @@ -127,22 +129,31 @@ int startSDL(int argc, char * argv[], BOOL startManually) @autoreleasepool { auto observer = [SDLViewObserver new]; observer.gameChatHandler = [GameChatKeyboardHanlder new]; - [NSNotificationCenter.defaultCenter addObserverForName:UIWindowDidBecomeKeyNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { - [UIApplication.sharedApplication.keyWindow.rootViewController addObserver:observer forKeyPath:NSStringFromSelector(@selector(view)) options:NSKeyValueObservingOptionNew context:NULL]; + + id __block sdlWindowCreationObserver = [NSNotificationCenter.defaultCenter addObserverForName:UIWindowDidBecomeKeyNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { + [NSNotificationCenter.defaultCenter removeObserver:sdlWindowCreationObserver]; + sdlWindowCreationObserver = nil; + + UIWindow * sdlWindow = note.object; + [sdlWindow.rootViewController addObserver:observer forKeyPath:NSStringFromSelector(@selector(view)) options:NSKeyValueObservingOptionNew context:NULL]; }]; - [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidEndEditingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { + id textFieldObserver = [NSNotificationCenter.defaultCenter addObserverForName:UITextFieldTextDidEndEditingNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) { removeFocusFromActiveInput(); }]; - if (!startManually) - return SDL_UIKitRunApp(argc, argv, SDL_main); - - // copied from -[SDLUIKitDelegate postFinishLaunch] - SDL_SetMainReady(); - SDL_iOSSetEventPump(SDL_TRUE); - auto result = SDL_main(argc, argv); - SDL_iOSSetEventPump(SDL_FALSE); + int result; + if (startManually) + { + // copied from -[SDLUIKitDelegate postFinishLaunch] + SDL_SetMainReady(); + SDL_iOSSetEventPump(SDL_TRUE); + result = SDL_main(argc, argv); + SDL_iOSSetEventPump(SDL_FALSE); + } + else + result = SDL_UIKitRunApp(argc, argv, SDL_main); + [NSNotificationCenter.defaultCenter removeObserver:textFieldObserver]; return result; } }