I use the code below for a non-consumable in-app purchase in my apps. Has anybody worked out how to handle this without using any of the deprecated items?
SKPaymentQueue - deprecated, SKPayment - deprecated, SKProduct - deprecated, transactionState - deprecated, SKPaymentTransaction - deprecated, finishTransaction - deprecated
func paymentQueue(_ queue: SKPaymentQueue, shouldAddStorePayment payment: SKPayment, for product: SKProduct) -> Bool
{
true
}
func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction])
{
for transaction in transactions
{
switch transaction.transactionState
{
case .purchasing:
break
case .purchased:
SKPaymentQueue.default().finishTransaction(transaction)
// Hide the restore button
navigationItem.setRightBarButton(nil, animated: true)
// Set the ProVerion in the Db to true
IAPHandler.setProVersionToPurchased()
// Also hide the Purchase button
UIView.animate(withDuration: 1.0, animations: { [weak self] in
self?.purchaseBtn_Outlet.alpha = 0
}) { [weak self] (success) in
if self!.theDevice.isOneOf(K.Device_Groups.SE_3_iPhone8) {
self?.segControlTop_Constraint.constant = 10
} else if self!.theDevice.isPhone {
self?.segControlTop_Constraint.constant = 30
}
}
case .failed:
if let error = transaction.error
{
let errorDescription = error.localizedDescription
print("Transaction failed due to error: \(errorDescription)")
}
case .restored:
SKPaymentQueue.default().finishTransaction(transaction)
// Hide the restore button
navigationItem.setRightBarButton(nil, animated: true)
// Set the ProVerion in the Db to true
IAPHandler.setProVersionToPurchased()
// Also hide the Purchase button
UIView.animate(withDuration: 1.0, animations: { [weak self] in
self?.purchaseBtn_Outlet.alpha = 0
}) { [weak self] (success) in
if self!.theDevice.isOneOf(K.Device_Groups.SE_3_iPhone8) {
self?.segControlTop_Constraint.constant = 10
} else if self!.theDevice.isPhone {
self?.segControlTop_Constraint.constant = 30
}
}
case .deferred:
break
@unknown default:
if let error = transaction.error
{
let errorDescription = error.localizedDescription
print("Transaction failed due to error: \(errorDescription)")
}
break
}
}
}
// Sets the purchase to true in the Db
class IAPHandler: NSObject {
//Get the ProVersion Status
static func isProVersionPurchased() -> Bool
{
let VC_String = "IAPHandler"
var theStatus = false
do {
let settings = try Database.shared.databaseConnection!.read { db in
try My_Settings.fetchOne(db)
}
let theStatusText = settings?.ProVersion ?? "false"
theStatus = theStatusText == "true" ? true : false
} catch {
print("Getting the ProVersion Status failed! \(VC_String) \(error)")
}
return theStatus
}
// Set ProVersion to true.
static func setProVersionToPurchased()
{
let VC_String = "IAPHandler"
do {
try Database.shared.databaseConnection!.write { db in
try db.execute(sql: "UPDATE My_Settings SET ProVersion = :proVersion WHERE Settings_ID = :id",
arguments: ["proVersion": "true", "id": 1])
}
} catch {
print("Update set pro version, failed! \(VC_String)s \(error)")
}
}
}// End of class