4.4 C
New York
Monday, January 27, 2025

ios – Is it essential to preserve Transaction listener for consumable buy in StoreKit 2?


I’m implementing consumable buy e.g. person can purchase 200 cash for $1.99 anytime. I’ve carried out buy movement however Xcode is giving me following warning:

Making a purchase order with out listening for transaction updates dangers
lacking profitable purchases. Create a Job to iterate
Transaction.updates at launch.

extension AppDelegate {
    
    func observeTransactionUpdates() {
        Job {
            for await end in Transaction.updates {
                change outcome {
                case .verified(let transaction):
                    if transaction.productID == ProductID.scans_200_price_1_95.rawValue {
                        print("Grant 200 scans")
                        print("Transaction id: (transaction.id)")
                        await transaction.end()
                    }
                case .unverified(_, let error):
                    print("Unverified transaction: (error.localizedDescription)")
                }
            }
        }
    }
}

Now my query is, in case of consumable buy, what’s objective of this observations? Consumer shouldn’t be going to unlock one thing till particular date however shopping for cash at any level of time.

I’m doing following when person hit the acquisition button:

func buy(_ product: Product) async {
    
    do {
        let outcome = strive await product.buy()
        
        change outcome 
        {
        case let .success(.verified(transaction)):
            print("Grant 200 scans")
            print("Transaction id: (transaction.id)")
            await transaction.end()
            
        case let .success(.unverified(_, error)):
            print("Buy was profitable however transaction/receipt cannot be verified. This cellphone may very well be a jailbroken.")
            print("Error: (error.localizedDescription)")
            break
            
        case .pending:
            print("Transaction is ready on SCA (Robust Buyer Authentication) or approval from Ask to Purchase")
            break
            
        case .userCancelled:
            print("Buy was cancelled!")
            break
            
        @unknown default:
            print("Buy was failed!")
            break
        }
    } 
    catch {
        print("Buy was failed!")
    }
}

You may see Grant 200 scans is executed at two locations:

  1. When person hit the acquisition button
  2. When observeTransactionUpdates is named on App launch

Will it grans person 200 tokens two occasions? The place ought to I grant person 200 tokens?

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles