アプリ内課金ライブラリ、SwiftyStoreKitを利用し課金開発時間とコストを低減

アプリ内課金のコーディングは結構大変な作業。開発者としてはアプリの開発に専念したいところです。そこで便利なのが、このSwiftyStoreKitライブラリ。自社のアプリのロジックに合わせて課金の機能を組み込んで行きます。ライブラリのバージョンは、まだ1.0.0になっていませんが、利用しているアプリの実績も多い。何よりアプリ課金の開発コスト下げ、簡単に自分がしたいアプリ課金のロジックにインテグレーションでき、更にアラートメッセージもしっかりと豊富なのが魅力です。

初期設定(iOS Cocoapad)

pod ‘SwiftyStoreKit’

トランザクションの終了

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
SwiftyStoreKit.completeTransactions(atomically: true) { purchases in
for purchase in purchases {
if purchase.transaction.transactionState == .purchased || purchase.transaction.transactionState == .restored {
if purchase.needsFinishTransaction {
// Deliver content from server, then:
SwiftyStoreKit.finishTransaction(purchase.transaction)
}
print(“purchased: \(purchase)”)
}
}
}
return true
}

プロダクト情報取得

SwiftyStoreKit.retrieveProductsInfo([“com.musevisions.SwiftyStoreKit.Purchase1”]) { result in
if let product = result.retrievedProducts.first {
let priceString = product.localizedPrice!
print(“Product: \(product.localizedDescription), price: \(priceString)”)
}
else if let invalidProductId = result.invalidProductIDs.first {
return alertWithTitle(“Could not retrieve product info”, message: “Invalid product identifier: \(invalidProductId)”)
} else {
print(“Error: \(result.error)”)
}
}

プロダクト購入(自動課金)

SwiftyStoreKit.purchaseProduct(“com.musevisions.SwiftyStoreKit.Purchase1”, quantity: 1, atomically: true) { result in
switch result {
case .success(let purchase):
print(“Purchase Success: \(purchase.productId)”) case .error(let error):
switch error.code {
case .unknown: print(“Unknown error. Please contact support”)
case .clientInvalid: print(“Not allowed to make the payment”)
case .paymentCancelled: break
case .paymentInvalid: print(“The purchase identifier was invalid”)
case .paymentNotAllowed: print(“The device is not allowed to make the payment”)
case .storeProductNotAvailable: print(“The product is not available in the current storefront”)
case .cloudServicePermissionDenied: print(“Access to cloud service information is not allowed”)
case .cloudServiceNetworkConnectionFailed: print(“Could not connect to the network”)
case .cloudServiceRevoked: print(“User has revoked permission to use this cloud service”)
}
}
}

過去の購入の復元(自動課金)(アップル要求条件項目)

SwiftyStoreKit.restorePurchases(atomically: true) { results in
if results.restoreFailedPurchases.count > 0 {
print(“Restore Failed: \(results.restoreFailedPurchases)”)
}
else if results.restoredPurchases.count > 0 {
print(“Restore Success: \(results.restoredPurchases)”)
}
else {
print(“Nothing to Restore”)
}
}

レ シートの確認

let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: “your-shared-secret”)
SwiftyStoreKit.verifyReceipt(using: appleValidator, forceRefresh: false) { result in
switch result {
case .success(let receipt):
print(“Verify receipt success: \(receipt)”)
case .error(let error):
print(“Verify receipt failed: \(error)”)
}
}

購入の確認

let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: “your-shared-secret”)
SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
switch result {
case .success(let receipt):
// Verify the purchase of Consumable or NonConsumable
let purchaseResult = SwiftyStoreKit.verifyPurchase(
productId: “com.musevisions.SwiftyStoreKit.Purchase1”,
inReceipt: receipt)

switch purchaseResult {
case .purchased(let receiptItem):
print(“Product is purchased: \(receiptItem)”)
case .notPurchased:
print(“The user has never purchased this product”)
}
case .error(let error): print(“Receipt verification failed: \(error)”)
}
}

定期購読の確認

let appleValidator = AppleReceiptValidator(service: .production, sharedSecret: “your-shared-secret”)
SwiftyStoreKit.verifyReceipt(using: appleValidator) { result in
switch result {
case .success(let receipt):
// Verify the purchase of a Subscription
let purchaseResult = SwiftyStoreKit.verifySubscription(
type: .autoRenewable, // or .nonRenewing (see below)
productId: “com.musevisions.SwiftyStoreKit.Subscription”,
inReceipt: receipt)

switch purchaseResult {
case .purchased(let expiryDate, let receiptItems):
print(“Product is valid until \(expiryDate)”)
case .expired(let expiryDate, let receiptItems):
print(“Product is expired since \(expiryDate)”)
case .notPurchased:
print(“The user has never purchased this product”)
}

case .error(let error):
print(“Receipt verification failed: \(error)”)
}
}

詳細は
https://github.com/bizz84/SwiftyStoreKit