Get localized product information from the App Store

suggest change

Get localized product information from a set of product identifier strings using SKProductsRequest:

import StoreKit

let productIdentifierSet = Set(["yellowSubmarine", "pennyLane"])
let productsRequest = SKProductsRequest(productIdentifiers: productIdentifierSet)

In order to process the products from the productsRequest, assign a delegate that handles the response. The delegate conforms to SKProductsRequestDelegate, so it must be an NSObject subclass and implement productsRequest. The method below is Swift 2. Swift 3 names it productsRequest(_:didReceive:).

class PaymentManager: NSObject, SKProductsRequestDelegate {

    var products: [SKProduct] = []

    func productsRequest(request: SKProductsRequest,
                         didReceiveResponse response: SKProductsResponse) {

        products = response.products

    }

}

To initiate the productsRequest we assign PaymentManager as the products-request’s delegate, and call the start() method on the request:

let paymentManager = PaymentManager()
productsRequest.delegate = paymentManager
productsRequest.start()

If the request succeeds, the products will be in paymentManager.products. delegate is weak, so this local paymentManager can be released before the response arrives unless something else retains it. Invalid product identifiers are reported on response.invalidProductIdentifiers, which this sample ignores.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents