Skip to content

Commit e874c5b

Browse files
authored
fix: storage not working on tvOS (#316)
* fix: storage not working on TvOS * fix: maxBatchSize and flushIntervalSeconds defaults * fix: refactor library path * fix: feedback * chore: revert changes for UserDefaults
1 parent 8cac126 commit e874c5b

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
## Next
22

3+
- fix: disk storage not working on tvOS ([#316](https://github.com/PostHog/posthog-ios/pull/316))
34
- fix: wrong is_identified fallback value ([#317](https://github.com/PostHog/posthog-ios/pull/317))
45

56
## 3.20.0 - 2025-03-04

PostHog/PostHogConfig.swift

+16-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77
import Foundation
88

99
@objc(PostHogConfig) public class PostHogConfig: NSObject {
10+
enum Defaults {
11+
#if os(tvOS)
12+
static let flushAt: Int = 5
13+
static let maxQueueSize: Int = 100
14+
#else
15+
static let flushAt: Int = 20
16+
static let maxQueueSize: Int = 1000
17+
#endif
18+
static let maxBatchSize: Int = 50
19+
static let flushIntervalSeconds: TimeInterval = 30
20+
}
21+
1022
@objc(PostHogDataMode) public enum PostHogDataMode: Int {
1123
case wifi
1224
case cellular
@@ -15,10 +27,10 @@ import Foundation
1527

1628
@objc public let host: URL
1729
@objc public let apiKey: String
18-
@objc public var flushAt: Int = 20
19-
@objc public var maxQueueSize: Int = 1000
20-
@objc public var maxBatchSize: Int = 50
21-
@objc public var flushIntervalSeconds: TimeInterval = 30
30+
@objc public var flushAt: Int = Defaults.flushAt
31+
@objc public var maxQueueSize: Int = Defaults.maxQueueSize
32+
@objc public var maxBatchSize: Int = Defaults.maxBatchSize
33+
@objc public var flushIntervalSeconds: TimeInterval = Defaults.flushIntervalSeconds
2234
@objc public var dataMode: PostHogDataMode = .any
2335
@objc public var sendFeatureFlagEvent: Bool = true
2436
@objc public var preloadFeatureFlags: Bool = true

PostHog/PostHogStorage.swift

+22-4
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@ import Foundation
1010
/**
1111
# Storage
1212

13-
posthog-ios stores data either to file or to UserDefaults in order to support tvOS. As recordings won't work on tvOS anyways and we have no tvOS users so far,
14-
we are opting to only support iOS via File storage.
13+
Note for tvOS:
14+
As tvOS restricts access to persisted Application Support directory, we use Library/Caches instead for storage
15+
16+
If needed, we can use UserDefaults for lightweight data - according to Apple, you can use UserDefaults to persist up to 500KB of data on tvOS
17+
see: https://developer.apple.com/forums/thread/16967?answerId=50696022#50696022
1518
*/
1619
func applicationSupportDirectoryURL() -> URL {
17-
let url = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
20+
#if os(tvOS)
21+
// tvOS restricts access to Application Support directory on physical devices
22+
// Use Library/Caches directory which may have less frequent eviction behavior than temp (which is purged when the app quits)
23+
let searchPath: FileManager.SearchPathDirectory = .cachesDirectory
24+
#else
25+
let searchPath: FileManager.SearchPathDirectory = .applicationSupportDirectory
26+
#endif
27+
28+
let url = FileManager.default.urls(for: searchPath, in: .userDomainMask).first!
1829
let bundleIdentifier = getBundleIdentifier()
1930

2031
return url.appendingPathComponent(bundleIdentifier)
@@ -38,9 +49,16 @@ func appGroupContainerUrl(config: PostHogConfig) -> URL? {
3849

3950
let bundleIdentifier = getBundleIdentifier()
4051

52+
#if os(tvOS)
53+
// tvOS: Due to stricter sandbox rules, creating "Application Support" directory is not possible on tvOS
54+
let librarySubPath = "Library/Caches/"
55+
#else
56+
let librarySubPath = "Library/Application Support/"
57+
#endif
58+
4159
let url = FileManager.default
4260
.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier)?
43-
.appendingPathComponent("Library/Application Support/")
61+
.appendingPathComponent(librarySubPath)
4462
.appendingPathComponent(bundleIdentifier)
4563

4664
if let url {

0 commit comments

Comments
 (0)