skip to Main Content

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


  1. Chosen as BEST ANSWER

    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 {

    var request: SKProductsRequest!
    
    override init() {
        super.init()
        request = SKProductsRequest()
        self.getProducts()
    }
    
    func getProducts() {
        print("OK")
        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")
    }
    

    }

    It works fine now.


  2. 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 a Set of Strings:

    let productID = NSSet(objects: "com.dev8882.MyDungeon.IAP.PowerSword")
    let request = SKProductsRequest(productIdentifiers: productID as! Set<String>)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search