成功购买应用程序后,iOS应用程序崩溃 - Swift 3,Xcode 8

时间:2016-12-30 23:53:07

标签: ios swift swift3 in-app-purchase

我创建了一个应用内购买,以从应用中删除广告。在我的测试应用程序中,它工作正常,但现在我已将它添加到主应用程序,它将无法正常工作。我可以很好地完成购买选项,我收到消息说“你已经完成了设置,你的购买成功了”但是在应用程序崩溃之后会立即进行。

这是我的代码:

import UIKit
import StoreKit

class PurchaseViewController: UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate {

    @IBOutlet var statusLabel: UILabel!
    @IBOutlet var activityIndicator: UIActivityIndicatorView!
    @IBOutlet var restorePurchasesReference: UIButton!
    @IBOutlet var removeAdsReference: UIButton!

    var product: SKProduct?
    var productID = "com.joshbury.GTAVCheats.RemoveAds"

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.

        statusLabel.isHidden = true        
        statusLabel.text = "Remove ads to enjoy an ad free experience!"

        activityIndicator.isHidden = true

        disableRestorePurchasesButton()
        disableRemoveAdsButton()

        SKPaymentQueue.default().add(self)

        getPurchaseInfo()

    }

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

    func getPurchaseInfo() {

        if SKPaymentQueue.canMakePayments() {

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

            request.delegate = self
            request.start()

            activityIndicator.isHidden = false
            activityIndicator.startAnimating()

        } else {

            statusLabel.isHidden = false
            statusLabel.text = "In app purchase is currently unavailable"

        }

    }

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

        var products = response.products

        if (products.count != 0) {

            product = products[0]

            statusLabel.isHidden = false
            statusLabel.text = "Remove ads to enjoy an ad free experience!"

            activityIndicator.isHidden = true
            activityIndicator.stopAnimating()

            enableRestorePurchasesButton()
            enableRemoveAdsButton()

        } else {

            statusLabel.isHidden = false
            statusLabel.text = "Product not found"

        }

        let invalids = response.invalidProductIdentifiers

        for product in invalids {

            print("Invalid Product...\(product)")

            statusLabel.isHidden = false
            statusLabel.text = "Invalid Product"

            activityIndicator.isHidden = true
            activityIndicator.stopAnimating()

        }

    }

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

        for transaction in transactions {

            switch transaction.transactionState {

            case SKPaymentTransactionState.purchased:

                SKPaymentQueue.default().finishTransaction(transaction)

                self.removeAds()

                statusLabel.isHidden = false
                statusLabel.text = "Ads were successfully removed!"

                activityIndicator.isHidden = true
                activityIndicator.stopAnimating()

                disableRestorePurchasesButton()
                disableRemoveAdsButton()

            case SKPaymentTransactionState.restored:

                SKPaymentQueue.default().finishTransaction(transaction)

                self.removeAds()

                statusLabel.isHidden = false
                statusLabel.text = "Restore complete, ads were successfully removed!"

                activityIndicator.isHidden = true
                activityIndicator.stopAnimating()

                disableRestorePurchasesButton()
                disableRemoveAdsButton()

            case SKPaymentTransactionState.failed:

                SKPaymentQueue.default().finishTransaction(transaction)

                statusLabel.isHidden = false
                statusLabel.text = "Ads were not removed!"

                activityIndicator.isHidden = true
                activityIndicator.stopAnimating()

            default:

                break

            }

        }

    }

    func paymentQueue(_ queue: SKPaymentQueue, restoreCompletedTransactionsFailedWithError error: Error) {

        statusLabel.isHidden = false
        statusLabel.text = "Restore failed!"

        activityIndicator.isHidden = true
        activityIndicator.stopAnimating()

    }

    func removeAds() {

        let appDelegate = UIApplication.shared.delegate as! AppDelegate

        appDelegate.mainViewController!.removeAds()
        appDelegate.playstationViewController!.removeAds()
        appDelegate.xboxViewController!.removeAds()
        appDelegate.pcViewController!.removeAds()
        appDelegate.mapViewController!.removeAds()

        let save = UserDefaults.standard

        save.set(true, forKey: "adsRemoved")
        save.synchronize()

    }

    func disableRestorePurchasesButton() {

        restorePurchasesReference.isEnabled = false
        restorePurchasesReference.setTitleColor(UIColor.init(red: 209/255, green: 209/255, blue: 209/255, alpha: 0.5), for: UIControlState())

    }

    func enableRestorePurchasesButton() {

        restorePurchasesReference.isEnabled = true
        restorePurchasesReference.setTitleColor(UIColor.white, for: UIControlState())

    }

    func disableRemoveAdsButton() {

        removeAdsReference.isEnabled = false
        removeAdsReference.setTitleColor(UIColor.init(red: 209/255, green: 209/255, blue: 209/255, alpha: 0.5), for: UIControlState())

    }

    func enableRemoveAdsButton() {

        removeAdsReference.isEnabled = true
        removeAdsReference.setTitleColor(UIColor.white, for: UIControlState())

    }

    @IBAction func restorePurchases(_ sender: Any) {

        SKPaymentQueue.default().add(self)
        SKPaymentQueue.default().restoreCompletedTransactions()

        statusLabel.isHidden = true

        activityIndicator.isHidden = false
        activityIndicator.startAnimating()

        DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(60)) {

            if self.activityIndicator.isAnimating == true {

                self.statusLabel.isHidden = false
                self.statusLabel.text = "Error"

                self.activityIndicator.isHidden = true
                self.activityIndicator.stopAnimating()

            }

        }

    }

    @IBAction func removeAds(_ sender: Any) {

        let payment = SKPayment(product: product!)

        SKPaymentQueue.default().add(payment)

        statusLabel.isHidden = true

        activityIndicator.isHidden = false
        activityIndicator.startAnimating()

    }


}

Aksing to get it again for free Screenshot

I get this alert, then it crashes Screenshot

0 个答案:

没有答案