From 286c153583877bbca73878faaba0ef54cf0af9bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tor=20Arne=20Vestb=C3=B8?= Date: Thu, 3 May 2018 17:06:24 +0200 Subject: [PATCH] iOS: Handle application state for application extensions Change-Id: I97df0f8ecf93e28bfbe9c719922f1ee5ec12b563 Reviewed-by: Richard Moe Gustavsen --- .../platforms/ios/qiosapplicationstate.mm | 27 ++++++++++++++----- 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/src/plugins/platforms/ios/qiosapplicationstate.mm b/src/plugins/platforms/ios/qiosapplicationstate.mm index 6d9bcdacbf..cc76d198f5 100644 --- a/src/plugins/platforms/ios/qiosapplicationstate.mm +++ b/src/plugins/platforms/ios/qiosapplicationstate.mm @@ -44,6 +44,7 @@ #include #include +#include #include @@ -57,7 +58,13 @@ static void qRegisterApplicationStateNotifications() // Map between notifications and corresponding application state. Note that // there's no separate notification for moving to UIApplicationStateInactive, // so we use UIApplicationWillResignActiveNotification as an intermediate. - static QMap notifications { + using NotificationMap = QMap; + static auto notifications = qt_apple_isApplicationExtension() ? NotificationMap{ + { NSExtensionHostWillEnterForegroundNotification, UIApplicationStateInactive }, + { NSExtensionHostDidBecomeActiveNotification, UIApplicationStateActive }, + { NSExtensionHostWillResignActiveNotification, UIApplicationStateInactive }, + { NSExtensionHostDidEnterBackgroundNotification, UIApplicationStateBackground }, + } : NotificationMap{ { UIApplicationWillEnterForegroundNotification, UIApplicationStateInactive }, { UIApplicationDidBecomeActiveNotification, UIApplicationStateActive }, { UIApplicationWillResignActiveNotification, UIApplicationStateInactive }, @@ -73,16 +80,24 @@ static void qRegisterApplicationStateNotifications() }]; } - // Initialize correct startup state, which may not be the Qt default (inactive) - UIApplicationState startupState = [UIApplication sharedApplication].applicationState; - QIOSApplicationState::handleApplicationStateChanged(startupState, QLatin1String("Application loaded")); + if (qt_apple_isApplicationExtension()) { + // Extensions are not allowed to access UIApplication, so we assume the state is active + QIOSApplicationState::handleApplicationStateChanged(UIApplicationStateActive, + QLatin1String("Extension loaded, assuming state is active")); + } else { + // Initialize correct startup state, which may not be the Qt default (inactive) + UIApplicationState startupState = [UIApplication sharedApplication].applicationState; + QIOSApplicationState::handleApplicationStateChanged(startupState, QLatin1String("Application loaded")); + } } Q_CONSTRUCTOR_FUNCTION(qRegisterApplicationStateNotifications) QIOSApplicationState::QIOSApplicationState() { - UIApplicationState startupState = [UIApplication sharedApplication].applicationState; - QIOSApplicationState::handleApplicationStateChanged(startupState, QLatin1String("Application launched")); + if (!qt_apple_isApplicationExtension()) { + UIApplicationState startupState = [UIApplication sharedApplication].applicationState; + QIOSApplicationState::handleApplicationStateChanged(startupState, QLatin1String("Application launched")); + } } void QIOSApplicationState::handleApplicationStateChanged(UIApplicationState uiState, const QString &reason)