在 itunesconnect.apple.com,中配置内购项目,如图右侧有一个(查看公共秘钥)(验证购买时需要使用)
点击加号新建购买项目
根据你们产品的不同选择对应的项目
创建就很简单了,每一项都有介绍这里就不多说了
创建沙箱技术测试员用于内购测试使用
内容可以随便填写,需要注意的是 邮箱 和 密码需要记住(后面需要使用)
import 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
}
func getList() {
SwiftyStoreKit.retrieveProductsInfo(["图1 内购项目的 产品ID 这个一般存储在服务器里"]) { 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 {
print("Invalid product identifier: \(invalidProductId)")
} else {
print("Error: \(result.error)")
}
}
}
这里是我的列表,因为就创建一个内购项目所以就一个
购买 需要使用刚你在沙箱测试添加的邮箱密码登录(退出AppStore账号),购买的时候会提示你输入账号密码,此账号非appid账号,不能登录在appstore 走成功就说明购买成功了,简单点就是扣钱了,这时候是没有验证处理的。
SwiftyStoreKit.purchaseProduct("产品ID", 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 已经写好的类) AppleReceiptValidator
// .production 苹果验证 .sandbox 本地验证
let receipt = AppleReceiptValidator(service: .production)
let password = "公共秘钥"
SwiftyStoreKit.verifyReceipt(using: receipt, password: password, completion: { (result) in
switch result {
case .success(let receipt):
print("receipt--->\(receipt)")
break
case .error(let error):
print("error--->\(error)")
break
}
})
AppPurchasesDemo is released under the MIT license. See LICENSE for details.