
相信做过的朋友都知道, Mac App Store 是个坑,而 In-App Purchase 则是坑中之坑…
刚开始做 Mac 应用内支付时,看苹果的示例都是 OC 、而不是 Swift 的,于是偷懒直接用了 SwiftyStoreKit 确实省了不时间,但也确实有些坑。关键是,出现问题之后,都不知道是这个库本身的问题,还是 Apple 的问题。再加上很多朋友在 iPic 升级遇到的无法升级、无法恢复等等诡异的问题,实在是头大。
在 iPic 1.2.0 新版中,痛定思痛,我还是决定啃啃 IAP 这个骨头。哪怕是踩坑,也要踩自己挖的坑,而不是掉进别人的…
啃完之后,不敢说对 IAP 对多深的了解,但也足够来开发了。想着这样无趣的过程,第位开发者都要经历一遍,便觉得更无趣。于是,就把自己做的库 开源出来给大家用,取名 IAPHelper ,希望能节约大家一点点时间。
------------------- 好长的序 -------------------
IAPHelper simply wraps the API of Apple's In-App Purchase using Swift. Very lightweight and easy to use.
var productIdentifiers = Set<ProductIdentifier>() productIdentifiers.insert("product_id_1") productIdentifiers.insert("product_id_2") IAP.requestProducts(productIdentifiers) { (response, error) in if let products = response?.products where !products.isEmpty { // Get the valid products } else if let invalidProductIdentifiers = response?.invalidProductIdentifiers { // Some products id are invalid } else if error?.code == SKErrorPaymentCancelled { // User cancelled } else { // Some error happened } } IAP.purchaseProduct(productIdentifier, handler: { (productIdentifier, error) in if let identifier = productIdentifier { // The product of 'productIdentifier' purchased. } else if error?.code == SKErrorPaymentCancelled { // User cancelled } else { // Some error happened } }) IAP.restorePurchases { (productIdentifiers, error) in if !productIdentifiers.isEmpty { // Products restored } else if error?.code == SKErrorUnknown { // NOTE: if no product ever purchased, will return this error. } else if error?.code == SKErrorPaymentCancelled { // User cancelled } else { // Some error happened } } ------------------- 尾巴 -------------------
目前,这一库应用在我的 图床神器 iPic 中,主要在 macOS 10.11 和 10.12 (Sierra Beta 6) 中测试。
可以明显的感觉, macOS 10.11 和 10.12 在逻辑细节上有不同。在今年的 WWDC 上, Apple 也说了秋季新的订阅模式会上线,相信 10.12 正式上线后,和 Beta 版还是会有些微的不同。没办法,在问题中长经验、继续改进呗。届时, IAPHelper 也会相应地更新。
最后,希望 IAPHelper 别成你的坑…
1 kitalphaj 2016-08-31 08:29:25 +08:00 嗯,之前我也写了一个类似的,而且还加上了用 OpenSSL 本地验证 /或者用 Apple 服务器远程验证机制。 |
2 quietjosen OP @kitalphaj 这部分我做得不好,只是直接连 Apple 服务器验证。为了验证再搭个中间服务器,感觉有些麻烦。毕竟 iPic 目前还是个小东东,高手要是连这都要破解,我也就不说啥了。 |
3 Pandara 2016-08-31 09:41:35 +08:00 赞 |