My application offers IAP. On the list screen a Buy button is generated as an Accessory. Pressing Buy starts the IAP process. It goes well to the end (product is now purchased). Instead of generating a Checkmark in the place of the Buy after purchase, I generate a Download button, as an Accessory again. Pressing the Download button should save the product content and then a Checkmark should appear. At the moment, the Download button produces the crash with "unrecognized selector sent to instance".
In ProductTableViewCell swift which is for managing the View Cell, I have the following :
// Préparation de l'affichage de l'accessoire
var product: SKProduct? {
didSet {
guard let product = product else { return }
let defaults = UserDefaults.standard
let statusCurrentProduct = defaults.string(forKey: product.productIdentifier)
//print("Accesory pour \(product.productIdentifier)")
// Si produit déjà acheté, afficher une checkmark ou un Download
if MyBridgeStoreProducts.store.isProductPurchased(product.productIdentifier) {
if statusCurrentProduct == "Purchased" { // Acheté mais Pas encore sauvé
//print("générer un bouton Save")
accessoryType = .none
accessoryView = self.newDownloadButton()
}
else {
accessoryType = .checkmark // "Saved"
accessoryView = nil
}
// Si produit achetable, affichage Buy button
} else if IAPHelper.canMakePayments() {
//ProductTableViewCell.priceFormatter.locale = product.priceLocale WARNING: garder pour la syntaxe
//sérieLabel.text = ProductTableViewCell.priceFormatter.string(from: product.price) WARNING: garder pour la syntaxe
accessoryType = .none
accessoryView = self.newBuyButton()
} else { // Achat non autorisé
// detailTextLabel?.text = "Not available" // WARNING: code à conserver pour référence, mais je ne gère pas ce statut
}
}
}
Then the code for newDownloadButton is :
// Description du bouton Download
func newDownloadButton() -> UIButton {
let button = UIButton(type: .system)
button.setTitleColor(tintColor, for: .normal)
button.setTitle(NSLocalizedString("buttonDowload", tableName: "Magasin", value: "Download", comment: " "), for: .normal)
button.addTarget(self, action: #selector(ProductTableViewController.downloadButtonTapped(_:)), for: .touchUpInside)
button.sizeToFit()
return button
}
Then, in ProductTableViewController swift which manages the VC, I have the code for downloadButtonTapped as is :
// Action func pour bouton Download
@objc func downloadButtonTapped(_ sender: AnyObject) {
// Index de la ligne sélectionnée
let indexPath = tableView.indexPathForSelectedRow
// Recherche dans les User Defaults du produit sélectionné
let selectedProduct: String = headers[(indexPath?.section)!].série[(indexPath?.row)!].sérieAscId
let defaults = UserDefaults.standard
var statusCurrentProduct = defaults.string(forKey: selectedProduct)
if statusCurrentProduct == "Purchased" { // Pas encore sauvé WARNING: normalement, c'est toujours du Purchased (on pourrait supprimer le if)
// Sauvegarde de la série
let title: String = "Information"
let message: String = NSLocalizedString("msgInformationSave", tableName: "Magasin", value: "Saving the purchased series on this device", comment: " ")
showAlert(message: message, title: title)
saveSérie()
// flag le Produit acheté et sauvé
statusCurrentProduct = "Saved"
defaults.set(statusCurrentProduct, forKey: selectedProduct)
// Reload la table pour avoir la checkmark
tableView.reloadData()
}
}
My question is simple, what's wrong above to clear that crash. Thanks for your help.