Skip to content

Commit 21ed813

Browse files
committed
refactor: route identifier reads through OneSignalIdentifiers
Add OneSignalIdentifiers in OneSignalOSCore with storedAppId and storedSubscriptionId accessors backed by shared UserDefaults. Migrate six call sites; drop the now-dead OSUD_APP_ID write to initStandard.
1 parent ae58bad commit 21ed813

7 files changed

Lines changed: 99 additions & 29 deletions

File tree

iOS_SDK/OneSignalSDK/OneSignal.xcodeproj/project.pbxproj

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@
9494
3C5501402E09CF0100E77DF7 /* OSCopyOnWriteSet.h in Headers */ = {isa = PBXBuildFile; fileRef = 3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */; };
9595
3C5501412E09CF0100E77DF7 /* OSCopyOnWriteSet.m in Sources */ = {isa = PBXBuildFile; fileRef = 3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */; };
9696
3C5501432E09F3D900E77DF7 /* LoggingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5501422E09F3D900E77DF7 /* LoggingTests.swift */; };
97+
3C5C6FFC2FCB8DED00102E2C /* OneSignalIdentifiers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3C5C6FFB2FCB8DED00102E2C /* OneSignalIdentifiers.swift */; };
98+
3C5C6FFD2FCB933100102E2C /* OneSignalOSCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3C115161289A259500565C41 /* OneSignalOSCore.framework */; };
99+
3C5C70022FCB935000102E2C /* OneSignalOSCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 3C115161289A259500565C41 /* OneSignalOSCore.framework */; };
97100
3C60BB9B2ECF860600C765F7 /* OneSignalInAppMessages.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DEBAAE282A4211D900BF2C1C /* OneSignalInAppMessages.framework */; };
98101
3C60BB9C2ECF860600C765F7 /* OneSignalInAppMessages.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DEBAAE282A4211D900BF2C1C /* OneSignalInAppMessages.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; };
99102
3C62999F2BEEA34800649187 /* PrivacyInfo.xcprivacy in Resources */ = {isa = PBXBuildFile; fileRef = 3C62999E2BEEA34800649187 /* PrivacyInfo.xcprivacy */; };
@@ -665,6 +668,20 @@
665668
remoteGlobalIDString = 3C115160289A259500565C41;
666669
remoteInfo = OneSignalOSCore;
667670
};
671+
3C5C6FFF2FCB933100102E2C /* PBXContainerItemProxy */ = {
672+
isa = PBXContainerItemProxy;
673+
containerPortal = 37747F8B19147D6400558FAD /* Project object */;
674+
proxyType = 1;
675+
remoteGlobalIDString = 3C115160289A259500565C41;
676+
remoteInfo = OneSignalOSCore;
677+
};
678+
3C5C70042FCB935000102E2C /* PBXContainerItemProxy */ = {
679+
isa = PBXContainerItemProxy;
680+
containerPortal = 37747F8B19147D6400558FAD /* Project object */;
681+
proxyType = 1;
682+
remoteGlobalIDString = 3C115160289A259500565C41;
683+
remoteInfo = OneSignalOSCore;
684+
};
668685
3C60BB9D2ECF860600C765F7 /* PBXContainerItemProxy */ = {
669686
isa = PBXContainerItemProxy;
670687
containerPortal = 37747F8B19147D6400558FAD /* Project object */;
@@ -1330,6 +1347,7 @@
13301347
3C55013E2E09CF0100E77DF7 /* OSCopyOnWriteSet.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = OSCopyOnWriteSet.h; sourceTree = "<group>"; };
13311348
3C55013F2E09CF0100E77DF7 /* OSCopyOnWriteSet.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = OSCopyOnWriteSet.m; sourceTree = "<group>"; };
13321349
3C5501422E09F3D900E77DF7 /* LoggingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoggingTests.swift; sourceTree = "<group>"; };
1350+
3C5C6FFB2FCB8DED00102E2C /* OneSignalIdentifiers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OneSignalIdentifiers.swift; sourceTree = "<group>"; };
13331351
3C62999E2BEEA34800649187 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
13341352
3C6299A02BEEA38100649187 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
13351353
3C6299A22BEEA3CC00649187 /* PrivacyInfo.xcprivacy */ = {isa = PBXFileReference; lastKnownFileType = text.xml; path = PrivacyInfo.xcprivacy; sourceTree = "<group>"; };
@@ -1964,6 +1982,7 @@
19641982
isa = PBXFrameworksBuildPhase;
19651983
buildActionMask = 2147483647;
19661984
files = (
1985+
3C5C70022FCB935000102E2C /* OneSignalOSCore.framework in Frameworks */,
19671986
DE7D1846270286C6002D3A5D /* OneSignalCore.framework in Frameworks */,
19681987
DE7D18D22703ADE0002D3A5D /* OneSignalOutcomes.framework in Frameworks */,
19691988
DE7D1843270283B9002D3A5D /* UserNotifications.framework in Frameworks */,
@@ -2048,6 +2067,7 @@
20482067
DEF7845F2912EA0D00A1F3A5 /* UserNotifications.framework in Frameworks */,
20492068
DEF784612912F5E100A1F3A5 /* UIKit.framework in Frameworks */,
20502069
DEF784422912E16F00A1F3A5 /* OneSignalCore.framework in Frameworks */,
2070+
3C5C6FFD2FCB933100102E2C /* OneSignalOSCore.framework in Frameworks */,
20512071
DE2D8F4A2947D86200844084 /* OneSignalOutcomes.framework in Frameworks */,
20522072
DE2D8F452947D85800844084 /* OneSignalExtension.framework in Frameworks */,
20532073
);
@@ -2203,6 +2223,7 @@
22032223
5BC1DE652C90BC9F00CA8807 /* Consistency */,
22042224
3C115163289A259500565C41 /* OneSignalOSCore.h */,
22052225
3C115188289ADEA300565C41 /* OSModelStore.swift */,
2226+
3C5C6FFB2FCB8DED00102E2C /* OneSignalIdentifiers.swift */,
22062227
3C115186289ADE7700565C41 /* OSModelStoreListener.swift */,
22072228
3C115184289ADE4F00565C41 /* OSModel.swift */,
22082229
3CF1A5622C669EA40056B3AA /* OSNewRecordsState.swift */,
@@ -3756,6 +3777,7 @@
37563777
dependencies = (
37573778
DE7D181B27026BEC002D3A5D /* PBXTargetDependency */,
37583779
DE7D18D52703ADE0002D3A5D /* PBXTargetDependency */,
3780+
3C5C70052FCB935000102E2C /* PBXTargetDependency */,
37593781
);
37603782
name = OneSignalExtension;
37613783
productName = OneSignalExtension;
@@ -3888,6 +3910,7 @@
38883910
DEF784452912E16F00A1F3A5 /* PBXTargetDependency */,
38893911
DE2D8F482947D85800844084 /* PBXTargetDependency */,
38903912
DE2D8F4D2947D86200844084 /* PBXTargetDependency */,
3913+
3C5C70002FCB933100102E2C /* PBXTargetDependency */,
38913914
);
38923915
name = OneSignalNotifications;
38933916
productName = OneSignalNotifications;
@@ -4350,6 +4373,7 @@
43504373
3C115165289A259500565C41 /* OneSignalOSCore.docc in Sources */,
43514374
5BC1DE5E2C90B80E00CA8807 /* OSCondition.swift in Sources */,
43524375
5BC1DE5C2C90B7E600CA8807 /* OSConsistencyManager.swift in Sources */,
4376+
3C5C6FFC2FCB8DED00102E2C /* OneSignalIdentifiers.swift in Sources */,
43534377
3C115189289ADEA300565C41 /* OSModelStore.swift in Sources */,
43544378
3C115185289ADE4F00565C41 /* OSModel.swift in Sources */,
43554379
3CF1A5632C669EA40056B3AA /* OSNewRecordsState.swift in Sources */,
@@ -4795,6 +4819,16 @@
47954819
target = 3C115160289A259500565C41 /* OneSignalOSCore */;
47964820
targetProxy = 3C115199289AF86C00565C41 /* PBXContainerItemProxy */;
47974821
};
4822+
3C5C70002FCB933100102E2C /* PBXTargetDependency */ = {
4823+
isa = PBXTargetDependency;
4824+
target = 3C115160289A259500565C41 /* OneSignalOSCore */;
4825+
targetProxy = 3C5C6FFF2FCB933100102E2C /* PBXContainerItemProxy */;
4826+
};
4827+
3C5C70052FCB935000102E2C /* PBXTargetDependency */ = {
4828+
isa = PBXTargetDependency;
4829+
target = 3C115160289A259500565C41 /* OneSignalOSCore */;
4830+
targetProxy = 3C5C70042FCB935000102E2C /* PBXContainerItemProxy */;
4831+
};
47984832
3C60BB9E2ECF860600C765F7 /* PBXTargetDependency */ = {
47994833
isa = PBXTargetDependency;
48004834
target = DEBAAE272A4211D900BF2C1C /* OneSignalInAppMessages */;

iOS_SDK/OneSignalSDK/OneSignalExtension/OneSignalNotificationServiceExtensionHandler.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#import <OneSignalCore/OneSignalCore.h>
29+
#import <OneSignalOSCore/OneSignalOSCore-Swift.h>
2930
#import "OSMacros.h"
3031
#import <OneSignalOutcomes/OneSignalOutcomes.h>
3132
#import "OneSignalNotificationServiceExtensionHandler.h"
@@ -140,9 +141,8 @@ + (void)onNotificationReceived:(NSString *)receivedNotificationId withBlockingTa
140141
[[OSSessionManager sharedSessionManager] onNotificationReceived:receivedNotificationId];
141142

142143
// Track confirmed delivery
143-
let sharedUserDefaults = OneSignalUserDefaults.initShared;
144-
let playerId = [sharedUserDefaults getSavedStringForKey:OSUD_PUSH_SUBSCRIPTION_ID defaultValue:nil];
145-
let appId = [sharedUserDefaults getSavedStringForKey:OSUD_APP_ID defaultValue:nil];
144+
NSString *playerId = OneSignalIdentifiers.subscriptionId;
145+
NSString *appId = OneSignalIdentifiers.storedAppId;
146146
// Randomize send of confirmed deliveries to lessen traffic for high recipient notifications
147147
int randomDelay = semaphore != nil ? arc4random_uniform(MAX_CONF_DELIVERY_DELAY) : 0;
148148
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"OneSignal onNotificationReceived sendReceiveReceipt with delay: %i", randomDelay]];

