Skip to content

Commit

Permalink
Mark methods with StaticString for appUserID as deprecated (#3739)
Browse files Browse the repository at this point in the history
I tried conditionally compiling the `availability` attribute, and
marking it as `unavailable` in `RELEASE` builds, but the compiler is
smart enough to ignore the method and choose the disfavored overload in
that case.

I also tried conditionally compiling the return type in the same way, to
cause a compilation error in `RELEASE` builds, but the compiler is again
smart enough to choose the disfavored overload.
  • Loading branch information
MarkVillacampa authored Feb 29, 2024
1 parent 540ce81 commit da11a79
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 8 deletions.
5 changes: 5 additions & 0 deletions Sources/Purchasing/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ import Foundation
return self
}

@available(*, deprecated, message: """
The appUserID passed to logIn is a constant string known at compile time.
This is likely a programmer error. This ID is used to identify the current user.
See https://docs.revenuecat.com/docs/user-ids for more information.
""")
// swiftlint:disable:next missing_docs
public func with(appUserID: StaticString) -> Configuration.Builder {
Logger.warn(Strings.identity.logging_in_with_static_string)
Expand Down
42 changes: 42 additions & 0 deletions Sources/Purchasing/Purchases/Purchases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,11 @@ public extension Purchases {

public extension Purchases {

@available(*, deprecated, message: """
The appUserID passed to logIn is a constant string known at compile time.
This is likely a programmer error. This ID is used to identify the current user.
See https://docs.revenuecat.com/docs/user-ids for more information.
""")
func logIn(_ appUserID: StaticString, completion: @escaping (CustomerInfo?, Bool, PublicError?) -> Void) {
Logger.warn(Strings.identity.logging_in_with_static_string)

Expand Down Expand Up @@ -756,6 +761,11 @@ public extension Purchases {
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *)
@available(*, deprecated, message: """
The appUserID passed to logIn is a constant string known at compile time.
This is likely a programmer error. This ID is used to identify the current user.
See https://docs.revenuecat.com/docs/user-ids for more information.
""")
func logIn(_ appUserID: StaticString) async throws -> (customerInfo: CustomerInfo, created: Bool) {
Logger.warn(Strings.identity.logging_in_with_static_string)

Expand Down Expand Up @@ -1257,11 +1267,23 @@ public extension Purchases {
*
* - Returns: An instantiated ``Purchases`` object that has been set as a singleton.
*/
@_disfavoredOverload
@objc(configureWithAPIKey:appUserID:)
@discardableResult static func configure(withAPIKey apiKey: String, appUserID: String?) -> Purchases {
Self.configure(withAPIKey: apiKey, appUserID: appUserID, observerMode: false)
}

@available(*, deprecated, message: """
The appUserID passed to logIn is a constant string known at compile time.
This is likely a programmer error. This ID is used to identify the current user.
See https://docs.revenuecat.com/docs/user-ids for more information.
""")
// swiftlint:disable:next missing_docs
@discardableResult static func configure(withAPIKey apiKey: String, appUserID: StaticString) -> Purchases {
Logger.warn(Strings.identity.logging_in_with_static_string)
return Self.configure(withAPIKey: apiKey, appUserID: "\(appUserID)", observerMode: false)
}

/**
* Configures an instance of the Purchases SDK with a custom `UserDefaults`.
*
Expand All @@ -1284,6 +1306,7 @@ public extension Purchases {
* - Warning: This assumes your IAP implementation uses StoreKit 1.
* Observer mode is not compatible with StoreKit 2.
*/
@_disfavoredOverload
@objc(configureWithAPIKey:appUserID:observerMode:)
@discardableResult static func configure(withAPIKey apiKey: String,
appUserID: String?,
Expand All @@ -1297,6 +1320,25 @@ public extension Purchases {
)
}

@available(*, deprecated, message: """
The appUserID passed to logIn is a constant string known at compile time.
This is likely a programmer error. This ID is used to identify the current user.
See https://docs.revenuecat.com/docs/user-ids for more information.
""")
// swiftlint:disable:next missing_docs
@discardableResult static func configure(withAPIKey apiKey: String,
appUserID: StaticString,
observerMode: Bool) -> Purchases {
Logger.warn(Strings.identity.logging_in_with_static_string)
return Self.configure(
with: Configuration
.builder(withAPIKey: apiKey)
.with(appUserID: "\(appUserID)")
.with(observerMode: observerMode)
.build()
)
}

#else

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func checkConfigurationAPI() {
let configuration = Configuration
.builder(withAPIKey: "")
.with(apiKey: "")
.with(appUserID: "")
.with(appUserID: nil)
.with(observerMode: false)
.with(userDefaults: UserDefaults.standard)
Expand All @@ -31,4 +30,5 @@ func checkConfigurationAPI() {
func checkDeprecatedConfiguration(_ builder: Configuration.Builder) {
_ = builder
.with(usesStoreKit2IfAvailable: true)
.with(appUserID: "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ func checkConfigurationAPI() {
let builder = Configuration
.builder(withAPIKey: "")
.with(apiKey: "")
.with(appUserID: "")
.with(appUserID: nil)
.with(observerMode: true)
.with(userDefaults: UserDefaults.standard)
Expand All @@ -35,4 +34,5 @@ func checkConfigurationAPI() {
func checkDeprecatedConfiguration(_ builder: Configuration.Builder) {
_ = builder
.with(usesStoreKit2IfAvailable: false)
.with(appUserID: "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ private func checkPurchasesPurchasingAPI(purchases: Purchases) {

private func checkIdentity(purchases: Purchases) {
purchases.logOut { (_: CustomerInfo?, _: Error?) in }
purchases.logIn("") { (_: CustomerInfo?, _: Bool, _: Error?) in }
}

private func checkPurchasesSupportAPI(purchases: Purchases) {
Expand Down Expand Up @@ -236,7 +235,6 @@ private func checkAsyncMethods(purchases: Purchases) async {
let offer: PromotionalOffer! = nil

do {
let _: (CustomerInfo, Bool) = try await purchases.logIn("")
let _: IntroEligibilityStatus = await purchases.checkTrialOrIntroDiscountEligibility(product: stp)
let _: [String: IntroEligibility] = await purchases.checkTrialOrIntroDiscountEligibility(
productIdentifiers: [String]()
Expand Down Expand Up @@ -303,11 +301,7 @@ private func checkConfigure() -> Purchases! {
.build())

Purchases.configure(withAPIKey: "")

Purchases.configure(withAPIKey: "", appUserID: nil)
Purchases.configure(withAPIKey: "", appUserID: "")

Purchases.configure(withAPIKey: "", appUserID: "", observerMode: false)
Purchases.configure(withAPIKey: "", appUserID: nil, observerMode: true)

return nil
Expand All @@ -330,6 +324,7 @@ private func checkAsyncDeprecatedMethods(_ purchases: Purchases, _ stp: StorePro
forProductDiscount: discount,
product: stp
)
let _: (CustomerInfo, Bool) = try await purchases.logIn("")
}

@available(*, deprecated) // Ignore deprecation warnings
Expand All @@ -343,10 +338,14 @@ private func checkDeprecatedMethods(_ purchases: Purchases) {

purchases.checkTrialOrIntroDiscountEligibility([String]()) { (_: [String: IntroEligibility]) in }

purchases.logIn("") { (_: CustomerInfo?, _: Bool, _: Error?) in }

Purchases.configure(withAPIKey: "", appUserID: "", observerMode: true, userDefaults: nil)
Purchases.configure(withAPIKey: "", appUserID: nil, observerMode: true, userDefaults: nil)
Purchases.configure(withAPIKey: "", appUserID: "", observerMode: true, userDefaults: UserDefaults())
Purchases.configure(withAPIKey: "", appUserID: nil, observerMode: true, userDefaults: UserDefaults())
Purchases.configure(withAPIKey: "", appUserID: "")
Purchases.configure(withAPIKey: "", appUserID: "", observerMode: false)
Purchases.configure(withAPIKey: "",
appUserID: nil,
observerMode: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,20 @@ class PurchasesConfiguringTests: BasePurchasesTests {
expect(Purchases.shared) === purchases
}

@available(*, deprecated)
func testSharedInstanceIsSetWhenConfiguringWithAppUserID() {
let purchases = Purchases.configure(withAPIKey: "", appUserID: "")
expect(Purchases.shared) === purchases
}

@available(*, deprecated)
func testSharedInstanceIsSetWhenConfiguringWithObserverMode() {
let purchases = Purchases.configure(withAPIKey: "", appUserID: "", observerMode: true)
expect(Purchases.shared) === purchases
expect(Purchases.shared.finishTransactions) == false
}

@available(*, deprecated)
func testSharedInstanceIsSetWhenConfiguringWithObserverModeDisabled() {
let purchases = Purchases.configure(withAPIKey: "", appUserID: "", observerMode: false)
expect(Purchases.shared) === purchases
Expand Down Expand Up @@ -109,6 +112,7 @@ class PurchasesConfiguringTests: BasePurchasesTests {
expect(purchases.appUserID) == Self.appUserID
}

@available(*, deprecated)
func testUserIdIsSetToAnonymousWhenConfiguringWithEmptyUserID() {
self.deviceCache.userIDStoredInCache = nil

Expand Down Expand Up @@ -163,6 +167,7 @@ class PurchasesConfiguringTests: BasePurchasesTests {
expect(purchases.appUserID) == Self.appUserID
}

@available(*, deprecated)
func testStaticUserIdSringLogsMessage() {
_ = Purchases.configure(
with: .init(withAPIKey: "")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ class PurchasesLogInTests: BasePurchasesLogInTests {
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.2, *)
@available(*, deprecated)
func testLogInWithStaticStringLogsMessage() async throws {
try AvailabilityChecks.iOS13APIAvailableOrSkipTest()

Expand All @@ -225,6 +226,7 @@ class PurchasesLogInTests: BasePurchasesLogInTests {
self.logger.verifyMessageWasNotLogged(Strings.identity.logging_in_with_static_string)
}

@available(*, deprecated)
func testCompletionBlockLogInWithStaticStringLogsMessage() {
self.identityManager.mockLogInResult = .success((Self.mockLoggedInInfo, true))

Expand Down

0 comments on commit da11a79

Please sign in to comment.