相信做過的朋友都知道绒疗, Mac App Store 是個(gè)坑嗅定,而 In-App Purchase 則是坑中之坑…
剛開始做 Mac 應(yīng)用內(nèi)支付時(shí)橄教,看蘋果的示例都是 OC 者填、而不是 Swift 的浩村,于是偷懶直接用了 SwiftyStoreKit 確實(shí)省了不時(shí)間,但也確實(shí)有些坑占哟。關(guān)鍵是心墅,出現(xiàn)問題之后,都不知道是這個(gè)庫(kù)本身的問題榨乎,還是 Apple 的問題怎燥。再加上很多朋友在 iPic 升級(jí)遇到的無(wú)法升級(jí)、無(wú)法恢復(fù)等等詭異的問題蜜暑,實(shí)在是頭大铐姚。
在 iPic 1.2.0 新版中,痛定思痛肛捍,我還是決定啃啃 IAP 這個(gè)骨頭隐绵。哪怕是踩坑,也要踩自己挖的坑拙毫,而不是掉進(jìn)別人的…
啃完之后依许,不敢說(shuō)對(duì) IAP 對(duì)多深的了解,但也足夠來(lái)開發(fā)了缀蹄。想著這樣無(wú)趣的過程峭跳,第位開發(fā)者都要經(jīng)歷一遍,便覺得更無(wú)趣缺前。于是蛀醉,就把自己做的庫(kù) 開源出來(lái)給大家用,取名 IAPHelper 衅码,希望能節(jié)約大家一點(diǎn)點(diǎn)時(shí)間拯刁。
------------------- 好長(zhǎng)的序 -------------------
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
}
}
------------------- 尾巴 -------------------
目前,這一庫(kù)應(yīng)用在我的 圖床神器 iPic 中逝段,主要在 macOS 10.11 和 10.12 (Sierra Beta 6) 中測(cè)試筛璧。
可以明顯的感覺, macOS 10.11 和 10.12 在邏輯細(xì)節(jié)上有不同惹恃。在今年的 WWDC 上夭谤, Apple 也說(shuō)了秋季新的訂閱模式會(huì)上線,相信 10.12 正式上線后巫糙,和 Beta 版還是會(huì)有些微的不同朗儒。沒辦法,在問題中長(zhǎng)經(jīng)驗(yàn)、繼續(xù)改進(jìn)唄醉锄。屆時(shí)乏悄, IAPHelper 也會(huì)相應(yīng)地更新。
最后恳不,希望 IAPHelper 別成你的坑…