让我的应用内购买在Swift中工作的问题

时间:2017-03-25 00:30:36

标签: ios swift in-app-purchase

我正在尝试在我的代码中添加两个应用内购买。 它适用于第一个(removeAds)但当我尝试添加第二个时,我得到错误,没有任何作用。

我附上了下面的完整代码。我需要修复什么来获得应用程序购买中的第二个(unlockPhrases)才能正常工作,哪里出错?

@IBOutlet var unlockPhrasesLabel: UILabel!
@IBOutlet var unlockPhrasesResult: UILabel!
@IBOutlet var removeAdsLabel: UILabel!
@IBOutlet var removeAdsResult: UILabel!

var activeProduct: SKProduct?

@IBAction func removeAds(_ sender: Any) {
    if let activeProduct = activeProduct {
        print("Buying \(activeProduct.productIdentifier)")

        let payment = SKPayment(product: activeProduct)
        SKPaymentQueue.default().add(payment)
    } else {
          print("No product")
    }
}

//Added
@IBAction func unlockPhrases(_ sender: Any) {
    if let activeProduct = activeProduct {
        print("Buying \(activeProduct.productIdentifier)")

        let payment = SKPayment(product: activeProduct)
        SKPaymentQueue.default().add(payment)
    } else {
        print("No product")
    }
}

//Added ended
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
    print("Loaded products")

    for product in response.products {
        print("Product: \(product.productIdentifier), \(product.localizedTitle), \(product.price.floatValue)")

        removeAdsLabel.text = "Buy \(product.localizedTitle)"
        activeProduct = product
    }
}

//Added
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
    print("Loaded products")

    for product in response.products {
        print("Product: \(product.productIdentifier), \(product.localizedTitle), \(product.price.floatValue)")

        unlockPhrasesLabel.text = "Buy \(product.localizedTitle)"
        activeProduct = product
    }
}

//added ended
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction in transactions {
        switch (transaction.transactionState) {
        case .purchased:
            SKPaymentQueue.default().finishTransaction(transaction)
            print("Purchased")
            removeAdsResult.text = "Purchased"
        case .failed:
            SKPaymentQueue.default().finishTransaction(transaction)
            print("Failed")
            removeAdsResult.text = "Failed"
        default:
            break
        }
    }
}

//Added
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
    for transaction in transactions {
        switch (transaction.transactionState) {
        case .purchased:
            SKPaymentQueue.default().finishTransaction(transaction)
            print("Purchased")
            unlockPhrasesResult.text = "Purchased"
        case .failed:
            SKPaymentQueue.default().finishTransaction(transaction)
            print("Failed")
            unlockPhrasesResult.text = "Failed"
        default:
            break
        }
    }
}

//added ended
override func viewDidLoad() {
    super.viewDidLoad()

    SKPaymentQueue.default().add(self)

    let productIdentifiers: Set<String> = ["removeAds", "mensPhrases"]

    let productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
    productsRequest.delegate = self
    productsRequest.start()
}

1 个答案:

答案 0 :(得分:0)

您只能拥有一个saveToken({data}){ let {userId, token, expires} = data.login; storeLoginToken(userId, token, expires); history.push('/dogs'); } setErrors(error){ this.setState('error', error.graphQLErrors); } handleSubmit(event) { event.preventDefault(); let {email, password} = this.state; let {history} = this.props; let clientKey = getClientKey(); this.props.mutate({ variables: { email, password, clientKey } }) .then(this.saveToken.bind(this)) .catch(this.setErrors.bind(this)) } 方法和一个paymentQueue:updatedTransactions:`method。

您需要查看传入的参数以确定与之关联的产品。然后,您可以正确更新您的应用状态。

productRequest:didReceive:

对另一种方法进行类似的更改。