Finish up ProductManager implementation

- Reload receipt on updated transactions (e.g. promo code)
- Implement restore purchases (refresh receipt before restoring)
This commit is contained in:
Davide De Rosa 2019-10-30 11:53:49 +01:00
parent 026a94065c
commit f936cffe5e
3 changed files with 58 additions and 14 deletions

View File

@ -43,7 +43,11 @@ class ProductManager: NSObject {
private var purchasedAppVersion: String?
private var purchasedFeatures: Set<Product>
private(set) var purchasedFeatures: Set<Product>
private var refreshRequest: SKReceiptRefreshRequest?
private var restoreCompletionHandler: ((Error?) -> Void)?
private override init() {
inApp = InApp()
@ -53,6 +57,11 @@ class ProductManager: NSObject {
super.init()
reloadReceipt()
SKPaymentQueue.default().add(self)
}
deinit {
SKPaymentQueue.default().remove(self)
}
func listProducts(completionHandler: (([SKProduct]) -> Void)?) {
@ -65,6 +74,10 @@ class ProductManager: NSObject {
}
}
func product(withIdentifier identifier: Product) -> SKProduct? {
return inApp.product(withIdentifier: identifier)
}
func purchase(_ product: SKProduct, completionHandler: @escaping (InAppPurchaseResult, Error?) -> Void) {
inApp.purchase(product: product) {
if $0 == .success {
@ -73,6 +86,13 @@ class ProductManager: NSObject {
completionHandler($0, $1)
}
}
func restorePurchases(completionHandler: @escaping (Error?) -> Void) {
restoreCompletionHandler = completionHandler
refreshRequest = SKReceiptRefreshRequest()
refreshRequest?.delegate = self
refreshRequest?.start()
}
// MARK: In-app eligibility
@ -138,3 +158,27 @@ class ProductManager: NSObject {
}
}
}
extension ProductManager: SKPaymentTransactionObserver {
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
reloadReceipt()
}
}
extension ProductManager: SKRequestDelegate {
func requestDidFinish(_ request: SKRequest) {
reloadReceipt()
inApp.restorePurchases { [weak self] (finished, _, error) in
guard finished else {
return
}
self?.restoreCompletionHandler?(error)
self?.restoreCompletionHandler = nil
}
}
func request(_ request: SKRequest, didFailWithError error: Error) {
restoreCompletionHandler?(error)
restoreCompletionHandler = nil
}
}

View File

@ -14,7 +14,7 @@ def shared_pods
pod 'SSZipArchive'
for spec in ['About', 'Alerts', 'Dialogs', 'InApp', 'Misc', 'Options', 'Persistence', 'Reviewer', 'Tables'] do
pod "Convenience/#{spec}", :git => 'https://github.com/keeshux/convenience', :commit => 'b990a8c'
pod "Convenience/#{spec}", :git => 'https://github.com/keeshux/convenience', :commit => '22778d5'
#pod "Convenience/#{spec}", :path => '../../personal/convenience'
end
end

View File

@ -34,15 +34,15 @@ PODS:
- TunnelKit/Core
DEPENDENCIES:
- Convenience/About (from `https://github.com/keeshux/convenience`, commit `b990a8c`)
- Convenience/Alerts (from `https://github.com/keeshux/convenience`, commit `b990a8c`)
- Convenience/Dialogs (from `https://github.com/keeshux/convenience`, commit `b990a8c`)
- Convenience/InApp (from `https://github.com/keeshux/convenience`, commit `b990a8c`)
- Convenience/Misc (from `https://github.com/keeshux/convenience`, commit `b990a8c`)
- Convenience/Options (from `https://github.com/keeshux/convenience`, commit `b990a8c`)
- Convenience/Persistence (from `https://github.com/keeshux/convenience`, commit `b990a8c`)
- Convenience/Reviewer (from `https://github.com/keeshux/convenience`, commit `b990a8c`)
- Convenience/Tables (from `https://github.com/keeshux/convenience`, commit `b990a8c`)
- Convenience/About (from `https://github.com/keeshux/convenience`, commit `22778d5`)
- Convenience/Alerts (from `https://github.com/keeshux/convenience`, commit `22778d5`)
- Convenience/Dialogs (from `https://github.com/keeshux/convenience`, commit `22778d5`)
- Convenience/InApp (from `https://github.com/keeshux/convenience`, commit `22778d5`)
- Convenience/Misc (from `https://github.com/keeshux/convenience`, commit `22778d5`)
- Convenience/Options (from `https://github.com/keeshux/convenience`, commit `22778d5`)
- Convenience/Persistence (from `https://github.com/keeshux/convenience`, commit `22778d5`)
- Convenience/Reviewer (from `https://github.com/keeshux/convenience`, commit `22778d5`)
- Convenience/Tables (from `https://github.com/keeshux/convenience`, commit `22778d5`)
- Kvitto
- MBProgressHUD
- SSZipArchive
@ -60,7 +60,7 @@ SPEC REPOS:
EXTERNAL SOURCES:
Convenience:
:commit: b990a8c
:commit: 22778d5
:git: https://github.com/keeshux/convenience
TunnelKit:
:commit: 4d930d3
@ -68,7 +68,7 @@ EXTERNAL SOURCES:
CHECKOUT OPTIONS:
Convenience:
:commit: b990a8c
:commit: 22778d5
:git: https://github.com/keeshux/convenience
TunnelKit:
:commit: 4d930d3
@ -84,6 +84,6 @@ SPEC CHECKSUMS:
SwiftyBeaver: aaf2ebd7dac2e952991f46a82ed24ad642867ae2
TunnelKit: 0743f0306be0869d51118ac33e274e7507a93537
PODFILE CHECKSUM: d1e12ff38bc1839dddbbbcaa5e436ea0fad60f70
PODFILE CHECKSUM: 8099389f0a709d4f175b81d077f47699a651fa42
COCOAPODS: 1.8.4