我有几项应用内购买。我使用以下代码:
@IBAction func purchaseFull(_ sender: Any) {
purchase = "purchaseFull"
product_id = "purchaseFull"
print("About to fetch the product...")
//self.loading.startAnimating()
SKPaymentQueue.default().add(self)
// Can make payments
if (SKPaymentQueue.canMakePayments())
{
let productID:NSSet = NSSet(object: self.product_id!);
let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
productsRequest.delegate = self;
productsRequest.start();
print("Fetching Products");
}else{
print("Can't make purchases");
}
}
@IBAction func purchase(_ sender: Any) {
purchase = "purchase"
product_id = "purchase\(index)"
print("About to fetch the product...")
//self.loading.startAnimating()
SKPaymentQueue.default().add(self)
// Can make payments
if (SKPaymentQueue.canMakePayments())
{
let productID:NSSet = NSSet(object: self.product_id!);
let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
productsRequest.delegate = self;
productsRequest.start();
print("Fetching Products");
}else{
print("Can't make purchases");
}
}
func productsRequest (_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
let count : Int = response.products.count
if (count>0) {
let validProduct: SKProduct = response.products[0] as SKProduct
if (validProduct.productIdentifier == self.product_id) {
print(validProduct.localizedTitle)
print(validProduct.localizedDescription)
print(validProduct.price)
buyProduct(product: validProduct);
} else {
print(validProduct.productIdentifier)
}
} else {
print("nothing")
}
}
func buyProduct(product: SKProduct){
print("Sending the Payment Request to Apple");
let payment = SKPayment(product: product)
SKPaymentQueue.default().add(payment);
//self.loading.stopAnimating()
}
func request(_ request: SKRequest, didFailWithError error: Error) {
print("Error Fetching product information");
//self.loading.stopAnimating()
}
func paymentQueue(_ queue: SKPaymentQueue,
updatedTransactions transactions: [SKPaymentTransaction]) {
print("Received Payment Transaction Response from Apple");
for transaction:AnyObject in transactions {
if let trans:SKPaymentTransaction = transaction as? SKPaymentTransaction{
switch trans.transactionState {
case .purchased:
print("Product Purchased");
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
// Handle the purchase
if purchase == "purchase" {
UserDefaults.standard.set(true , forKey: "purchase\(index)")
}
if purchase == "purchaseFull" {
UserDefaults.standard.set(true , forKey: "purchaseFull")
}
viewDidLoad()
break;
case .failed:
print("Purchased Failed");
SKPaymentQueue.default().finishTransaction(transaction as! SKPaymentTransaction)
break;
case .restored:
print("Already Purchased");
SKPaymentQueue.default().restoreCompletedTransactions()
// Handle the purchase
//UserDefaults.standard.set(true , forKey: "purchased")
viewDidLoad()
break;
default:
break;
}
}
}
}
@IBAction func restoreAction(_ sender: Any) {
SKPaymentQueue.default().add(self)
if (SKPaymentQueue.canMakePayments()) {
SKPaymentQueue.default().restoreCompletedTransactions()
}
}
func requestDidFinish(_ request: SKRequest) {
}
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
print("transactions restored")
for transaction in queue.transactions {
let t: SKPaymentTransaction = transaction
let prodID = t.payment.productIdentifier as String
if prodID == "purchaseFull" {
print("action for restored")
queue.finishTransaction(t)
UserDefaults.standard.set(true , forKey: "purchaseFull")
} else if prodID == "purchase0" {
print("action0")
queue.finishTransaction(t)
UserDefaults.standard.set(true , forKey: "purchase0")
} else if prodID == "purchase1" {
print("action1")
queue.finishTransaction(t)
UserDefaults.standard.set(true , forKey: "purchase1")
} else if prodID == "purchase2" {
print("action2")
queue.finishTransaction(t)
UserDefaults.standard.set(true , forKey: "purchase2")
} else if prodID == "purchase3" {
print("action3")
queue.finishTransaction(t)
UserDefaults.standard.set(true , forKey: "purchase3")
} else if prodID == "purchase4" {
print("action4")
queue.finishTransaction(t)
UserDefaults.standard.set(true , forKey: "purchase4")
} else if prodID == "purchase5" {
print("action5")
queue.finishTransaction(t)
UserDefaults.standard.set(true , forKey: "purchase5")
}
}
cancelAction((Any).self)
}
但是我有一个问题。当我单击我的购买按钮时,我的代码调用此函数-paymentQueueRestoreCompletedTransactionsFinished(_队列:SKPaymentQueue)和if else
检查有效。我的Userdefaults
设置了true
作为密钥。结果,用户取消阻止内容,但不支付购买费用。如何解决?
答案 0 :(得分:2)
在两个位置更新UserDefaults时,调试您的工作有点困难,并且购买跟踪代码与购买代码紧密相关。
我将购买和跟踪购买的关注点分开,因此您只需要在一个地方跟踪,更新或解锁它们即可。像这样...
首先,我将所有iTunesConnect购买代码分为单独的谨慎类(一个用于iTunesStore,一个用于iTunesStore回调观察器),创建表示购买状态和错误状态的模型,并创建回调以通知应用程序产品验证和购买流程中发生的重大操作。
回调协议看起来像这样:
import StoreKit
/// Defines callbacks that will occur when products are being validated with the iTunes Store.
protocol iTunesProductStatusReceiver: class {
func didValidateProducts(_ products: [SKProduct])
func didReceiveInvalidProductIdentifiers(_ identifiers: [String])
}
/// Defines callbacks that occur during the purchase or restore process
protocol iTunesPurchaseStatusReceiver: class {
func purchaseStatusDidUpdate(_ status: PurchaseStatus)
func restoreStatusDidUpdate(_ status: PurchaseStatus)
}
我的iTunesStore类如下所示,它将处理与iTunesConnect(或现在的AppStoreConnect)的所有交互:
import Foundation
import StoreKit
class iTunesStore: NSObject, SKProductsRequestDelegate {
weak var delegate: (iTunesProductStatusReceiver & iTunesPurchaseStatusReceiver)?
var transactionObserver: IAPObserver = IAPObserver()
var availableProducts: [SKProduct] = []
var invalidProductIDs: [String] = []
deinit {
SKPaymentQueue.default().remove(self.transactionObserver)
}
override init() {
super.init()
transactionObserver.delegate = self
}
func fetchStoreProducts(identifiers:Set<String>) {
print("Sending products request to ITC")
let request:SKProductsRequest = SKProductsRequest.init(productIdentifiers: identifiers)
request.delegate = self
request.start()
}
func purchaseProduct(identifier:String) {
guard let product = self.product(identifier: identifier) else {
print("No products found with identifier: \(identifier)")
// fire purchase status: failed notification
delegate?.purchaseStatusDidUpdate(PurchaseStatus.init(state: .failed, error: PurchaseError.productNotFound, transaction: nil, message:"An error occured"))
return
}
guard SKPaymentQueue.canMakePayments() else {
print("Unable to make purchases...")
delegate?.purchaseStatusDidUpdate(PurchaseStatus.init(state: .failed, error: PurchaseError.unableToPurchase, transaction: nil, message:"An error occured"))
return
}
// Fire purchase began notification
delegate?.purchaseStatusDidUpdate(PurchaseStatus.init(state: .initiated, error: nil, transaction: nil, message:"Processing Purchase"))
let payment = SKPayment.init(product: product)
SKPaymentQueue.default().add(payment)
}
func restorePurchases() {
// Fire purchase began notification
delegate?.restoreStatusDidUpdate(PurchaseStatus.init(state: .initiated, error: nil, transaction: nil, message:"Restoring Purchases"))
SKPaymentQueue.default().restoreCompletedTransactions()
}
// returns a product for a given identifier if it exists in our available products array
func product(identifier:String) -> SKProduct? {
for product in availableProducts {
if product.productIdentifier == identifier {
return product
}
}
return nil
}
}
// Receives purchase status notifications and forwards them to this classes delegate
extension iTunesStore: iTunesPurchaseStatusReceiver {
func purchaseStatusDidUpdate(_ status: PurchaseStatus) {
delegate?.purchaseStatusDidUpdate(status)
}
func restoreStatusDidUpdate(_ status: PurchaseStatus) {
delegate?.restoreStatusDidUpdate(status)
}
}
// MARK: SKProductsRequest Delegate Methods
extension iTunesStore {
@objc(productsRequest:didReceiveResponse:) func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
// set new products
availableProducts = response.products
// set invalid product id's
invalidProductIDs = response.invalidProductIdentifiers
if invalidProductIDs.isEmpty == false {
// call delegate if we received any invalid identifiers
delegate?.didReceiveInvalidProductIdentifiers(invalidProductIDs)
}
print("iTunes Store: Invalid product IDs: \(response.invalidProductIdentifiers)")
// call delegate with available products.
delegate?.didValidateProducts(availableProducts)
}
}
您会注意到该类利用PurchaseStatus,PurchaseState和PurchaseError对象来向应用程序传达状态更改和更新。
这些类如下:
import Foundation
import StoreKit
enum PurchaseState {
case initiated
case complete
case cancelled
case failed
}
class PurchaseStatus {
var state:PurchaseState
var error:Error?
var transaction:SKPaymentTransaction?
var message:String
init(state:PurchaseState, error:Error?, transaction:SKPaymentTransaction?, message:String) {
self.state = state
self.error = error
self.transaction = transaction
self.message = message
}
}
public enum PurchaseError: Error {
case productNotFound
case unableToPurchase
public var code: Int {
switch self {
case .productNotFound:
return 100101
case .unableToPurchase:
return 100101
}
}
public var description: String {
switch self {
case .productNotFound:
return "No products found for the requested product ID."
case .unableToPurchase:
return "Unable to make purchases. Check to make sure you are signed into a valid itunes account and that you are allowed to make purchases."
}
}
public var title: String {
switch self {
case .productNotFound:
return "Product Not Found"
case .unableToPurchase:
return "Unable to Purchase"
}
}
public var domain: String {
return "com.myAppId.purchaseError"
}
public var recoverySuggestion: String {
switch self {
case .productNotFound:
return "Try again later."
case .unableToPurchase:
return "Check to make sure you are signed into a valid itunes account and that you are allowed to make purchases."
}
}
}
有了这些类,我们仅需再设置两部分即可设置商店,并使其易于在各个应用程序之间重复使用,而无需每次我们想在应用程序中添加应用程序购买时都重写大部分内容。
下一个是观察者,它从StoreKit接收回调,iTunesStore类应该是唯一使用此方法的类:
import Foundation
import StoreKit
class IAPObserver: NSObject, SKPaymentTransactionObserver {
// delegate to propagate status update up
weak var delegate: iTunesPurchaseStatusReceiver?
override init() {
super.init()
SKPaymentQueue.default().add(self)
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
for transaction in transactions {
switch transaction.transactionState {
case .purchasing: // Transaction is being added to the server queue
break
case .purchased: // Transaction is in queue, user has been charged. Complete transaction now
// Notify purchase complete status
delegate?.purchaseStatusDidUpdate(PurchaseStatus.init(state: .complete, error: nil, transaction: transaction, message:"Purchase Complete."))
SKPaymentQueue.default().finishTransaction(transaction)
case .failed: // Transaction was cancelled or failed before being added to the server queue
// An error occured, notify
delegate?.purchaseStatusDidUpdate(PurchaseStatus.init(state: .failed, error: transaction.error, transaction: transaction, message:"An error occured."))
SKPaymentQueue.default().finishTransaction(transaction)
case .restored: // transaction was rewtored from the users purchase history. Complete transaction now.
// notify purchase completed with status... success
delegate?.restoreStatusDidUpdate(PurchaseStatus.init(state: .complete, error: nil, transaction: transaction, message:"Restore Success!"))
SKPaymentQueue.default().finishTransaction(transaction)
case .deferred: // transaction is in the queue, but it's final status is pending user/external action
break
}
}
}
func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
guard queue.transactions.count > 0 else {
// Queue does not include any transactions, so either user has not yet made a purchase
// or the user's prior purchase is unavailable, so notify app (and user) accordingly.
print("restore queue.transaction.count === 0")
return
}
for transaction in queue.transactions {
// TODO: provide content access here??
print("Product restored with id: \(String(describing: transaction.original?.payment.productIdentifier))")
SKPaymentQueue.default().finishTransaction(transaction)
}
}
func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {
// fire notification to dismiss spinner, restore error
delegate?.restoreStatusDidUpdate(PurchaseStatus.init(state: .failed, error: error, transaction: nil, message:"Restore Failed."))
}
}
最后一个是商店类,用于管理触发购买并处理授予对已购买产品的访问权限:
enum ProductIdentifier: String {
case one = "com.myprefix.id1"
case two = "com.myprefix.id2"
static func from(rawValue: String) -> ProductIdentifier? {
switch rawValue {
case one.rawValue: return .one
case two.rawValue: return .two
default: return nil
}
}
}
class Store {
static let shared = Store()
// purchase processor
var paymentProcessor: iTunesStore = iTunesStore()
init() {
// register for purchase status update callbacks
paymentProcessor.delegate = self
validateProducts()
}
// validates products with the iTunesConnect store for faster purchase processing
// when a user wants to buy
internal func validateProducts() {
// all products to validate
let products = [
ProductIdentifier.one.rawValue,
ProductIdentifier.two.rawValue
]
paymentProcessor.fetchStoreProducts(identifiers: Set.init(products))
}
/// Purchase a product by specifying the product identifier.
///
/// - Parameter identifier: The product identifier for the product being purchased. This must belong to a valid product in the 'products' array, as this array is searched for a product with the specified identifier. If none are found this function bails.
func purchaseProduct(identifier: ProductIdentifier) {
print("purchase product: \(identifier)")
self.paymentProcessor.purchaseProduct(identifier: identifier.rawValue)
}
/// Initiates restore purchases functionality.
func restorePurchases() {
self.paymentProcessor.restorePurchases()
}
/// This function is called during the purchase/restore purchase process with each status change in the flow. If the status is complete then access to the product should be granted at this point.
///
/// - Parameter status: The current status of the transaction.
internal func processPurchaseStatus(_ status: PurchaseStatus) {
switch status.state {
case .initiated:
// TODO: show alert that purchase is in progress...
break
case .complete:
if let productID = status.transaction?.payment.productIdentifier {
// Store product id in UserDefaults or some other method of tracking purchases
UserDefaults.standard.set(true , forKey: productID)
UserDefaults.standard.synchronize()
}
case .cancelled:
break
case .failed:
// TODO: notify user with alert...
break
}
}
}
extension Store: iTunesPurchaseStatusReceiver, iTunesProductStatusReceiver {
func purchaseStatusDidUpdate(_ status: PurchaseStatus) {
// process based on received status
processPurchaseStatus(status)
}
func restoreStatusDidUpdate(_ status: PurchaseStatus) {
// pass this into the same flow as purchasing for unlocking products
processPurchaseStatus(status)
}
func didValidateProducts(_ products: [SKProduct]) {
print("Product identifier validation complete with products: \(products)")
// TODO: if you have a local representation of your products you could
// sync them up with the itc version here
}
func didReceiveInvalidProductIdentifiers(_ identifiers: [String]) {
// TODO: filter out invalid products? maybe... by default isActive is false
}
}
使用这种方法,您可以将所有iTunesConnect相关类放在一个文件夹中,并在各个项目中使用它们,而只需为要在其中使用的每个项目更新ProductIdentifer枚举和Store类。
祝你好运!希望这会有所帮助!
编辑:这是一个示例应用程序(Swift 4),其中集成了显示如何使用它的所有内容-https://github.com/appteur/eziap
编辑2 :(回答有关显示警报的评论)
您可以通过多种方式显示提醒用户的警报。
您可以通过发送Notification
并让视图控制器侦听警报来在任何视图控制器中触发警报。您可以将委托链设置到适当的视图控制器,还可以创建将传递给Store
对象的闭包,并根据状态更改进行更新。
我可能会创建一个可选的自定义警报视图控制器,并在其类型的Store
类中设置一个变量,并将其显示在最顶部的视图控制器上。
我将在启动状态的internal func processPurchaseStatus(_ status: PurchaseStatus)
函数中显示它,并在同一函数中状态改变时对其进行更新。
我将使用当前状态更新警报视图上的标签,并使其在成功购买后自动关闭或显示成功屏幕。如果交易失败,我将使用错误更新警报消息并显示一个按钮以关闭警报视图。
我使用这样的扩展名来获得最顶级的视图控制器:
import UIKit
extension UIApplication {
static func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
}
使用此方法,您可能会具有如下功能:
internal func showAlert() {
// do UI stuff on the main thread
DispatchQueue.main.async { [weak self] in
// load alert from storyboard, nib, or some method, get the topmost view controller in the controller hierarchy, only use it if it's not being dismissed
guard let alertVC = MyAlertClass.fromNib(), let topVC = UIApplication.topViewController(), topVC.isBeingDismissed == false else {
return
}
// set our local variable so we can update the message later and dismiss it more easily
self.myAlertController = alertVC
// Present the alert view controller
topVC.present(alertVC, animated: true, completion: nil)
}
}
除了执行自定义警报视图控制器外,您还可以使用警报UI创建视图子类,并将其添加到最顶部的视图控制器的视图中。