diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2018-05-07 15:10:01 +0200 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2018-05-30 09:16:23 +0000 |
commit | 0587941f6eda05dfc3cc66ce44bf8f32320d7d24 (patch) | |
tree | 53e649e33eeeab68d20c015bb8678062c875f81d /src | |
parent | 6ada504475fc8ec3f7fe4d05f8af1ba3735208e9 (diff) |
Add function to safely access the shared application on Apple platforms
Change-Id: I52910309ba94d84d69f049b5c1990f1f866e1698
Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/kernel/qcore_mac_objc.mm | 22 | ||||
-rw-r--r-- | src/corelib/kernel/qcore_mac_p.h | 13 | ||||
-rw-r--r-- | src/corelib/kernel/qeventdispatcher_cf.mm | 17 |
3 files changed, 41 insertions, 11 deletions
diff --git a/src/corelib/kernel/qcore_mac_objc.mm b/src/corelib/kernel/qcore_mac_objc.mm index d1a736c9bb..c29c4dfc14 100644 --- a/src/corelib/kernel/qcore_mac_objc.mm +++ b/src/corelib/kernel/qcore_mac_objc.mm @@ -167,6 +167,28 @@ bool qt_apple_isApplicationExtension() return isExtension; } +#if !defined(QT_BOOTSTRAPPED) && !defined(Q_OS_WATCHOS) +AppleApplication *qt_apple_sharedApplication() +{ + // Application extensions are not allowed to access the shared application + if (qt_apple_isApplicationExtension()) { + qWarning() << "accessing the shared" << [AppleApplication class] + << "is not allowed in application extensions"; + + // In practice the application is actually available, but the the App + // review process will likely catch uses of it, so we return nil just + // in case, unless we don't care about being App Store compliant. +#if QT_CONFIG(appstore_compliant) + return nil; +#endif + } + + // We use performSelector so that building with -fapplication-extension will + // not mistakenly think we're using the shared application in extensions. + return [[AppleApplication class] performSelector:@selector(sharedApplication)]; +} +#endif + #ifdef Q_OS_MACOS /* Ensure that Objective-C objects auto-released in main(), directly or indirectly, diff --git a/src/corelib/kernel/qcore_mac_p.h b/src/corelib/kernel/qcore_mac_p.h index d0fab8254b..9bd2e31bc9 100644 --- a/src/corelib/kernel/qcore_mac_p.h +++ b/src/corelib/kernel/qcore_mac_p.h @@ -160,6 +160,19 @@ QDebug operator<<(QDebug debug, const QMacAutoReleasePool *pool); Q_CORE_EXPORT void qt_apple_check_os_version(); Q_CORE_EXPORT bool qt_apple_isApplicationExtension(); +#if !defined(QT_BOOTSTRAPPED) && !defined(Q_OS_WATCHOS) +QT_END_NAMESPACE +# if defined(Q_OS_MACOS) +Q_FORWARD_DECLARE_OBJC_CLASS(NSApplication); +using AppleApplication = NSApplication; +# else +Q_FORWARD_DECLARE_OBJC_CLASS(UIApplication); +using AppleApplication = UIApplication; +# endif +QT_BEGIN_NAMESPACE +Q_CORE_EXPORT AppleApplication *qt_apple_sharedApplication(); +#endif + // -------------------------------------------------------------------------- #if !defined(QT_BOOTSTRAPPED) && (QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_12) || !defined(Q_OS_MACOS)) diff --git a/src/corelib/kernel/qeventdispatcher_cf.mm b/src/corelib/kernel/qeventdispatcher_cf.mm index 8499b3fd57..503836d071 100644 --- a/src/corelib/kernel/qeventdispatcher_cf.mm +++ b/src/corelib/kernel/qeventdispatcher_cf.mm @@ -72,17 +72,12 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(RunLoopModeTracker); if (self = [super init]) { m_runLoopModes.push(kCFRunLoopDefaultMode); - [[NSNotificationCenter defaultCenter] - addObserver:self - selector:@selector(receivedNotification:) - name:nil -#ifdef Q_OS_OSX - object:[NSApplication sharedApplication]]; -#elif defined(Q_OS_WATCHOS) - object:[WKExtension sharedExtension]]; -#else - // Use performSelector so this can work in an App Extension - object:[[UIApplication class] performSelector:@selector(sharedApplication)]]; +#if !defined(Q_OS_WATCHOS) + if (!qt_apple_isApplicationExtension()) { + [[NSNotificationCenter defaultCenter] + addObserver:self selector:@selector(receivedNotification:) + name:nil object:qt_apple_sharedApplication()]; + } #endif } |