I’m trying to understand implementing IAP using Xcode 12 and SwiftUI. I prepared everything using App Store Connect and also registered the product id.
I’m using a StoreManager class adapting the SKProductsRequestDelegate protocol to fetch the corresponding SKProduct of the specified product id.
However, the func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) never gets called even when I’m working with a physical device. Actually, the console prints nothing at all.
I don’t understand why this function never gets called.
class StoreManager: NSObject, SKProductsRequestDelegate {
override init() {
super.init()
getProducts()
}
func getProducts() {
let productID = "com.dev8882.MyDungeon.IAP.PowerSword"
let request = SKProductsRequest(productIdentifiers: Set([productID]))
request.delegate = self
request.start()
}
func productsRequest(_ request: SKProductsRequest, didReceive response: SKProductsResponse) {
print("Did receive response")
}
}
2
Answers
I am not sure why this fixed my problem:
According the Apple docs a strong reference SKProductsRequest should by used to start the request. So I updated my code like this:
class StoreManager: NSObject, SKProductsRequestDelegate {
}
It works fine now.
My code is a bit different. It’s been about 3 years, so I’m not able to be as detailed in my explanation of why this works, but basically I’m creating my product ID(s) as an
NSSet
, then force-casting it as aSet
ofStrings
: