Pierro787 Pierro787 - 22 days ago 18
iOS Question

Swift 3: In app purchase functionnalities with several items

I try to implement in-app purchase functionnalities with several permanent items in Swift 3 for my spritekit game. Evrything is created on iTunes Connect, the bundle name is set and my 4 items are correctly declared in the iTunes Connect. The problem is about code inside xcode. I use the tutorial from Raywenderlich.com here:

https://www.raywenderlich.com/122144/in-app-purchase-tutorial

This tutorial is made for just one item, but what about if we need more items ? (4 in my case). I try for 2 days to modify the code to manage more than one single item, without any success. I have modified the structure RageProducts with the following code:

import Foundation

public struct RageProducts {

public static let GirlfriendOfDrummerRage = "com.creaworks.FurryLight_Puzzles.Guitarist"



fileprivate static let productIdentifiers: Set<ProductIdentifier> = [RageProducts.GirlfriendOfDrummerRage]

// Initial declaration for one item
// public static let store = IAPHelper(productIds: RageProducts.productIdentifiers)
}

//New declaration for 4 items
public static let store = IAPHelper(productIds: Set(
[
GreenGrocerPurchase.AdRemoval,
GreenGrocerPurchase.NewShoppingLists_One,
GreenGrocerPurchase.NewShoppingLists_Five,
GreenGrocerPurchase.NewShoppingLists_Ten
].map { $0.productId }
))

func resourceNameForProductIdentifier(_ productIdentifier: String) -> String? {
return productIdentifier.components(separatedBy: ".").last
}


When I test the code, the compiler return no error but I see no item in my purchase list (instead of one when I activate the old one item declaration). What am I missing ?

Is there a swift 3 simplest code example to declare several items for in app purchase ? I can't find any on the web. I use spritekit, so, I don't need tableview components or UIView buttons, this is a little bit confusing for just starting with in app purchase functionnalities.

Thanks !

Answer

Make sure you add additional identifiers, similar to this one:

public static let GirlfriendOfDrummerRage = "com.creaworks.FurryLight_Puzzles.Guitarist"

Then add them to Set<ProductIdentifier>.

Here an example with 2 products:

public struct Products {

    public static let One = "com.creaworks.FurryLight_Puzzles.Guitarist"
    public static let Two = "com.creaworks.FurryLight_Puzzles.GuitaristTWO"

    fileprivate static let productIndentifiers: Set<ProductIdentifier> = [Products.One, Products.Two]

    func resourceNameForProductIdentifier(_ productIdentifier: String) -> String? {
        return productIdentifier.components(separatedBy: ".").last
    }
}