Skip to content

Commit

Permalink
test docs with jazzy and swift-doc
Browse files Browse the repository at this point in the history
  • Loading branch information
iridescent-dev committed May 21, 2020
1 parent 8ff235c commit 5dbb33f
Show file tree
Hide file tree
Showing 89 changed files with 17,369 additions and 232 deletions.
85 changes: 60 additions & 25 deletions Sources/InAppPurchaseLib/InAppPurchase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,37 @@ import StoreKit


public class InAppPurchase: NSObject, InAppPurchaseLib {
/// InAppPurchaseLib version number.
// InAppPurchaseLib version number.
internal static let versionNumber = "1.0.2"
/// The initialize function has been called.
// The initialize function has been called.
internal static var initialized: Bool {
return !iapProducts.isEmpty && iapPurchaseDelegate != nil && validatorUrlString != nil
}
/// Callbacks that are waiting for the refresh.
// Callbacks that are waiting for the refresh.
private static var refreshCallbacks: Array<IAPRefreshCallback> = []
/// The date of the last refresh.
// The date of the last refresh.
private static var lastRefreshDate: Date? = nil


/* MARK: - Properties */
/// The array of `IAPProduct`.
public static var iapProducts: Array<IAPProduct> = []
public static var iapPurchaseDelegate: IAPPurchaseDelegate? = nil
/// The validator url retrieved from Fovea.
public static var validatorUrlString: String? = nil
/// The instance of class that adopts the `IAPPurchaseDelegate` protocol.
public static var iapPurchaseDelegate: IAPPurchaseDelegate? = nil
/// The user name, if your app implements user login.
public static var applicationUsername: String? = nil


/* MARK: - Main methods */
// Start observing the payment queue, as soon as possible, and refresh Product list and user Receipt.
public static func initialize(iapProducts: Array<IAPProduct>, iapPurchaseDelegate: IAPPurchaseDelegate, validatorUrlString: String, applicationUsername: String?) {
/// Start observing the payment queue, as soon as possible, and refresh Product list and user Receipt.
/// - Parameters:
/// - iapProducts: An array of `IAPProduct`.
/// - validatorUrlString: The validator url retrieved from Fovea.
/// - iapPurchaseDelegate: An instance of class that adopts the `IAPPurchaseDelegate` protocol (default value = `DefaultPurchaseDelegate`).
/// - applicationUsername: The user name, if your app implements user login.
public static func initialize(iapProducts: Array<IAPProduct>, validatorUrlString: String, iapPurchaseDelegate: IAPPurchaseDelegate, applicationUsername: String?) {

InAppPurchase.iapProducts = iapProducts
InAppPurchase.iapPurchaseDelegate = iapPurchaseDelegate
Expand All @@ -42,12 +51,14 @@ public class InAppPurchase: NSObject, InAppPurchaseLib {
IAPTransactionObserver.shared.start()
}

// Stop observing the payment queue, when the application will terminate, for proper cleanup.
/// Stop observing the payment queue, when the application will terminate, for proper cleanup.
public static func stop() {
IAPTransactionObserver.shared.stop()
}

// Refresh Product list and user Receipt.
/// Refresh Product list and user Receipt.
/// - Parameter callback: The function that will be called after processing.
/// - See also:`IAPRefreshCallback` and `IAPRefreshResult`.
public static func refresh(callback: @escaping IAPRefreshCallback) {
if !initialized {
callback(IAPRefreshResult(state: .failed, iapError: IAPError(code: .libraryNotInitialized)))
Expand Down Expand Up @@ -111,19 +122,35 @@ public class InAppPurchase: NSObject, InAppPurchaseLib {


/* MARK: - Products information */
// Returns all products retrieved from the App Store.
/// Gets all products retrieved from the App Store
/// - Returns: An array of products.
/// - See also: `SKProduct`.
public static func getProducts() -> Array<SKProduct> {
return IAPProductService.shared.getProducts()
}

// Gets the product by its identifier from the list of products retrieved from the App Store.
/// Gets the product by its identifier from the list of products retrieved from the App Store.
/// - Parameter identifier: The identifier of the product.
/// - Returns: The product if it was retrieved from the App Store.
/// - See also: `SKProduct`.
public static func getProductBy(identifier: String) -> SKProduct? {
return IAPProductService.shared.getProductBy(identifier: identifier)
}


/* MARK: - Purchasing and Restoring */
// Request a Payment from the App Store.
/// Checks if the user is allowed to authorize payments.
/// - Returns: A boolean indicates if the user is allowed.
public static func canMakePayments() -> Bool {
return IAPTransactionObserver.shared.canMakePayments()
}

/// Request a Payment from the App Store.
/// - Parameters:
/// - productIdentifier: The identifier of the product to purchase.
/// - quantity: The quantity to purchase (default value = 1).
/// - callback: The function that will be called after processing.
/// - See also:`IAPPurchaseCallback` and `IAPPurchaseResult`.
public static func purchase(productIdentifier: String, quantity: Int, callback: @escaping IAPPurchaseCallback) {
if !initialized {
callback(IAPPurchaseResult(state: .failed, iapError: IAPError(code: .libraryNotInitialized)))
Expand All @@ -137,7 +164,9 @@ public class InAppPurchase: NSObject, InAppPurchaseLib {
IAPTransactionObserver.shared.purchase(product: product, quantity: quantity, applicationUsername: applicationUsername, callback: callback)
}

// Restore purchased products.
/// Restore purchased products.
/// - Parameter callback: The function that will be called after processing.
/// - See also:`IAPRefreshCallback` and `IAPRefreshResult`.
public static func restorePurchases(callback: @escaping IAPRefreshCallback) {
if !initialized {
callback(IAPRefreshResult(state: .failed, iapError: IAPError(code: .libraryNotInitialized)))
Expand All @@ -147,34 +176,36 @@ public class InAppPurchase: NSObject, InAppPurchaseLib {
IAPReceiptService.shared.refresh(callback: callback)
}

// Checks if the user is allowed to authorize payments.
public static func canMakePayments() -> Bool {
return IAPTransactionObserver.shared.canMakePayments()
}

// Finish all transactions for the product.
/// Finish all transactions for the product.
/// - Parameter productIdentifier: The identifier of the product.
public static func finishTransactions(for productIdentifier: String) {
IAPTransactionObserver.shared.finishTransactions(for: productIdentifier)
}

// Checks if the last transaction state for a given product was deferred.
/// Checks if the last transaction state for a given product was deferred.
/// - Parameter productIdentifier: The identifier of the product.
/// - Returns: A boolean indicates if the last transaction state was deferred.
public static func hasDeferredTransaction(for productIdentifier: String) -> Bool {
return IAPTransactionObserver.shared.hasDeferredTransaction(for: productIdentifier)
}


/* MARK: - Purchases information */
// Checks if the user has already purchased at least one product.
/// Checks if the user has already purchased at least one product.
/// - Returns: A boolean indicates if the .
public static func hasAlreadyPurchased() -> Bool {
return IAPReceiptService.shared.hasAlreadyPurchased()
}

// Checks if the user currently own (or is subscribed to) a given product (nonConsumable or autoRenewableSubscription).
/// Checks if the user currently own (or is subscribed to) a given product (nonConsumable or autoRenewableSubscription).
/// - Parameter productIdentifier: The identifier of the product.
/// - Returns: A boolean indicates if the user currently own (or is subscribed to) a given product.
public static func hasActivePurchase(for productIdentifier: String) -> Bool {
return IAPReceiptService.shared.hasActivePurchase(for: productIdentifier)
}

// Checks if the user has an active auto renewable subscription.
/// Checks if the user has an active auto renewable subscription regardless of the product identifier.
/// - Returns: A boolean indicates if the user has an active auto renewable subscription.
public static func hasActiveSubscription() -> Bool {
for productIdentifier in (InAppPurchase.iapProducts.filter{ $0.productType == IAPProductType.autoRenewableSubscription }.map{ $0.productIdentifier }) {
if IAPReceiptService.shared.hasActivePurchase(for: productIdentifier){
Expand All @@ -184,12 +215,16 @@ public class InAppPurchase: NSObject, InAppPurchaseLib {
return false;
}

// Returns the latest purchased date for a given product.
/// Returns the latest purchased date for a given product.
/// - Parameter productIdentifier: The identifier of the product.
/// - Returns: The latest purchase `Date` if set or `nil`.
public static func getPurchaseDate(for productIdentifier: String) -> Date? {
return IAPReceiptService.shared.getPurchaseDate(for: productIdentifier)
}

// Returns the expiry date for a subcription. May be past or future.
/// Returns the expiry date for a subcription. May be past or future.
/// - Parameter productIdentifier: The identifier of the product.
/// - Returns: The expiry `Date` is set or `nil`.
public static func getExpiryDate(for productIdentifier: String) -> Date? {
return IAPReceiptService.shared.getExpiryDate(for: productIdentifier)
}
Expand Down
107 changes: 74 additions & 33 deletions Sources/InAppPurchaseLib/InAppPurchaseLib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,69 +9,104 @@ import Foundation
import StoreKit


/* MARK: - The protocol that InAppPurchase adopts. */
/// The protocol that `InAppPurchase`` adopts.
public protocol InAppPurchaseLib {
/// The array of `IAPProduct`.
static var iapProducts: Array<IAPProduct> { get }
static var iapPurchaseDelegate: IAPPurchaseDelegate? { get }
/// The validator url retrieved from Fovea.
static var validatorUrlString: String? { get }
/// The instance of class that adopts the `IAPPurchaseDelegate` protocol.
static var iapPurchaseDelegate: IAPPurchaseDelegate? { get }
/// The user name, if your app implements user login.
static var applicationUsername: String? { get set }

// Start observing the payment queue, as soon as possible, and refresh Product list and user Receipt.
static func initialize(iapProducts: Array<IAPProduct>, iapPurchaseDelegate: IAPPurchaseDelegate, validatorUrlString: String, applicationUsername: String?)
/// Start observing the payment queue, as soon as possible, and refresh Product list and user Receipt.
/// - Parameters:
/// - iapProducts: An array of `IAPProduct`.
/// - validatorUrlString: The validator url retrieved from Fovea.
/// - iapPurchaseDelegate: An instance of class that adopts the `IAPPurchaseDelegate` protocol (default value = `DefaultPurchaseDelegate`).
/// - applicationUsername: The user name, if your app implements user login.
static func initialize(iapProducts: Array<IAPProduct>, validatorUrlString: String, iapPurchaseDelegate: IAPPurchaseDelegate, applicationUsername: String?) -> Void

// Stop observing the payment queue, when the application will terminate, for proper cleanup.
static func stop()
/// Stop observing the payment queue, when the application will terminate, for proper cleanup.
static func stop() -> Void

// Refresh Product list and user Receipt.
static func refresh(callback: @escaping IAPRefreshCallback)
/// Refresh Product list and user Receipt.
/// - Parameter callback: The function that will be called after processing.
/// - See also:`IAPRefreshCallback` and `IAPRefreshResult`.
static func refresh(callback: @escaping IAPRefreshCallback) -> Void


/* MARK: - Products information */
// Returns all products retrieved from the App Store.
/// Gets all products retrieved from the App Store
/// - Returns: An array of products.
/// - See also: `SKProduct`.
static func getProducts() -> Array<SKProduct>

// Gets the product by its identifier from the list of products retrieved from the App Store.
/// Gets the product by its identifier from the list of products retrieved from the App Store.
/// - Parameter identifier: The identifier of the product.
/// - Returns: The product if it was retrieved from the App Store.
/// - See also: `SKProduct`.
static func getProductBy(identifier: String) -> SKProduct?


/* MARK: - Purchasing and Restoring */
// Checks if the user is allowed to authorize payments.
/// Checks if the user is allowed to authorize payments.
/// - Returns: A boolean indicates if the user is allowed.
static func canMakePayments() -> Bool

// Request a Payment from the App Store.
static func purchase(productIdentifier: String, quantity: Int, callback: @escaping IAPPurchaseCallback)

// Restore purchased products.
static func restorePurchases(callback: @escaping IAPRefreshCallback)

// Finish all transactions for the product.
static func finishTransactions(for productIdentifier: String)

// Checks if the last transaction state for a given product was deferred.
/// Request a Payment from the App Store.
/// - Parameters:
/// - productIdentifier: The identifier of the product to purchase.
/// - quantity: The quantity to purchase (default value = 1).
/// - callback: The function that will be called after processing.
/// - See also:`IAPPurchaseCallback` and `IAPPurchaseResult`.
static func purchase(productIdentifier: String, quantity: Int, callback: @escaping IAPPurchaseCallback) -> Void

/// Restore purchased products.
/// - Parameter callback: The function that will be called after processing.
/// - See also:`IAPRefreshCallback` and `IAPRefreshResult`.
static func restorePurchases(callback: @escaping IAPRefreshCallback) -> Void

/// Finish all transactions for the product.
/// - Parameter productIdentifier: The identifier of the product.
static func finishTransactions(for productIdentifier: String) -> Void

/// Checks if the last transaction state for a given product was deferred.
/// - Parameter productIdentifier: The identifier of the product.
/// - Returns: A boolean indicates if the last transaction state was deferred.
static func hasDeferredTransaction(for productIdentifier: String) -> Bool


/* MARK: - Purchases information */
// Checks if the user has already purchased at least one product.
/// Checks if the user has already purchased at least one product.
/// - Returns: A boolean indicates if the .
static func hasAlreadyPurchased() -> Bool

// Checks if the user currently own (or is subscribed to) a given product (nonConsumable or autoRenewableSubscription).
/// Checks if the user currently own (or is subscribed to) a given product (nonConsumable or autoRenewableSubscription).
/// - Parameter productIdentifier: The identifier of the product.
/// - Returns: A boolean indicates if the user currently own (or is subscribed to) a given product.
static func hasActivePurchase(for productIdentifier: String) -> Bool

// Checks if the user has an active auto renewable subscription.
/// Checks if the user has an active auto renewable subscription regardless of the product identifier.
/// - Returns: A boolean indicates if the user has an active auto renewable subscription.
static func hasActiveSubscription() -> Bool

// Returns the latest purchased date for a given product.
/// Returns the latest purchased date for a given product.
/// - Parameter productIdentifier: The identifier of the product.
/// - Returns: The latest purchase `Date` if set or `nil`.
static func getPurchaseDate(for productIdentifier: String) -> Date?

// Returns the expiry date for a subcription. May be past or future.
/// Returns the expiry date for a subcription. May be past or future.
/// - Parameter productIdentifier: The identifier of the product.
/// - Returns: The expiry `Date` is set or `nil`.
static func getExpiryDate(for productIdentifier: String) -> Date?
}

public extension InAppPurchaseLib {
// Sets default IAPPurchaseDelegate
static func initialize(iapProducts: Array<IAPProduct>, iapPurchaseDelegate: IAPPurchaseDelegate = DefaultPurchaseDelegate(), validatorUrlString: String, applicationUsername: String? = nil) {
return initialize(iapProducts: iapProducts, iapPurchaseDelegate: iapPurchaseDelegate, validatorUrlString: validatorUrlString, applicationUsername: applicationUsername)
// Sets default `iapPurchaseDelegate` and `applicationUsername`
static func initialize(iapProducts: Array<IAPProduct>, validatorUrlString: String, iapPurchaseDelegate: IAPPurchaseDelegate = DefaultPurchaseDelegate(), applicationUsername: String? = nil) {
return initialize(iapProducts: iapProducts, validatorUrlString: validatorUrlString, iapPurchaseDelegate: iapPurchaseDelegate, applicationUsername: applicationUsername)
}

// Sets 1 as default value for the quantity.
Expand All @@ -82,17 +117,23 @@ public extension InAppPurchaseLib {


/* MARK: - The protocol that you must adopt. */
/// The protocol that you must adopt if you have `consumable` and/or `nonRenewingSubscription` products.
public protocol IAPPurchaseDelegate {
// Called when a product is newly purchased, updated or restored.
func productPurchased(productIdentifier: String)
/// Called when a product is newly purchased, updated or restored.
/// - Parameter productIdentifier: The identifier of the product.
///
/// - Important: You have to acknowledge delivery of the (virtual) item to finalize the transaction. Then you have to call `InAppPurchase.finishTransactions(for: productIdentifier)`as soon as you have delivered the product.
func productPurchased(productIdentifier: String) -> Void
}


// The default implementation of IAPPurchaseDelegate if no other is provided.
/// The default implementation of `IAPPurchaseDelegate` if no other is provided.
public class DefaultPurchaseDelegate: IAPPurchaseDelegate {
public init(){}

/// Finish the product transactions when a product is newly purchased, updated or restored.
/// - Parameter productIdentifier: The identifier of the product.
public func productPurchased(productIdentifier: String) {
// Finish the product transactions.
InAppPurchase.finishTransactions(for: productIdentifier)
}
}
10 changes: 10 additions & 0 deletions Sources/InAppPurchaseLib/Product/IAPProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,19 @@ import Foundation


public struct IAPProduct {

/// The identifier of the product.
public var productIdentifier: String

/// The type of the product.
/// - See also: `IAPProductType`.
public var productType: IAPProductType

/// Initializes an `IAPProduct` with its identifier and type.
/// - Parameters:
/// - productIdentifier: The identifier of the product.
/// - productType: The type of the product.
/// - See also: `IAPProductType`.
public init(productIdentifier: String, productType: IAPProductType) {
self.productIdentifier = productIdentifier
self.productType = productType
Expand Down
Loading

0 comments on commit 5dbb33f

Please sign in to comment.