Skip to content

Commit 87da90e

Browse files
committed
fixing application state innacuracy
1 parent 1ef2bd6 commit 87da90e

File tree

9 files changed

+104
-12
lines changed

9 files changed

+104
-12
lines changed

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,11 @@
425425
DE16C14524D3724700670EFA /* OneSignalLifecycleObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */; };
426426
DE16C14724D3727200670EFA /* OneSignalLifecycleObserver.h in Headers */ = {isa = PBXBuildFile; fileRef = DE16C14624D3727200670EFA /* OneSignalLifecycleObserver.h */; };
427427
DE16C17024D3989A00670EFA /* OneSignalLifecycleObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */; };
428+
DE20425E24E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */; };
429+
DE20425F24E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */; };
430+
DE20426024E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */ = {isa = PBXBuildFile; fileRef = DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */; };
428431
DE5EFECA24D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m in Sources */ = {isa = PBXBuildFile; fileRef = DE5EFEC924D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m */; };
432+
DEE8198D24E21DF000868CBA /* UIApplication+OneSignal.h in Headers */ = {isa = PBXBuildFile; fileRef = DE20425C24E21C1500350E4F /* UIApplication+OneSignal.h */; };
429433
/* End PBXBuildFile section */
430434

431435
/* Begin PBXCopyFilesBuildPhase section */
@@ -700,6 +704,8 @@
700704
CACBAAAB218A662B000ACAA5 /* WebKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = WebKit.framework; path = System/Library/Frameworks/WebKit.framework; sourceTree = SDKROOT; };
701705
DE16C14324D3724700670EFA /* OneSignalLifecycleObserver.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OneSignalLifecycleObserver.m; sourceTree = "<group>"; };
702706
DE16C14624D3727200670EFA /* OneSignalLifecycleObserver.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OneSignalLifecycleObserver.h; sourceTree = "<group>"; };
707+
DE20425C24E21C1500350E4F /* UIApplication+OneSignal.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIApplication+OneSignal.h"; sourceTree = "<group>"; };
708+
DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = "UIApplication+OneSignal.m"; sourceTree = "<group>"; };
703709
DE5EFEC924D8DBF70032632D /* OSInAppMessageViewControllerOverrider.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSInAppMessageViewControllerOverrider.m; sourceTree = "<group>"; };
704710
DE5EFECB24D8DC0E0032632D /* OSInAppMessageViewControllerOverrider.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSInAppMessageViewControllerOverrider.h; sourceTree = "<group>"; };
705711
/* End PBXFileReference section */
@@ -1104,6 +1110,8 @@
11041110
CA1A6E6820DC2E31001C41B9 /* OneSignalDialogController.m */,
11051111
CA1A6E6D20DC2E73001C41B9 /* OneSignalDialogRequest.h */,
11061112
CA1A6E6E20DC2E73001C41B9 /* OneSignalDialogRequest.m */,
1113+
DE20425C24E21C1500350E4F /* UIApplication+OneSignal.h */,
1114+
DE20425D24E21C2C00350E4F /* UIApplication+OneSignal.m */,
11071115
);
11081116
name = Categories;
11091117
sourceTree = "<group>";
@@ -1320,6 +1328,7 @@
13201328
9124123D1E73342200E41FD7 /* UIApplicationDelegate+OneSignal.h in Headers */,
13211329
7AF9865324451F3900C36EAE /* OSFocusCallParams.h in Headers */,
13221330
CAB269D921B0B6F000F8A43C /* OSInAppMessageAction.h in Headers */,
1331+
DEE8198D24E21DF000868CBA /* UIApplication+OneSignal.h in Headers */,
13231332
7AECE59C23675F5700537907 /* OSFocusTimeProcessorFactory.h in Headers */,
13241333
7AECE59A23674ADC00537907 /* OSUnattributedFocusTimeProcessor.h in Headers */,
13251334
CA63AFC22022670A00E340FB /* ReattemptRequest.h in Headers */,
@@ -1588,6 +1597,7 @@
15881597
CA63AFC32022670A00E340FB /* ReattemptRequest.m in Sources */,
15891598
912412221E73342200E41FD7 /* OneSignalLocation.m in Sources */,
15901599
CACBAAA4218A6243000ACAA5 /* OSInAppMessageViewController.m in Sources */,
1600+
DE20425E24E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */,
15911601
CA7FC8A021927229002C4FD9 /* OSDynamicTriggerController.m in Sources */,
15921602
7AECE59623674AB700537907 /* OSUnattributedFocusTimeProcessor.m in Sources */,
15931603
1AF75EAE1E8567FD0097B315 /* NSString+OneSignal.m in Sources */,
@@ -1678,6 +1688,7 @@
16781688
CA7FC8A121927229002C4FD9 /* OSDynamicTriggerController.m in Sources */,
16791689
912412231E73342200E41FD7 /* OneSignalLocation.m in Sources */,
16801690
9D1BD965237A08A400A064F7 /* OneSignalUserDefaults.m in Sources */,
1691+
DE20425F24E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */,
16811692
7AF9863C2444C43900C36EAE /* OSInAppMessageTracker.m in Sources */,
16821693
1AF75EB01E8569720097B315 /* NSString+OneSignal.m in Sources */,
16831694
9129C6BF1E89E7AB009CB6A0 /* OSSubscription.m in Sources */,
@@ -1762,6 +1773,7 @@
17621773
912412341E73342200E41FD7 /* OneSignalTracker.m in Sources */,
17631774
7AF98694244A567B00C36EAE /* OSOutcomeEventsCache.m in Sources */,
17641775
03866CBD2378A33B0009C1D8 /* OutcomeIntegrationTests.m in Sources */,
1776+
DE20426024E21C2C00350E4F /* UIApplication+OneSignal.m in Sources */,
17651777
CA36F35B21C33A2500300C77 /* OSInAppMessageController.m in Sources */,
17661778
9124122C1E73342200E41FD7 /* OneSignalReachability.m in Sources */,
17671779
03389F691FB548A0006537F0 /* OneSignalTrackFirebaseAnalyticsOverrider.m in Sources */,

iOS_SDK/OneSignalSDK/Source/OSMessagingController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ - (void)presentInAppMessage:(OSInAppMessage *)message {
224224
if (self.isInAppMessageShowing)
225225
return;
226226
// Return early if the app is not active
227-
if ([[UIApplication sharedApplication] applicationState] != UIApplicationStateActive) {
227+
if (![UIApplication applicationIsActive]) {
228228
[OneSignal onesignal_Log:ONE_S_LL_VERBOSE message:@"Pause IAMs display due to app inactivity"];
229229
_isAppInactive = YES;
230230
return;

iOS_SDK/OneSignalSDK/Source/OneSignalHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
#import "OneSignal.h"
2929
#import "OneSignalWebView.h"
30+
#import "UIApplication+OneSignal.h"
3031

3132
#pragma clang diagnostic push
3233
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

iOS_SDK/OneSignalSDK/Source/OneSignalLifecycleObserver.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@ THE SOFTWARE.
2828
+ (OneSignalLifecycleObserver*) sharedInstance;
2929
+ (void)registerLifecycleObserver;
3030
+ (void)removeObserver;
31-
+ (BOOL)isAppUsingUIScene;
3231
@end

iOS_SDK/OneSignalSDK/Source/OneSignalLifecycleObserver.m

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ of this software and associated documentation files (the "Software"), to deal
3232
#import "OneSignalTracker.h"
3333
#import "OneSignalLocation.h"
3434
#import "OSMessagingController.h"
35+
#import "UIApplication+OneSignal.h"
3536

3637
@implementation OneSignalLifecycleObserver
3738

@@ -47,16 +48,9 @@ +(OneSignalLifecycleObserver*) sharedInstance {
4748
return _instance;
4849
}
4950

50-
+ (BOOL)isAppUsingUIScene {
51-
if (@available(iOS 13.0, *)) {
52-
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIApplicationSceneManifest"] != nil;
53-
}
54-
return NO;
55-
}
56-
5751
+ (void)registerLifecycleObserver {
5852
// Replacing swizzled lifecycle selectors with notification center observers for scene based Apps
59-
if ([self isAppUsingUIScene]) {
53+
if ([UIApplication isAppUsingUIScene]) {
6054
[self registerLifecycleObserverAsUIScene];
6155
} else {
6256
[self registerLifecycleObserverAsUIApplication];
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2020 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
#import <Foundation/Foundation.h>
28+
#import <UIKit/UIKit.h>
29+
@interface UIApplication (OneSignal)
30+
+ (BOOL)applicationIsActive;
31+
+ (BOOL)isAppUsingUIScene;
32+
@end
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Modified MIT License
3+
*
4+
* Copyright 2020 OneSignal
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* 1. The above copyright notice and this permission notice shall be included in
14+
* all copies or substantial portions of the Software.
15+
*
16+
* 2. All copies of substantial portions of the Software may only be used in connection
17+
* with services provided by OneSignal.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#import "UIApplication+OneSignal.h"
29+
#import "OneSignalCommonDefines.h"
30+
31+
@implementation UIApplication (OneSignal)
32+
33+
+ (BOOL)applicationIsActive {
34+
if ([self isAppUsingUIScene]) {
35+
if (@available(iOS 13.0, *)) {
36+
UIWindow *keyWindow = UIApplication.sharedApplication.keyWindow;
37+
id windowScene = [keyWindow performSelector:@selector(windowScene)];
38+
id session = [windowScene performSelector:@selector(session)];
39+
id scene = [session performSelector:@selector(scene)];
40+
return [scene performSelector:@selector(activationState)] == 0;
41+
}
42+
}
43+
return [[UIApplication sharedApplication] applicationState] == UIApplicationStateActive;
44+
}
45+
46+
+ (BOOL)isAppUsingUIScene {
47+
if (@available(iOS 13.0, *)) {
48+
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"UIApplicationSceneManifest"] != nil;
49+
}
50+
return NO;
51+
}
52+
53+
@end

iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import <Foundation/Foundation.h>
2929
#import <XCTest/XCTest.h>
3030
#import "OneSignal.h"
31+
#import "UIApplication+OneSignal.h"
3132
#import "OneSignalNotificationCategoryController.h"
3233

3334
#define TEST_EXTERNAL_USER_ID @"i_am_a_test_external_user_id"

iOS_SDK/OneSignalSDK/UnitTests/UnitTestCommonMethods.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ + (void)initOneSignalAndThreadWait {
222222
+ (void)foregroundApp {
223223
UIApplicationOverrider.currentUIApplicationState = UIApplicationStateActive;
224224

225-
if ([OneSignalLifecycleObserver isAppUsingUIScene]) {
225+
if ([UIApplication isAppUsingUIScene]) {
226226
if (@available(iOS 13.0, *)) {
227227
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidActivateNotification object:nil];
228228
}
@@ -233,7 +233,7 @@ + (void)foregroundApp {
233233

234234
+ (void)backgroundApp {
235235
UIApplicationOverrider.currentUIApplicationState = UIApplicationStateBackground;
236-
if ([OneSignalLifecycleObserver isAppUsingUIScene]) {
236+
if ([UIApplication isAppUsingUIScene]) {
237237
if (@available(iOS 13.0, *)) {
238238
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneWillDeactivateNotification object:nil];
239239
[[NSNotificationCenter defaultCenter] postNotificationName:UISceneDidEnterBackgroundNotification object:nil];

0 commit comments

Comments
 (0)