问题
can we assign skpayment applicationusername?
When in app purchase is completed, I got a null applicationUsername in the
SKPayment.
How do I assign a value to SKPayment
applicationUsername
?
回答1:
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.
回答2:
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)
}
回答3:
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.
来源:https://stackoverflow.com/questions/37202248/how-to-assign-skpayment-applicationusername