Skip to content

Commit

Permalink
Configuration: log warning if attempting to use observer mode with …
Browse files Browse the repository at this point in the history
…StoreKit 2

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 15, 2023
1 parent 461111b commit 5e7f906
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Sources/Logging/Strings/ConfigureStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ enum ConfigureStrings {

case observer_mode_enabled

case observer_mode_with_storekit2

case response_verification_mode(Signing.ResponseVerificationMode)

case delegate_set
Expand Down Expand Up @@ -98,6 +100,8 @@ extension ConfigureStrings: LogMessage {
return "StoreKit 2 support enabled"
case .observer_mode_enabled:
return "Purchases is configured in observer mode"
case .observer_mode_with_storekit2:
return "Observer mode is not currently compatible with StoreKit 2"
case let .response_verification_mode(mode):
switch mode {
case .disabled:
Expand Down
2 changes: 1 addition & 1 deletion Sources/Misc/Deprecations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ extension CustomerInfo {

public extension Configuration.Builder {

/// Set `usesStoreKit2IfAvailable`. If `true`, the SDK will use StoreKit 2 APIs internally. If disabled, it will use StoreKit 1 APIs instead.
/// Set `storeKit2Setting`. If `true`, the SDK will use StoreKit 2 APIs internally. If disabled, it will use StoreKit 1 APIs instead.
/// - Parameter usesStoreKit2IfAvailable: enable StoreKit 2 on devices that support it.
/// Defaults to `false`.
/// - Important: This configuration flag has been deprecated, and will be replaced by automatic remote configuration in the future.
Expand Down
13 changes: 11 additions & 2 deletions Sources/Purchasing/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import Foundation

private init(with builder: Builder) {
Self.verify(apiKey: builder.apiKey)
Self.verify(observerMode: builder.observerMode, storeKit2Setting: builder.storeKit2Setting)

self.apiKey = builder.apiKey
self.appUserID = builder.appUserID
Expand Down Expand Up @@ -129,12 +130,14 @@ 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`.
*
* - Warning: This assumes your IAP implementation uses StoreKit 1.
* Observer mode is not compatible with StoreKit 2.
*/
@objc public func with(observerMode: Bool) -> Builder {
@objc public func with(observerMode: Bool) -> Configuration.Builder {
self.observerMode = observerMode
return self
}

/**
* Set `userDefaults`.
* - Parameter userDefaults: Custom `UserDefaults` to use
Expand Down Expand Up @@ -289,6 +292,12 @@ extension Configuration {
}
}

fileprivate static func verify(observerMode: Bool, storeKit2Setting: StoreKit2Setting) {
if observerMode, storeKit2Setting.usesStoreKit2IfAvailable {
Logger.warn(Strings.configure.observer_mode_with_storekit2)
}
}

private static let applePlatformKeyPrefix: String = "appl_"

}
Expand Down
3 changes: 3 additions & 0 deletions Sources/Purchasing/Purchases/Purchases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,9 @@ public extension Purchases {
* RevenueCat's backend. Default is `false`.
*
* - Returns: An instantiated ``Purchases`` object that has been set as a singleton.
*
* - Warning: This assumes your IAP implementation uses StoreKit 1.
* Observer mode is not compatible with StoreKit 2.
*/
@objc(configureWithAPIKey:appUserID:observerMode:)
@discardableResult static func configure(withAPIKey apiKey: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,14 @@ func checkConfigurationAPI() {
.with(networkTimeout: 1)
.with(storeKit1Timeout: 1)
.with(platformInfo: Purchases.PlatformInfo(flavor: "", version: ""))
// Trusted Entitlements: internal until ready to be made public.
// .with(entitlementVerificationMode: .informational)
.build()

print(configuration)
}

@available(*, deprecated)
func checkDeprecatedConfiguration(_ builder: Configuration.Builder) {
_ = builder
.with(usesStoreKit2IfAvailable: true)
}
19 changes: 10 additions & 9 deletions Tests/APITesters/ObjCAPITester/ObjCAPITester/RCConfigurationAPI.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,16 @@ @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];
withObserverMode:false]
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 @@ -14,7 +14,7 @@ func checkConfigurationAPI() {
.with(apiKey: "")
.with(appUserID: "")
.with(appUserID: nil)
.with(observerMode: false)
.with(observerMode: true)
.with(userDefaults: UserDefaults.standard)
.with(dangerousSettings: DangerousSettings())
.with(dangerousSettings: DangerousSettings(autoSyncPurchases: true))
Expand All @@ -30,3 +30,9 @@ func checkConfigurationAPI() {
.build()
}
}

@available(*, deprecated)
func checkDeprecatedConfiguration(_ builder: Configuration.Builder) {
_ = builder
.with(usesStoreKit2IfAvailable: false)
}
46 changes: 46 additions & 0 deletions Tests/UnitTests/Purchasing/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,50 @@ class ConfigurationTests: TestCase {
expect(Configuration.validate(apiKey: "swRTCezdEzjnJSxdexDNJfcfiFrMXwqZ")) == .legacy
}

func testNoObserverModeWithStoreKit1() {
let configuration = Configuration.Builder(withAPIKey: "test").build()

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

self.logger.verifyMessageWasNotLogged(Strings.configure.observer_mode_with_storekit2)
}

@available(*, deprecated)
func testNoObserverModeWithStoreKit2() {
let configuration = Configuration.Builder(withAPIKey: "test")
.with(usesStoreKit2IfAvailable: true)
.build()

expect(configuration.observerMode) == false
expect(configuration.storeKit2Setting) == .enabledForCompatibleDevices

self.logger.verifyMessageWasNotLogged(Strings.configure.observer_mode_with_storekit2)
}

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

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

self.logger.verifyMessageWasNotLogged(Strings.configure.observer_mode_with_storekit2)
}

@available(*, deprecated)
func testObserverModeWithStoreKit2() {
let configuration = Configuration.Builder(withAPIKey: "test")
.with(observerMode: true)
.with(usesStoreKit2IfAvailable: true)
.build()

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

self.logger.verifyMessageWasLogged(Strings.configure.observer_mode_with_storekit2,
level: .warn)
}

}

0 comments on commit 5e7f906

Please sign in to comment.