Skip to content

Commit

Permalink
Configuration: added observerMode overload that allows configurin…
Browse files Browse the repository at this point in the history
…g SK2 setting

See also RevenueCat/revenuecat-docs#299.

Since #3032 developers using observer mode need to configure the SDK with the correct StoreKit 2 setting.
This new API makes that explicit, while leaving `with(usesStoreKit2IfAvailable:)` still deprecated.
  • Loading branch information
NachoSoto committed Sep 14, 2023
1 parent e100bd3 commit 5487dfb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 10 deletions.
14 changes: 14 additions & 0 deletions Sources/Purchasing/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,23 @@ import Foundation
* Set `observerMode`.
* - Parameter observerMode: Set this to `true` if you have your own IAP implementation and want to use only
* RevenueCat's backend. Default is `false`.
*
* - Note: This assumes your IAP implementation uses StoreKit 1.
* If you use StoreKit 2, use ``with(observerMode:storeKit2:)`` instead.
*/
@objc public func with(observerMode: Bool) -> Builder {
return self.with(observerMode: observerMode, storeKit2: false)
}

/**
* Set `observerMode` with a corresponding `StoreKit 2` setting.
* - Parameter observerMode: Set this to `true` if you have your own IAP implementation and want to use only
* RevenueCat's backend. Default is `false`.
* - Parameter storeKit2: Set this to `true` if your own IAP implementation uses StoreKit 2. Default is `false`.
*/
@objc public func with(observerMode: Bool, storeKit2: Bool) -> Builder {
self.observerMode = observerMode
self.storeKit2Setting = .init(useStoreKit2IfAvailable: storeKit2)
return self
}

Expand Down
21 changes: 11 additions & 10 deletions Tests/APITesters/ObjCAPITester/ObjCAPITester/RCConfigurationAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ @implementation RCConfigurationAPI

+ (void)checkAPI {
RCConfigurationBuilder *builder = [RCConfiguration builderWithAPIKey:@""];
RCConfiguration *config __unused = [[[[[[[[[[[builder withApiKey:@""]
withObserverMode:false]
withUserDefaults:NSUserDefaults.standardUserDefaults]
withAppUserID:@""]
withAppUserID:nil]
withDangerousSettings:[[RCDangerousSettings alloc] init]]
withNetworkTimeout:1]
withStoreKit1Timeout: 1]
withPlatformInfo:[[RCPlatformInfo alloc] initWithFlavor:@"" version:@""]]
withUsesStoreKit2IfAvailable:false] build];
RCConfiguration *config __unused = [[[[[[[[[[[[builder withApiKey:@""]
withObserverMode:false]
withObserverMode:true storeKit2:true]
withUserDefaults:NSUserDefaults.standardUserDefaults]
withAppUserID:@""]
withAppUserID:nil]
withDangerousSettings:[[RCDangerousSettings alloc] init]]
withNetworkTimeout:1]
withStoreKit1Timeout: 1]
withPlatformInfo:[[RCPlatformInfo alloc] initWithFlavor:@"" version:@""]]
withUsesStoreKit2IfAvailable:false] build];

if (@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *)) {
RCConfiguration *config __unused = [[builder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func checkConfigurationAPI() {
.with(appUserID: "")
.with(appUserID: nil)
.with(observerMode: false)
.with(observerMode: false, storeKit2: true)
.with(userDefaults: UserDefaults.standard)
.with(dangerousSettings: DangerousSettings())
.with(dangerousSettings: DangerousSettings(autoSyncPurchases: true))
Expand Down
29 changes: 29 additions & 0 deletions Tests/UnitTests/Purchasing/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,33 @@ class ConfigurationTests: TestCase {
expect(Configuration.validate(apiKey: "swRTCezdEzjnJSxdexDNJfcfiFrMXwqZ")) == .legacy
}

func testObserverModeDefaultsToStoreKit1() {
let observerMode = Bool.random()

let configuration = Configuration.Builder(withAPIKey: "test")
.with(observerMode: observerMode)
.build()

expect(configuration.observerMode) == observerMode
expect(configuration.storeKit2Setting) == .enabledOnlyForOptimizations
}

func testObserverModeWithStoreKit2Disabled() {
let configuration = Configuration.Builder(withAPIKey: "test")
.with(observerMode: true, storeKit2: false)
.build()

expect(configuration.observerMode) == true
expect(configuration.storeKit2Setting) == .enabledOnlyForOptimizations
}

func testObserverModeWithStoreKit2Enabled() {
let configuration = Configuration.Builder(withAPIKey: "test")
.with(observerMode: true, storeKit2: true)
.build()

expect(configuration.observerMode) == true
expect(configuration.storeKit2Setting) == .enabledForCompatibleDevices
}

}

0 comments on commit 5487dfb

Please sign in to comment.