Thread 1: Fatal error

I am trying to integrate in app purchases to remove ads in my app. I am following a youtube tutorial. Hovever I am getting a fatal error. Here is my code. Error on line 7.



class HomeViewController: UIViewController, SKPaymentTransactionObserver, SKProductsRequestDelegate {

var product : SKProduct?
    var productID = "NolanZ.Winer.removeads"

@IBAction func removeAds(_ sender: Any) {
        let payment = SKPayment(product: product!) //getting error here
        SKPaymentQueue.default().add(payment)
    }

 func getPurchaseInfo() {
        if SKPaymentQueue.canMakePayments() {
            let request = SKProductsRequest(productIdentifiers: NSSet(objects: self.productID) as! Set)
            request.delegate = self
            request.start()
        }
    }
    
    func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
        var products = response.products
        
        if (products.count == 0) {
            
            SVProgressHUD.showError(withStatus: "Check Your Internet Connection And Try Again")
        } else {
            buyButton.isEnabled = true
        }
        let invalids = response.invalidProductIdentifiers
        
        for product in invalids {
            SVProgressHUD.showError(withStatus: "Product Not Found")
        }
    }
    
    func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
        for transaction in transactions {
            switch transaction.transactionState {
            case SKPaymentTransactionState.purchased:
                
                SKPaymentQueue.default().finishTransaction(transaction)
                SVProgressHUD.showSuccess(withStatus: "Purchase Successful")
                buyButton.isEnabled = false
                adsRemoved = true
              
                defaults.set(adsRemoved, forKey: "ads")
                
            case SKPaymentTransactionState.failed:
                SKPaymentQueue.default().finishTransaction(transaction)
                SVProgressHUD.showError(withStatus: "Purchase Not Successful")
                
            default:
                break
            }
        }
    }
}


I know that the error means that there is no value and that it's nil. However I don't know what to do from here.


Thanks for any help provided!!!!

Accepted Reply

You define product as an optional (SKProduct?)


So it is initilized as nil.


I see nowhere where you set a value, with a statement as

product = …

If you have an initilizer of SKProduct from its productID, could be

product = SKProduct(productID)


What should be its value ?


To avoid crash

@IBAction func removeAds(_ sender: Any) { 
       if let product = product {
             let payment = SKPayment(product: product) //getting error here 
             SKPaymentQueue.default().add(payment) 
     }
    }



IBAction will do nothing, but not crash.

However, to make it useful, you need to give a real value to product.

Replies

You define product as an optional (SKProduct?)


So it is initilized as nil.


I see nowhere where you set a value, with a statement as

product = …

If you have an initilizer of SKProduct from its productID, could be

product = SKProduct(productID)


What should be its value ?


To avoid crash

@IBAction func removeAds(_ sender: Any) { 
       if let product = product {
             let payment = SKPayment(product: product) //getting error here 
             SKPaymentQueue.default().add(payment) 
     }
    }



IBAction will do nothing, but not crash.

However, to make it useful, you need to give a real value to product.