diff --git a/CHANGELOG.md b/CHANGELOG.md index 98ab8c7426..028e39cf8f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ - [FEATURE] `DatadogTrace` now supports head-based sampling. See [#1794][] - [FEATURE] Support WebView recording in Session Replay. See [#1776][] +- [IMPROVEMENT] Add `isInitialized` and `stopInstance` methods to ObjC API. See [#1800][] - [IMPROVEMENT] Add `addUserExtraInfo` method to ObjC API. See [#1799][] # 2.10.0 / 23-04-2024 diff --git a/DatadogCore/Tests/DatadogObjc/DDDatadogTests.swift b/DatadogCore/Tests/DatadogObjc/DDDatadogTests.swift index edf7c6d112..40473a017a 100644 --- a/DatadogCore/Tests/DatadogObjc/DDDatadogTests.swift +++ b/DatadogCore/Tests/DatadogObjc/DDDatadogTests.swift @@ -24,7 +24,7 @@ class DDDatadogTests: XCTestCase { super.tearDown() } - // MARK: - Initializing with configuration + // MARK: - SDK initialization / stop lifecycle func testItForwardsInitializationToSwift() throws { let config = DDConfiguration( @@ -50,6 +50,49 @@ class DDDatadogTests: XCTestCase { XCTAssertNil(CoreRegistry.default.get(feature: LogsFeature.self)) } + func testItReflectsInitializationStatus() throws { + let config = DDConfiguration( + clientToken: "abcefghi", + env: "tests" + ) + + config.bundle = .mockWith(CFBundleExecutable: "app-name") + XCTAssertFalse(DDDatadog.isInitialized()) + + DDDatadog.initialize( + configuration: config, + trackingConsent: randomConsent().objc + ) + + XCTAssertTrue(DDDatadog.isInitialized()) + + Datadog.flushAndDeinitialize() + + XCTAssertNil(CoreRegistry.default.get(feature: LogsFeature.self)) + } + + func testItForwardsStopInstanceToSwift() throws { + let config = DDConfiguration( + clientToken: "abcefghi", + env: "tests" + ) + + config.bundle = .mockWith(CFBundleExecutable: "app-name") + + DDDatadog.initialize( + configuration: config, + trackingConsent: randomConsent().objc + ) + + XCTAssertTrue(Datadog.isInitialized()) + + DDDatadog.stopInstance() + + XCTAssertFalse(Datadog.isInitialized()) + + XCTAssertNil(CoreRegistry.default.get(feature: LogsFeature.self)) + } + // MARK: - Changing Tracking Consent func testItForwardsTrackingConsentToSwift() { diff --git a/DatadogCore/Tests/DatadogObjc/ObjcAPITests/DDDatadog+apiTests.m b/DatadogCore/Tests/DatadogObjc/ObjcAPITests/DDDatadog+apiTests.m index 81e074cbf0..1fd8d2c746 100644 --- a/DatadogCore/Tests/DatadogObjc/ObjcAPITests/DDDatadog+apiTests.m +++ b/DatadogCore/Tests/DatadogObjc/ObjcAPITests/DDDatadog+apiTests.m @@ -29,6 +29,8 @@ - (void)testDDDatadog { [DDDatadog initializeWithConfiguration:configuration trackingConsent:[DDTrackingConsent notGranted]]; + [DDDatadog isInitialized]; + DDSDKVerbosityLevel verbosity = [DDDatadog verbosityLevel]; [DDDatadog setVerbosityLevel:verbosity]; @@ -37,7 +39,7 @@ - (void)testDDDatadog { [DDDatadog setTrackingConsentWithConsent:[DDTrackingConsent notGranted]]; [DDDatadog clearAllData]; - [DDDatadog flushAndDeinitialize]; + [DDDatadog stopInstance]; } #pragma clang diagnostic pop diff --git a/DatadogObjc/Sources/Datadog+objc.swift b/DatadogObjc/Sources/Datadog+objc.swift index 89235d987f..da55a08285 100644 --- a/DatadogObjc/Sources/Datadog+objc.swift +++ b/DatadogObjc/Sources/Datadog+objc.swift @@ -80,6 +80,16 @@ public class DDDatadog: NSObject { Datadog.set(trackingConsent: consent.sdkConsent) } + @objc + public static func isInitialized() -> Bool { + return Datadog.isInitialized() + } + + @objc + public static func stopInstance() { + Datadog.stopInstance() + } + @objc public static func clearAllData() { Datadog.clearAllData() diff --git a/api-surface-objc b/api-surface-objc index 3e5ee14e91..bea8b31c61 100644 --- a/api-surface-objc +++ b/api-surface-objc @@ -27,6 +27,8 @@ public class DDDatadog: NSObject public static func setUserInfo(id: String? = nil, name: String? = nil, email: String? = nil, extraInfo: [String: Any] = [:]) public static func addUserExtraInfo(_ extraInfo: [String: Any]) public static func setTrackingConsent(consent: DDTrackingConsent) + public static func isInitialized() -> Bool + public static func stopInstance() public static func clearAllData() public class DDSite: NSObject public static func us1() -> DDSite