diff --git a/Purchases/Public/RCPurchases.h b/Purchases/Public/RCPurchases.h index 9eea832971..83d44a4afd 100644 --- a/Purchases/Public/RCPurchases.h +++ b/Purchases/Public/RCPurchases.h @@ -152,11 +152,18 @@ NS_SWIFT_NAME(Purchases) /** @return A singleton `RCPurchases` object. Call this after a configure method to access the singleton. + @note: If the SDK has not been configured, calls to sharedPurchases will raise an exception. Make sure to configure the SDK before making calls to sharedPurchases. */ @property (class, nonatomic, readonly) RCPurchases *sharedPurchases; #pragma mark Configuration +/** + @note True if the SDK has been configured, false otherwise. This property should only be used in special circumstances. If the shared instance has not been configured, + calls made to it will raise an exception. + */ +@property (class, nonatomic, readonly) BOOL isConfigured; + /** Set this to true if you are passing in an appUserID but it is anonymous, this is true by default if you didn't pass an appUserID If a user tries to purchase a product that is active on the current app store account, we will treat it as a restore and alias the new ID with the previous id. diff --git a/Purchases/Public/RCPurchases.m b/Purchases/Public/RCPurchases.m index 1b4feec1fd..284b14df3b 100644 --- a/Purchases/Public/RCPurchases.m +++ b/Purchases/Public/RCPurchases.m @@ -138,10 +138,12 @@ - (void)setFinishTransactions:(BOOL)finishTransactions { self.systemInfo.finishTransactions = finishTransactions; } ++ (BOOL)isConfigured { + return _sharedPurchases != nil; +} + + (instancetype)sharedPurchases { - if (!_sharedPurchases) { - RCWarnLog(@"%@", RCStrings.configure.no_singleton_instance); - } + NSCAssert(_sharedPurchases, RCStrings.configure.no_singleton_instance); return _sharedPurchases; } diff --git a/PurchasesTests/Purchasing/PurchasesTests.swift b/PurchasesTests/Purchasing/PurchasesTests.swift index e02b178d71..ba298faece 100644 --- a/PurchasesTests/Purchasing/PurchasesTests.swift +++ b/PurchasesTests/Purchasing/PurchasesTests.swift @@ -250,7 +250,19 @@ class PurchasesTests: XCTestCase { setupPurchases() expect(self.purchases).toNot(beNil()) } + + func testUsingSharedInstanceWithoutInitializingRaisesException() { + expect{ Purchases.shared }.to(raiseException()) + setupPurchases() + expect{ Purchases.shared }.toNot(raiseException()) + } + func testIsConfiguredReturnsCorrectvalue() { + expect(Purchases.isConfigured) == false + setupPurchases() + expect(Purchases.isConfigured) == true + } + func testFirstInitializationCallDelegate() { setupPurchases() expect(self.purchasesDelegate.purchaserInfoReceivedCount).toEventually(equal(1))