Skip to content

Commit 2eb5bf0

Browse files
fix(catalyst): simplify unsupported platform handling
Keep Catalyst behavior explicit while avoiding unnecessary resilient-storage and Live Activities branches. Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 2d7060b commit 2eb5bf0

8 files changed

Lines changed: 23 additions & 105 deletions

File tree

‎examples/demo/App/App.swift‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@
2727

2828
import SwiftUI
2929
import OneSignalFramework
30-
#if !targetEnvironment(macCatalyst)
31-
import OneSignalLiveActivities
32-
#endif
3330

3431
@main
3532
struct App: SwiftUI.App {
@@ -64,9 +61,11 @@ class AppDelegate: NSObject, UIApplicationDelegate {
6461
setupInAppMessageListeners()
6562

6663
// Set up Live Activities (iOS 16.1+)
64+
#if !targetEnvironment(macCatalyst)
6765
if #available(iOS 16.1, *) {
6866
LiveActivityController.setup()
6967
}
68+
#endif
7069

7170
return true
7271
}

‎examples/demo/App/Services/LiveActivityController.swift‎

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727

2828
import Foundation
2929
import OneSignalFramework
30-
#if !targetEnvironment(macCatalyst)
30+
#if targetEnvironment(macCatalyst)
31+
// Live Activities are unavailable on Mac Catalyst.
32+
#else
3133
import OneSignalLiveActivities
32-
#endif
3334

3435
/// Order tracking phases used by the Live Activity demo
3536
enum LiveActivityStatus: String, CaseIterable, Identifiable {
@@ -78,9 +79,7 @@ enum LiveActivityController {
7879

7980
@available(iOS 16.1, *)
8081
static func setup() {
81-
#if !targetEnvironment(macCatalyst)
8282
OneSignal.LiveActivities.setupDefault()
83-
#endif
8483
}
8584

8685
@available(iOS 16.1, *)
@@ -89,7 +88,6 @@ enum LiveActivityController {
8988
orderNumber: String,
9089
status: LiveActivityStatus
9190
) {
92-
#if !targetEnvironment(macCatalyst)
9391
let attributes: [String: Any] = [
9492
"orderNumber": orderNumber
9593
]
@@ -103,13 +101,9 @@ enum LiveActivityController {
103101
attributes: attributes,
104102
content: content
105103
)
106-
#endif
107104
}
108105

109106
static func update(appId: String, activityId: String, status: LiveActivityStatus) async -> Bool {
110-
#if targetEnvironment(macCatalyst)
111-
return false
112-
#else
113107
let payload: [String: Any] = [
114108
"event": "update",
115109
"name": "Live Activity Update",
@@ -123,13 +117,9 @@ enum LiveActivityController {
123117
]
124118
]
125119
return await postLiveActivity(appId: appId, activityId: activityId, payload: payload)
126-
#endif
127120
}
128121

129122
static func end(appId: String, activityId: String) async -> Bool {
130-
#if targetEnvironment(macCatalyst)
131-
return false
132-
#else
133123
let payload: [String: Any] = [
134124
"event": "end",
135125
"name": "End Live Activity",
@@ -140,16 +130,9 @@ enum LiveActivityController {
140130
]
141131
]
142132
return await postLiveActivity(appId: appId, activityId: activityId, payload: payload)
143-
#endif
144133
}
145134

146-
static var hasApiKey: Bool {
147-
#if targetEnvironment(macCatalyst)
148-
false
149-
#else
150-
SecretsConfig.hasApiKey
151-
#endif
152-
}
135+
static var hasApiKey: Bool { SecretsConfig.hasApiKey }
153136

154137
private static func postLiveActivity(appId: String, activityId: String, payload: [String: Any]) async -> Bool {
155138
guard let key = SecretsConfig.apiKey else { return false }
@@ -171,3 +154,4 @@ enum LiveActivityController {
171154
}
172155
}
173156
}
157+
#endif

‎examples/demo/App/ViewModels/OneSignalViewModel.swift‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ final class OneSignalViewModel: ObservableObject {
389389

390390
// MARK: - Live Activities
391391

392+
#if !targetEnvironment(macCatalyst)
392393
func startLiveActivity(activityId: String, orderNumber: String, status: LiveActivityStatus) {
393394
let trimmedId = activityId.trimmingCharacters(in: .whitespacesAndNewlines)
394395
guard !trimmedId.isEmpty else { return }
@@ -423,6 +424,7 @@ final class OneSignalViewModel: ObservableObject {
423424
)
424425
}
425426
}
427+
#endif
426428

427429
// MARK: - Tooltips
428430

‎examples/demo/App/Views/Sections/LiveActivitySection.swift‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727

2828
import SwiftUI
2929

30+
#if targetEnvironment(macCatalyst)
31+
// Live Activities are unavailable on Mac Catalyst.
32+
#else
3033
/// Live Activities (iOS 16.1+) section with activity ID + order # inputs and status cycler.
3134
/// Mirrors the Capacitor demo's LiveActivitySection.
3235
struct LiveActivitySection: View {
@@ -142,3 +145,4 @@ struct LiveActivitySection: View {
142145
}
143146
}
144147
}
148+
#endif

