开源了自己封装 Mac In-App Purchase 的 Swift 库: IAPHelper

2016-08-31 00:37:37 +08:00
 quietjosen

相信做过的朋友都知道, Mac App Store 是个坑,而 In-App Purchase 则是坑中之坑…

刚开始做 Mac 应用内支付时,看苹果的示例都是 OC 、而不是 Swift 的,于是偷懒直接用了 SwiftyStoreKit 确实省了不时间,但也确实有些坑。关键是,出现问题之后,都不知道是这个库本身的问题,还是 Apple 的问题。再加上很多朋友在 iPic 升级遇到的无法升级、无法恢复等等诡异的问题,实在是头大。

在 iPic 1.2.0 新版中,痛定思痛,我还是决定啃啃 IAP 这个骨头。哪怕是踩坑,也要踩自己挖的坑,而不是掉进别人的…

啃完之后,不敢说对 IAP 对多深的了解,但也足够来开发了。想着这样无趣的过程,第位开发者都要经历一遍,便觉得更无趣。于是,就把自己做的库 开源出来给大家用,取名 IAPHelper ,希望能节约大家一点点时间。

------------------- 好长的序 -------------------

What's IAPHelper

IAPHelper simply wraps the API of Apple's In-App Purchase using Swift. Very lightweight and easy to use.

IAPHelper Usage

Request Product List

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
	}
}

Purchase Product

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

Restore

 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 别成你的坑…

2587 次点击
所在节点    分享创造
3 条回复
kitalphaj
2016-08-31 08:29:25 +08:00
嗯,之前我也写了一个类似的,而且还加上了用 OpenSSL 本地验证 /或者用 Apple 服务器远程验证机制。
quietjosen
2016-08-31 09:19:58 +08:00
@kitalphaj 这部分我做得不好,只是直接连 Apple 服务器验证。为了验证再搭个中间服务器,感觉有些麻烦。毕竟 iPic 目前还是个小东东,高手要是连这都要破解,我也就不说啥了。
Pandara
2016-08-31 09:41:35 +08:00

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/302805

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX