From 83ace9d3912e9e0f3589f1b6224982cb18f4a2df Mon Sep 17 00:00:00 2001 From: Dave Weston Date: Mon, 16 Oct 2017 12:33:42 -0700 Subject: [PATCH 1/2] Listen for extension host notifications if needed The UIApplication... notifications are not fired if Hockey is running in the context of an extension. In this case, we should be using the NSExtensionHost... notifications instead. --- Classes/BITCrashManager.m | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index 823af61b..a5786e12 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -474,7 +474,14 @@ - (void) registerObservers { __weak typeof(self) weakSelf = self; if(nil == self.appDidBecomeActiveObserver) { - self.appDidBecomeActiveObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification + NSNotificationName name = UIApplicationDidBecomeActiveNotification; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpartial-availability" + if (bit_isRunningInAppExtension() && &NSExtensionHostDidBecomeActiveNotification != NULL) { + name = NSExtensionHostDidBecomeActiveNotification; + } +#pragma clang diagnostic pop + self.appDidBecomeActiveObserver = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification __unused *note) { @@ -504,7 +511,14 @@ - (void) registerObservers { } if (nil == self.appDidEnterBackgroundObserver) { - self.appDidEnterBackgroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidEnterBackgroundNotification + NSNotificationName name = UIApplicationDidEnterBackgroundNotification; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpartial-availability" + if (bit_isRunningInAppExtension() && &NSExtensionHostDidEnterBackgroundNotification != NULL) { + name = NSExtensionHostDidEnterBackgroundNotification; + } +#pragma clang diagnostic pop + self.appDidEnterBackgroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification __unused *note) { @@ -514,7 +528,14 @@ - (void) registerObservers { } if (nil == self.appWillEnterForegroundObserver) { - self.appWillEnterForegroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationWillEnterForegroundNotification + NSNotificationName name = UIApplicationWillEnterForegroundNotification; +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wpartial-availability" + if (bit_isRunningInAppExtension() && &NSExtensionHostWillEnterForegroundNotification != NULL) { + name = NSExtensionHostWillEnterForegroundNotification; + } +#pragma clang diagnostic pop + self.appWillEnterForegroundObserver = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:NSOperationQueue.mainQueue usingBlock:^(NSNotification __unused *note) { From c8ad9bcecc8c3b8fff7d82e76f08478c82bfb4b4 Mon Sep 17 00:00:00 2001 From: Dave Weston Date: Mon, 16 Oct 2017 12:34:45 -0700 Subject: [PATCH 2/2] Detect memory pressure in extensions The UIApplicationDidReceiveMemoryWarningNotification isn't fired when running in an extension, so we need to resort to a lower-level mechanism. --- Classes/BITCrashManager.m | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/Classes/BITCrashManager.m b/Classes/BITCrashManager.m index a5786e12..6185646b 100644 --- a/Classes/BITCrashManager.m +++ b/Classes/BITCrashManager.m @@ -545,17 +545,30 @@ - (void) registerObservers { } if (nil == self.appDidReceiveLowMemoryWarningObserver) { - self.appDidReceiveLowMemoryWarningObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification - object:nil - queue:NSOperationQueue.mainQueue - usingBlock:^(NSNotification __unused *note) { - // we only need to log this once - if (!self.didLogLowMemoryWarning) { - [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kBITAppDidReceiveLowMemoryNotification]; - self.didLogLowMemoryWarning = YES; - - } - }]; + if (bit_isRunningInAppExtension()) { + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_MEMORYPRESSURE, 0, DISPATCH_MEMORYPRESSURE_WARN|DISPATCH_MEMORYPRESSURE_CRITICAL, dispatch_get_main_queue()); + dispatch_source_set_event_handler(source, ^{ + if (!self.didLogLowMemoryWarning) { + [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kBITAppDidReceiveLowMemoryNotification]; + self.didLogLowMemoryWarning = YES; + } + }); + }); + } else { + self.appDidReceiveLowMemoryWarningObserver = [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidReceiveMemoryWarningNotification + object:nil + queue:NSOperationQueue.mainQueue + usingBlock:^(NSNotification __unused *note) { + // we only need to log this once + if (!self.didLogLowMemoryWarning) { + [[NSUserDefaults standardUserDefaults] setBool:YES forKey:kBITAppDidReceiveLowMemoryNotification]; + self.didLogLowMemoryWarning = YES; + + } + }]; + } } }