‎examples/demo/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ xcodegen generate # rewrites App.xcodeproj
9292

9393
### 3. Capabilities & App Group
9494

95-
The shipped `App.entitlements` and `OneSignalNotificationServiceExtension/OneSignalNotificationServiceExtension.entitlements` use `group.com.onesignal.example.onesignal`. If you need a different group (for example to install on a real device under your own team), change the value in both files to the same string. The other capabilities (Push Notifications, Remote notifications background mode, `NSSupportsLiveActivities`) are already declared in the entitlements / `App/Info.plist`.
95+
The shipped `App.entitlements` and `OneSignalNotificationServiceExtension/OneSignalNotificationServiceExtension.entitlements` use `group.com.onesignal.example.onesignal`. Both targets also set `OneSignal_app_groups_key` to this value because Catalyst derives a `maccatalyst.*` bundle identifier that would otherwise produce a different default group name. If you need a different group (for example to install on a real device under your own team), change the value in both entitlements and both Info.plists to the same string. The other capabilities (Push Notifications, Remote notifications background mode, `NSSupportsLiveActivities`) are already declared in the entitlements / `App/Info.plist`.
9696

9797
### 4. Configure your OneSignal credentials
9898

‎iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSResilientStorage.swift‎

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -71,61 +71,24 @@ public final class OSResilientStorage: NSObject {
7171
let fileManager = FileManager.default
7272

7373
let groupName = OneSignalUserDefaults.appGroupName()
74-
let groupContainer = fileManager.containerURL(
75-
forSecurityApplicationGroupIdentifier: groupName
76-
)
77-
return resolveFileURL(
78-
groupContainer: groupContainer,
79-
applicationSupportDirectory: {
80-
try fileManager.url(
81-
for: .applicationSupportDirectory,
82-
in: .userDomainMask,
83-
appropriateFor: nil,
84-
create: true
85-
)
86-
},
87-
prepare: {
88-
try preparedFileURL(in: $0, fileManager: fileManager)
89-
}
90-
)
91-
}
92-
93-
static func resolveFileURL(
94-
groupContainer: URL?,
95-
applicationSupportDirectory: () throws -> URL,
96-
prepare: (URL) throws -> URL
97-
) -> URL? {
98-
if let groupContainer {
99-
do {
100-
return try prepare(groupContainer)
101-
} catch {
102-
OneSignalLog.onesignalLog(
103-
.LL_ERROR,
104-
message: "OSResilientStorage could not prepare the App Group container: \(error)"
105-
)
106-
return nil
107-
}
74+
if let container = fileManager.containerURL(forSecurityApplicationGroupIdentifier: groupName) {
75+
return container.appendingPathComponent(fileName)
10876
}
10977

11078
do {
111-
return try prepare(applicationSupportDirectory())
79+
let support = try fileManager.url(
80+
for: .applicationSupportDirectory,
81+
in: .userDomainMask,
82+
appropriateFor: nil,
83+
create: true
84+
)
85+
return support.appendingPathComponent(fileName)
11286
} catch {
11387
OneSignalLog.onesignalLog(.LL_ERROR, message: "OSResilientStorage could not resolve a container URL: \(error)")
11488
return nil
11589
}
11690
}
11791

118-
static func preparedFileURL(
119-
in directory: URL,
120-
fileManager: FileManager = .default
121-
) throws -> URL {
122-
try fileManager.createDirectory(
123-
at: directory,
124-
withIntermediateDirectories: true
125-
)
126-
return directory.appendingPathComponent(fileName)
127-
}
128-
12992
/// Reads the cache file. Caller is responsible for queue-serialization.
13093
/// Returns an empty dict if the file is missing or unreadable.
13194
private static func loadUnsafe() -> [String: String] {

‎iOS_SDK/OneSignalSDK/OneSignalOSCore/Source/OSStubLiveActivities.swift‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ public class OSStubLiveActivities: NSObject, OSLiveActivities {
3737
}
3838

3939
private static func logUnavailable() {
40-
#if !targetEnvironment(macCatalyst)
4140
let message = "OneSignalLiveActivities not found. In order to use OneSignal's " +
4241
"LiveActivities features the OneSignalLiveActivities module must be added."
4342
OneSignalLog.onesignalLog(.LL_ERROR, message: message)
44-
#endif
4543
}
4644
}

‎iOS_SDK/OneSignalSDK/OneSignalOSCoreTests/OSResilientStorageTests.swift‎

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ import XCTest
3131

3232
final class OSResilientStorageTests: XCTestCase {
3333

34-
private enum TestError: Error {
35-
case preparationFailed
36-
}
37-
3834
/// Test keys. We avoid the real key constants so collisions with anything written by
3935
/// other tests / fixtures during the same simulator session can't bleed into assertions.
4036
private let keyA = "test_key_a"
@@ -68,34 +64,6 @@ final class OSResilientStorageTests: XCTestCase {
6864
XCTAssertNil(OSResilientStorage.string(forKey: "never_written_\(UUID().uuidString)"))
6965
}
7066

71-
func testPreparedFileURL_createsMissingContainerForAtomicWrites() throws {
72-
let root = FileManager.default.temporaryDirectory
73-
.appendingPathComponent(UUID().uuidString, isDirectory: true)
74-
let container = root.appendingPathComponent("group.example", isDirectory: true)
75-
defer { try? FileManager.default.removeItem(at: root) }
76-
77-
let fileURL = try OSResilientStorage.preparedFileURL(in: container)
78-
try Data("value".utf8).write(to: fileURL, options: .atomic)
79-
80-
XCTAssertTrue(FileManager.default.fileExists(atPath: fileURL.path))
81-
}
82-
83-
func testResolveFileURL_doesNotFallBackWhenGroupPreparationFails() {
84-
var applicationSupportWasRequested = false
85-
86-
let fileURL = OSResilientStorage.resolveFileURL(
87-
groupContainer: URL(fileURLWithPath: "/group"),
88-
applicationSupportDirectory: {
89-
applicationSupportWasRequested = true
90-
return URL(fileURLWithPath: "/application-support")
91-
},
92-
prepare: { _ in throw TestError.preparationFailed }
93-
)
94-
95-
XCTAssertNil(fileURL)
96-
XCTAssertFalse(applicationSupportWasRequested)
97-
}
98-
9967
func testSetWithNil_removesKey() {
10068
OSResilientStorage.setString("alpha", forKey: keyA)
10169
XCTAssertEqual(OSResilientStorage.string(forKey: keyA), "alpha")

0 commit comments

Comments
 (0)