需要帮助者实施助手类的应用内购买

时间:2019-05-21 16:30:58

标签: swift in-app-purchase

我第一次尝试使用IAP,我需要一些帮助连接点。

我的应用程序有两个不同的非消耗性IAP,如果我的应用程序本身是这样,则我在“产品”类中设置了它们:

enum IAPProduct: String{
    case tempoLevels  = ".....TempoLevels"
    case timingLevels = ".....TimingLevels"
}

然后,我通过一个这样的教程建立了一个帮助器类:

class IAPService: NSObject  {
    private override init() {}
    static let shared = IAPService()

    var products = [SKProduct]()
    let paymentQueue = SKPaymentQueue.default()

    func getProducts() {
        let products: Set = [IAPProduct.tempoLevels.rawValue, IAPProduct.timingLevels.rawValue]

        let request = SKProductsRequest(productIdentifiers: products)
        request.delegate = self
        request.start()
        paymentQueue.add(self)
    }

    func purchase(product: IAPProduct) {
        guard let productToPurchase = products.filter({ $0.productIdentifier == product.rawValue }).first
            else {return}
        let payment = SKPayment(product: productToPurchase)
        paymentQueue.add(payment)
    }
}

extension IAPService: SKProductsRequestDelegate {
    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        self.products = response.products
        for product in response.products {
            print(product.localizedTitle)
        }
    }   
}

extension IAPService: SKPaymentTransactionObserver {
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            print(transaction.transactionState.status(), transaction.payment.productIdentifier)
            switch transaction.transactionState {
            case .purchasing: break
            default: queue.finishTransaction(transaction)
            }
        }
    }
}

extension SKPaymentTransactionState {
    func status() -> String{
        switch self{
            case .deferred: return "deferred"
            case .failed: return "failed"
            case .purchased: return "purchased"
            case .purchasing: return "purchasing"
            case .restored: return "restored" 

        @unknown default:
            return("Something is wrong...")
        }
    }
}

购买产品后,我想在另一个视图控制器中触发某些功能。我该怎么做?

我有点在其中一个视图控制器中设置了一个函数,以检查其中一个视图控制器中的UserDefaults,如下所示:

func isPurchased() -> Bool {
    let purchaseStatus = UserDefaults.standard.bool(forKey: ??)

    if purchaseStatus == true {
        print("Previously purchased!")
        return true
    }else{
        print("Never purchased!")
        return false
    }
}

我不确定如何使用此功能,但是如果可以的话,我的应用程序中有可能的代码可以使用它。

1 个答案:

答案 0 :(得分:1)

您可以直接使用SKPaymentTransactionState进行购买检查。

如果要Bool价值,您可以在创建交易后像t​​hi一样致电

func isPurchased(transaction: SKPaymentTransaction) -> Bool {
  return transaction.transactionState == .purchased
}

或直接从SKPaymentTransactionState的扩展名

func isPurchased(transaction: SKPaymentTransaction) -> String {
      return transaction.status
    }

同时,您永远不要在布尔值中存储一个布尔值,以检查用户是否已在UserDefaults中购买了应用内购买。用户可以很容易地更改它(无需越狱),并免费获得您的好东西! 您应该使用钥匙串代替UserDefaults。