在应用内购买兑换按钮

时间:2017-06-19 19:49:29

标签: ios swift xcode

我将我的应用程序提交给苹果,但他们拒绝了,因为在应用内购买时没有兑换按钮。我怎样才能做到这一点。我制作了一个兑换按钮并将其链接到视图控制器。我从哪里去? 这是我的代码:

class StoreViewController: UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate {

@IBOutlet var redeemOutlet: UIButton!

@IBAction func redeem(_ sender: Any) {



}

@IBOutlet var productDescription: UITextView!

var product: SKProduct?

let productID = "com.myCoolAwesomeApp.mjay.noAdvertisements"

@IBOutlet var buyButton: UIButton!

@IBOutlet var returnButton: UIButton!

override func viewDidLoad() {
    super.viewDidLoad()

    buyButton.layer.cornerRadius = 5.0
    returnButton.layer.cornerRadius = 5.0

    buyButton.isEnabled = false


    SKPaymentQueue.default().add(self)
    getPurchaseInfo()


    // Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}






@IBAction func purchase(_ sender: Any) {

    let payment = SKPayment(product: product!)

    SKPaymentQueue.default().add(payment)

}


@IBAction func dismissView(_ sender: Any) {

    //self.dismiss(animated: true, completion: nil)

}



func getPurchaseInfo() {

    if SKPaymentQueue.canMakePayments() {

        let request = SKProductsRequest(productIdentifiers: NSSet(objects: self.productID) as! Set<String>)

        request.delegate = self

        request.start()


    } else {

        productDescription.text = "Please enable In App Purchases in your settings."


    }


}

func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

    var products = response.products

    if (products.count) == 0 {


        productDescription.text = "Product not found."


    } else {


        product = products[0]
        productDescription.text = "Remove all advertisements within the game for $0.99"
        buyButton.isEnabled = true


    }

    let invalids = response.invalidProductIdentifiers

    for product in invalids {

        print("Product not found: \(product)")

    }


}


func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

    for transaction in transactions {

        switch transaction.transactionState {
        case SKPaymentTransactionState.purchased:
            SKPaymentQueue.default().finishTransaction(transaction)

            productDescription.text = "Purchase Successful"
            buyButton.isEnabled = false

            let save = UserDefaults.standard
            save.set(true, forKey: "Purchase")
            save.synchronize()

        case SKPaymentTransactionState.failed:
            SKPaymentQueue.default().finishTransaction(transaction)
            productDescription.text = "Purchase Failed. Try again later."

        default:
            break

        }


    }



}



}

0 个答案:

没有答案