I am setting up in app purchases in my app to remove ads. I am following a youtube tutorial. In the tutorial on line 3 it does not provide a value to product. I know that that is the product of removing the ads. However I don't know what to set that value as.
I was told to do this:
product = SKProduct(productID)
But I got an error.
Here is my code:
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
}
}
}
}
Thanks for all help!