StoreKitCompanion 2.0.0

StoreKitCompanion 2.0.0

测试已测试
Lang语言 SwiftSwift
许可证 MIT
Released最后发布2017年3月
SwiftSwift 版本3.0
SPM支持 SPM

Vincent CARLIERTom Baranes 维护。



  • Recisio

StoreKitCompanion

Apple StoreKit 的轻量级包装器,用 Swift 编写。

安装

Swift 包管理器

StoreKitCompanion 可在 SPM 上使用。只需将以下内容添加到您的 Package 文件中:

import PackageDescription

let package = Package(
    dependencies: [
        .Package(url: "https://github.com/recisio/StoreKitCompanion.git", majorVersion: 1)
    ]
)

手动

StoreKitCompanion.swift 添加到您的项目中。

用法

StoreKitCompanion 允许您通过几行代码来 获取产品启动支付事务 以及 通过网络发送 App Store 收据数据进行验证

获取产品

获取产品非常简单,调用 fetchProductsWithIdentifiers(_:completion:) 并传递一组产品标识符。至少,也提供一个完成闭包以获取返回的有效 SKProduct 对象。可选地,提供一个错误处理闭包以处理错误。

StoreKitCompanion.sharedInstance.fetchProductsWithIdentifiers(["com.mycompany.MyKillerProduct"], completion: { products, invalids in
    print("Got products", products)
    print("Got invalids", invalids)
})

购买

在尝试购买之前,您应该检查用户是否有权进行支付。您可以通过调用 canMakePayments() 来做这件事。

StoreKitCompanion.sharedInstance.canMakePayments()

支付提交到默认支付队列(SKPaymentQueue.defaultQueue()),StoreKitCompanion 会观察它。每当它发生任何事情时,StoreKitCompanion 都会发送通知,并调用您设置的闭包。

// Provide a closure to be called when transactions are updated
StoreKitCompanion.sharedInstance.transactionsUpdateHandler = { queue, transactions in
    // Process transactions..
}
// Add a payment, quantity is 1 by default
StoreKitCompanion.sharedInstance.addPaymentForProductIdentifier("com.mycompany.MyKillerProduct")

恢复先前事务

您可以通过调用 restoreCompletedTransactionsWithUsername(_:) 来恢复已完成的事务。
您也可以提供可选的用户名。
您设置的回调会响应恢复请求。

StoreKitCompanion.restoreCompletedTransactionsWithUsername()

验证 App Store 收据

有关App Store收据验证的详细信息,可以在苹果的网站上找到。虽然StoreKitCompanion目前还不执行任何本地收据验证,但它允许您将其发送到服务器进行验证。

默认情况下,StoreKitCompanion将数据使用HTTP POST请求发送,参数名为receiptData,但您可以根据需要覆盖这些设置。

// First supply a validation URL, StoreKitCompanion will use it
StoreKitCompanion.sharedInstance.validationURLString = "http://myserver.com"
// Send the data out
StoreKitCompanion.sharedInstance.sendReceiptWithPOSTRequest() { responseData, error in
    print("Got response", responseData)
    print("Got error", error)
}

为了自定义发送收据的方式,请使用sendReceiptWithDescriptor(_:completion:),并向它提供一个符合ReceiptValidationRequestDescriptor协议的对象。协议扩展已经提供了默认值,因此仅覆盖所需的值。

struct MyRequestDescriptor: ReceiptValidationRequestDescriptor {

    // Name parameters differently or add parameters
    func parametersWithReceiptData(receiptData: NSData) -> [String: AnyObject]? {
        return ["best_param_name": receiptData, "new_param": "new_param_value"]
    }

}

// Send the data out
StoreKitCompanion.sharedInstance.sendReceiptWithDescriptor(MyRequestDescriptor()) { responseData, error in
    print("Got response", responseData)
    print("Got error", error)
}

其他

通知

StoreKitCompanion监视默认支付队列并发送通知

/**
    The name of the notification sent when the payment queue finished to restore completed transactions.
    User info dictionary contains a reference to the `SKPaymentQueue` object.
*/
public static let PaymentQueueDidFinishRestoringCompletedTransactions  = "SKCPaymentQueueDidFinishRestoringCompletedTransactions"
/**
    The name of the notification sent when transactions are updated.
    User info dictionary contains a reference to the `SKPaymentQueue` object and an array of `SKPaymentTransaction` objects.
*/
public static let PaymentQueueDidUpdateTransactions                    = "SKCPaymentQueueDidUpdateTransactions"
/**
    The name of the notification sent when transactions are removed.
    User info dictionary contains a reference to the `SKPaymentQueue` object and an array of `SKPaymentTransaction` objects.
*/
public static let PaymentQueueDidRemoveTransactions                    = "SKCPaymentQueueDidRemoveTransactions"
/**
    The name of the notification sent when downloads are updated
    User info dictionary contains a reference to the `SKPaymentQueue` object and an array of `SKDownload` objects.
*/
public static let PaymentQueueDidUpdateDownloads                       = "SKCPaymentQueueDidUpdateDownloads"
/**
    The name of the notification sent when the payment queue fails to restore completed transactions.
    User info dictionary contains a reference to the `SKPaymentQueue` object and a reference to the `NSError` object.
*/
public static let PaymentQueueDidFailRestoringCompletedTransactions    = "SKCPaymentQueueDidFailRestoringCompletedTransactions"

回调

处理支付队列事件的另一种方法是向StoreKitCompanion提供事件闭包。

public typealias CompletedTransactionsRestoreCompletion    = (SKPaymentQueue) -> Void
public typealias CompletedTransactionsRestoreFailure       = (SKPaymentQueue, NSError) -> Void
public typealias DownloadsUpdateCompletion                 = (SKPaymentQueue, [SKDownload]) -> Void
public typealias TransactionsUpdateCompletion              = (SKPaymentQueue, [SKPaymentTransaction]) -> Void
public typealias TransactionsRemovalCompletion             = (SKPaymentQueue, [SKPaymentTransaction]) -> Void

// ....

/**
    Handles successful completed transactions restoration
*/
public var completedTransactionsRestorationSuccessHandler: CompletedTransactionsRestoreCompletion?
/**
    Handles completed transaction restoration failure
*/
public var completedTransactionsRestorationFailureHandler: CompletedTransactionsRestoreFailure?
/**
    Handles successful downloads restoration
*/
public var downloadsUpdateSuccessHandler: DownloadsUpdateCompletion?
/**
    Handles transaction updates
*/
public var transactionsUpdateHandler: TransactionsUpdateCompletion?
/**
    Handles transaction removals
*/
public var transactionsRemovalHandler: TransactionsRemovalCompletion?

接下来是什么

  • 处理交易恢复
  • 本地验证App Store收据

贡献

  • 如果您发现了一个错误,请提交一个问题
  • 如果您有一个功能请求,请提交一个问题
  • 如果您想做出贡献,请提交一个拉取请求

许可证

StoreKitCompanion在MIT许可证下提供。有关更多信息,请参阅LICENSE文件。