0

I have two purchases with id myPurchase and purchase01. I want to get prices of two my purchases and add this prices in two variable.

var price = ""
var price2 = ""

I use this code to load price of one purchase.

var product_id: String? 

override func viewDidLoad() {
    super.viewDidLoad()

product_id = "myPurchase"

if !(UserDefaults.standard.bool(forKey: "purchased")){
    if (SKPaymentQueue.canMakePayments()) {
        let productID:NSSet = NSSet(object: self.product_id!);
        let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productID as! Set<String>);
        productsRequest.delegate = self;
        productsRequest.start();
        print("Fetching Products");
    }else{
        print("Can't make purchases");
    }
}
}

func productsRequest (_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

        let count : Int = response.products.count
        if (count>0) {
            let validProduct: SKProduct = response.products[0] as SKProduct
            if (validProduct.productIdentifier == self.product_id) {
                buyProduct(product: validProduct);
            } else {
                print(validProduct.productIdentifier)
            }
        } else {
            print("nothing")
        }
    }

    func buyProduct(product: SKProduct){
        price = localizedPriceForProduct(product)
        //price2 = localizedPriceForProduct(product)
    }

    func localizedPriceForProduct(_ product:SKProduct) -> String {
        let priceFormatter = NumberFormatter()
        priceFormatter.formatterBehavior = NumberFormatter.Behavior.behavior10_4
        priceFormatter.numberStyle = NumberFormatter.Style.currency
        priceFormatter.locale = product.priceLocale
        return priceFormatter.string(from: product.price)!
    }

How to I get the price of second purchase if my product_id = first purchase? Without third-party libraries.

  • Send both product ids in the `SKProductsRequest` and then look at all of the results in the delegate function – Paulw11 Jul 28 '18 at 07:31
  • @Paulw11 To send 2 purchases `myPurchase` and `purchase01` in `SKProductsRequest `, do I need to create an array of them? Or how to send them? –  Jul 28 '18 at 07:35
  • You need to add both product ids to the set. In Swift, use `Set` rather than `NSSet` – Paulw11 Jul 28 '18 at 07:38
  • @Paulw11 I create `var product_id: Set = ["myPurchase", "purchase01"] `but I get this `Binary operator '==' cannot be applied to operands of type 'String' and 'Set'` on this line: `if (validProduct.productIdentifier == self.product_id) {` –  Jul 28 '18 at 08:26
  • That’s right. You can’t compare a set and a string. I would suggest that you iterate over the returned products and use a switch statement to look for your valid product ids. Putting your products into a dictionary is probably better than discrete variables – Paulw11 Jul 28 '18 at 08:46
  • @Paulw11 I do not quite understand what you mean. Can you show an example? –  Jul 28 '18 at 09:03

1 Answers1

0

You can send a Set of product IDs with the SKProductsRequest (Don't use NSSet in Swift).

You will then receive an array of valid products back in the response. Rather than storing the prices in discrete variables, I would suggest you create a dictionary keyed by product ID to store the results.

var products = [String:SKProduct]()

override func viewDidLoad() {
    super.viewDidLoad()

    let productIds = Set(["myPurchase","purchase01"])

    if !(UserDefaults.standard.bool(forKey: "purchased")){
        if (SKPaymentQueue.canMakePayments()) {
            let productsRequest:SKProductsRequest = SKProductsRequest(productIdentifiers: productIds);
            productsRequest.delegate = self;
            productsRequest.start();
            print("Fetching Products");
        } else {
            print("Can't make purchases");
        }
    }
}

func productsRequest (_ request: SKProductsRequest, didReceive response: SKProductsResponse) {

    for product in response.products {
        self.products[product.productIdentifier] = product
    }
}

When you are implementing in app purchasing, one of the first things you should do in didFinishLaunchingWithOptions is instantiate an object to listen to the payment queue and process transactions.

The way your code is currently written it appears that you are checking whether purchase is available and retrieving the products when the user has initiated the purchase. This isn't the best approach.

You should check for purchasing and retrieve products when your app starts. Once products have been retrieved, enable the purchase button

Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • Thank you! I have some questions. I use this code `var price = [String: String]() `and `price[product.productIdentifier] = localizedPriceForProduct(product)`. But in price I get `["myPurchase": "6,00 USD"]` and `["myPurchase": "6,00 USD", "purchase01": "1,00 USD"]` But if I use this line `price[4] = localizedPriceForProduct(product) `I get `["4": "6,00 USD"]` and `["4": "1,00 USD"]`. How to fix it? –  Jul 29 '18 at 06:06
  • I understand this is not welcomed by the community. But could you please help with this problem. I just don't know what to do. Thanks! - https://stackoverflow.com/questions/69851479/audio-files-wont-play-with-airpods-pro-on-ios-15 – user Nov 12 '21 at 20:36