Ankur Patel
Ankur Patel

Reputation: 477

how to assign skpayment applicationusername

can we assign skpayment applicationusername?

When in app purchase is completed, I got a null applicationUsername in theSKPayment.

How do I assign a value to SKPayment applicationUsername?

Upvotes: 4

Views: 5007

Answers (3)

Paulw11
Paulw11

Reputation: 114865

You can't assign a value to the property of an SKPayment; When you create the SKMutablePayment instance in order to submit the purchase request, your app can provide an opaque value that represents the application user id. This enables the iTunes store to detect irregular activity.

Despite its name, the applicationUsername property is not meant to hold the actual username.

A suggested approach is shown in the In-App Purchasing Programming Guide

Note that the value you assigned may not be present in the SKPayment you receive on the transaction queue.

Important

The applicationUsername property is not guaranteed to persist between when you add the payment transaction to the queue and when the queue updates the transaction. Do not attempt to use this property for purposes other than providing fraud detection.

Upvotes: 2

guru
guru

Reputation: 2817

You can assign it like :

   // MARK: purchase SKProduct from store
    @objc func buy(product: SKProduct, withHandler handler: @escaping ((_ success : Bool , _ error :RiscoIAPManagerError? ) -> Void)) {
        let payment = SKMutablePayment(product: product)
        
        payment.applicationUsername = "applicationUsername"
        SKPaymentQueue.default().add(payment)
        // Keep the completion handler.
        productBuyComplition = handler
    }

And can get it again in

     func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
                    print(transaction.payment.applicationUsername as Any)

}

Upvotes: 1

Gregory Furmanek
Gregory Furmanek

Reputation: 364

You may not assign a read only property. The objects iOS is providing on completed transaction are immutable. To be honest Apple's API fails to properly inform the client apps about the processed transaction.

Upvotes: -2

Related Questions