iOS_SDK/OneSignalSDK/OneSignalExtension/OneSignalReceiveReceiptsController.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#import <Foundation/Foundation.h>
2929
#import "OneSignalReceiveReceiptsController.h"
3030
#import <OneSignalCore/OneSignalCore.h>
31+
#import <OneSignalOSCore/OneSignalOSCore-Swift.h>
3132
#import "OSMacros.h"
3233
#import "OneSignalExtensionRequests.h"
3334

@@ -38,9 +39,8 @@ - (BOOL)isReceiveReceiptsEnabled {
3839
}
3940

4041
- (void)sendReceiveReceiptWithNotificationId:(NSString *)notificationId {
41-
let sharedUserDefaults = OneSignalUserDefaults.initShared;
42-
let playerId = [sharedUserDefaults getSavedStringForKey:OSUD_PUSH_SUBSCRIPTION_ID defaultValue:nil];
43-
let appId = [sharedUserDefaults getSavedStringForKey:OSUD_APP_ID defaultValue:nil];
42+
NSString *playerId = OneSignalIdentifiers.subscriptionId;
43+
NSString *appId = OneSignalIdentifiers.storedAppId;
4444

4545
[self sendReceiveReceiptWithPlayerId:playerId
4646
notificationId:notificationId

iOS_SDK/OneSignalSDK/OneSignalNotifications/OSNotificationsManager.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ of this software and associated documentation files (the "Software"), to deal
2727

2828
#import "OSNotificationsManager.h"
2929
#import <OneSignalCore/OneSignalCore.h>
30+
#import <OneSignalOSCore/OneSignalOSCore-Swift.h>
3031
#import "OSMacros.h"
3132
#import <UIKit/UIKit.h>
3233
#import <UserNotifications/UserNotifications.h>
@@ -234,7 +235,7 @@ + (NSString*)pushToken {
234235
static NSString *_pushSubscriptionId;
235236
+ (NSString*)pushSubscriptionId {
236237
if (!_pushSubscriptionId) {
237-
_pushSubscriptionId = [OneSignalUserDefaults.initShared getSavedStringForKey:OSUD_PUSH_SUBSCRIPTION_ID defaultValue:nil];
238+
_pushSubscriptionId = OneSignalIdentifiers.subscriptionId;
238239
}
239240
return _pushSubscriptionId;
240241
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Modified MIT License
3+
4+
Copyright 2026 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 Foundation
29+
import OneSignalCore
30+
31+
/// Durable accessors for OneSignal identifiers persisted to shared UserDefaults.
32+
/// Useful when the in-memory source isn't available, notably the NSE process
33+
///
34+
/// Callers with access to the in-memory current value (main-app SDK code)
35+
/// should continue to use that source
36+
@objc(OneSignalIdentifiers)
37+
public final class OneSignalIdentifiers: NSObject {
38+
39+
/// Last-known persisted `app_id` from shared UserDefaults. Returns nil if absent.
40+
@objc public static var storedAppId: String? {
41+
return OneSignalUserDefaults.initShared().getSavedString(forKey: OSUD_APP_ID, defaultValue: nil)
42+
}
43+
44+
/// Persisted push `subscription_id` from shared UserDefaults. Returns nil if absent.
45+
@objc public static var subscriptionId: String? {
46+
return OneSignalUserDefaults.initShared().getSavedString(forKey: OSUD_PUSH_SUBSCRIPTION_ID, defaultValue: nil)
47+
}
48+
}

iOS_SDK/OneSignalSDK/OneSignalUser/Source/OneSignalUserManagerImpl.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ public class OneSignalUserManagerImpl: NSObject, OneSignalUserManager {
500500
let sharedUserDefaults = OneSignalUserDefaults.initShared()
501501
let reachable = OSNotificationsManager.currentPermissionState.reachable
502502
let token = sharedUserDefaults.getSavedString(forKey: OSUD_PUSH_TOKEN, defaultValue: nil)
503-
let subscriptionId = subscriptionId ?? sharedUserDefaults.getSavedString(forKey: OSUD_PUSH_SUBSCRIPTION_ID, defaultValue: nil)
503+
let subscriptionId = subscriptionId ?? OneSignalIdentifiers.subscriptionId
504504

505505
return OSSubscriptionModel(type: .push,
506506
address: token,

iOS_SDK/OneSignalSDK/Source/OneSignal.m

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727

2828
#import "OneSignalFramework.h"
29+
#import <OneSignalOSCore/OneSignalOSCore-Swift.h>
2930
#import "OneSignalInternal.h"
3031
#import "OneSignalTracker.h"
3132
#import "OneSignalTrackIAP.h"
@@ -236,17 +237,6 @@ + (void)initialize:(nonnull NSString*)newAppId withLaunchOptions:(nullable NSDic
236237
[self init];
237238
}
238239

239-
+ (NSString * _Nullable)getCachedAppId {
240-
let prevAppId = [OneSignalUserDefaults.initStandard getSavedStringForKey:OSUD_APP_ID defaultValue:nil];
241-
if (!prevAppId) {
242-
[OneSignalLog onesignalLog:ONE_S_LL_INFO message:@"Waiting for setAppId(appId) with a valid appId to complete OneSignal init!"];
243-
} else {
244-
let logMessage = [NSString stringWithFormat:@"Initializing OneSignal with cached appId: '%@'.", prevAppId];
245-
[OneSignalLog onesignalLog:ONE_S_LL_INFO message:logMessage];
246-
}
247-
return prevAppId;
248-
}
249-
250240
/*
251241
1/2 steps in OneSignal init, relying on setLaunchOptions (usage order does not matter)
252242
Sets the app id OneSignal should use in the application
@@ -257,10 +247,12 @@ + (void)setAppId:(nullable NSString*)newAppId {
257247
[OneSignalLog onesignalLog:ONE_S_LL_VERBOSE message:[NSString stringWithFormat:@"setAppId called with appId: %@!", newAppId]];
258248

259249
if (!newAppId || newAppId.length == 0) {
260-
NSString* cachedAppId = [self getCachedAppId];
250+
NSString* cachedAppId = OneSignalIdentifiers.storedAppId;
261251
if (cachedAppId) {
252+
[OneSignalLog onesignalLog:ONE_S_LL_INFO message:[NSString stringWithFormat:@"Initializing OneSignal with cached appId: '%@'.", cachedAppId]];
262253
[OneSignalConfigManager setAppId:cachedAppId];
263254
} else {
255+
[OneSignalLog onesignalLog:ONE_S_LL_INFO message:@"Waiting for setAppId(appId) with a valid appId to complete OneSignal init!"];
264256
return;
265257
}
266258
} else if ([OneSignalConfigManager getAppId] && ![newAppId isEqualToString:[OneSignalConfigManager getAppId]]) {
@@ -533,7 +525,7 @@ + (void)handleAppIdChange:(NSString*)appId {
533525
}
534526

535527
let standardUserDefaults = OneSignalUserDefaults.initStandard;
536-
let prevAppId = [standardUserDefaults getSavedStringForKey:OSUD_APP_ID defaultValue:nil];
528+
NSString *prevAppId = OneSignalIdentifiers.storedAppId;
537529

538530
// Handle changes to the app id, this might happen on a developer's device when testing
539531
// Will also run the first time OneSignal is initialized
@@ -543,22 +535,17 @@ + (void)handleAppIdChange:(NSString*)appId {
543535
_didCallDownloadParameters = false;
544536

545537
let sharedUserDefaults = OneSignalUserDefaults.initShared;
546-
547-
[standardUserDefaults saveStringForKey:OSUD_APP_ID withValue:appId];
548-
538+
549539
// Remove player_id from both standard and shared NSUserDefaults
550540
[standardUserDefaults removeValueForKey:OSUD_PUSH_SUBSCRIPTION_ID];
551541
[sharedUserDefaults removeValueForKey:OSUD_PUSH_SUBSCRIPTION_ID];
552542
[standardUserDefaults removeValueForKey:OSUD_LEGACY_PLAYER_ID];
553543
[sharedUserDefaults removeValueForKey:OSUD_LEGACY_PLAYER_ID];
554-
544+
555545
// Clear all cached data, does not start User Module nor call logout.
556546
[OneSignalUserManagerImpl.sharedInstance clearAllModelsFromStores];
557547
}
558-
559-
// Always save appId and player_id as it will not be present on shared if:
560-
// - Updating from an older SDK
561-
// - Updating to an app that didn't have App Groups setup before
548+
562549
[OneSignalUserDefaults.initShared saveStringForKey:OSUD_APP_ID withValue:appId];
563550
}
564551

0 commit comments

Comments
 (0)