A Day In The Life

とあるプログラマの備忘録

面倒な課金処理をRx化するRxStoreKitを作りました

StoreKit の SKProductsRequest と SKPaymentQueue を Rx化して課金処理を簡単に書けるライブラリを GitHub にて公開しました。 github.com RxStoreKit を使うと課金処理を以下のような感じで書けます。

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()

Rx以前の課金処理

だいぶ前の記事ですが iOS アプリの課金処理まわりの記事を書きました。 glassonion.hatenablog.com こちら見ていただくとわかると思いますが StoreKit(In-App Purchase) のプログラムはものすごく複雑です。中でも SKPaymentTransactionObserver の処理が特に大変です。StoreKit のような非同期コールバック地獄みたいなプログラムは RxSwift とRxCocoa を使って Rx化するとものすごく簡略化できます。というわけで RxStoreKit を宜しくお願いします。

関連記事

失敗しない iOS In-App Purchase プログラミング - A Day In The Life
RxSwift DelegateProxy with required methods – LazySequence<UnsafePointer>