RxStoreKit is lightweight and easy to use Rx support for StoreKit(In-App Purchases).
import StoreKit
import RxSwift
import RxStoreKit
let disposeBag = DisposeBag()
let productRequest = SKProductsRequest(productIdentifiers: Set(["your app product id"]))
productRequest.rx.productsRequest
.subscribe(onNext: { product in
print(product)
}).disposed(by: disposeBag)
productRequest.start()
SKPaymentQueue.default().rx.restoreCompletedTransactions()
.subscribe(onNext: { queue in
// paymentQueueRestoreCompletedTransactionsFinished
print(queue)
}, onError: { error in
// restoreCompletedTransactionsFailedWithError
print(queue)
}).disposed(by: disposeBag)
let productRequest = SKProductsRequest(productIdentifiers: Set(["xxx.xxx.xxx"]))
productRequest.rx.productsRequest
.flatMap { response -> Observable<SKProduct> in
return Observable.from(response.products)
}
.flatMap { product -> Observable<SKPaymentTransaction> in
return SKPaymentQueue.default().rx.add(product: product)
}
.subscribe(onNext: { transition in
print(transition)
}).disposed(by: disposeBag)
productRequest.start()
Download In-App Purchase Contents
let productRequest = SKProductsRequest(productIdentifiers: Set(["xxx.xxx.xxx"]))
productRequest.rx.productsRequest
.flatMap { response -> Observable<SKProduct> in
return Observable.from(response.products)
}
.flatMap { product -> Observable<SKPaymentTransaction> in
return SKPaymentQueue.default().rx.add(product: product)
}
.flatMap { transaction -> Observable<SKDownload> in
return SKPaymentQueue.default().rx.start(downloads: transaction.downloads)
}
.subscribe(onNext: { download in
print(download)
}).disposed(by: disposeBag)
productRequest.start()
This library depends on both RxSwift and RxCocoa
RxStoreKit is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "RxStoreKit"
Then, run following command:
$ pod install
RxStoreKit is available under the MIT license. See the LICENSE file for more info.