title: How to deal with multiple In-App Purchases in Swift?
body: <p>I have two In-App Purchases in my iOS app and unable to manage more than one productID correctly. Right now the code only restores the first of my IAPs, and does not unlock the second one after purchased or restored.</p>
  <p>The problem might be with the if/if else statements in the .purchased or in the paymentQueueRestoreCompletedTransactionsFinished() part.</p>
  <p>I'm completely hopeless as <strong>no YouTube tutorial, wiki page or documentation deals with two IAPs, they only focus on one. I would be also happy if someone can just drop me an example code snippet dealing with to IAPs.</strong></p>
  <pre><code>//MARK: Purchase buttons
  
  @IBAction func didTapBuyPRODUCT1(_ sender: Any) {
      SKPaymentQueue.default().add(self)
      if SKPaymentQueue.canMakePayments() {
          let paymentRequest = SKMutablePayment()
          paymentRequest.productIdentifier = &quot;example.productID1&quot;
          SKPaymentQueue.default().add(paymentRequest)
      } else {
      }
  }
  
  @IBAction func didTapBuyPRODUCT2(_ sender: Any) {
      SKPaymentQueue.default().add(self)
      if SKPaymentQueue.canMakePayments() {
          let paymentRequest = SKMutablePayment()
          paymentRequest.productIdentifier = &quot;example.productID2&quot;
          SKPaymentQueue.default().add(paymentRequest)
      } else {
      }
   }
      
      
  //MARK: paymentQueue()
  
  func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
      for transaction in transactions {
          if transaction.transactionState == .purchased {
              
              let t: SKPaymentTransaction = transaction
              let prodID = t.payment.productIdentifier as String
              
              if prodID == &quot;example.productID1&quot; {
                  UserDefaults.standard.setValue(true, forKey: &quot;product1purchased&quot;) }
              if prodID == &quot;example.productID2&quot; {
                  UserDefaults.standard.setValue(true, forKey: &quot;product2purchased&quot;)
              }
              
              purchaseUIFeedback()
  
              SKPaymentQueue.default().finishTransaction(transaction)
              
          } else if transaction.transactionState == .restored {
              
          } else if transaction.transactionState == .failed {
              print(&quot;Transaction failed&quot;)
          } else if transaction.transactionState == .purchasing {
              print(&quot;Purchasing&quot;)
          } else if transaction.transactionState == .deferred {
              print(&quot;Deferred!&quot;)
          }
      }
  }
  
  //MARK: Restore Button
  
      @IBAction func restoreButton(_ sender: Any) {
      SKPaymentQueue.default().add(self)
      SKPaymentQueue.default().restoreCompletedTransactions()
      
  }
  
  //MARK:restoreFunc()
      func paymentQueueRestoreCompletedTransactionsFinished(_ queue: SKPaymentQueue) {
          for transaction in queue.transactions {
              let t: SKPaymentTransaction = transaction
              let prodID = t.payment.productIdentifier as String
  
          if prodID == &quot;example.productID1&quot; {
              UserDefaults.standard.setValue(true, forKey: &quot;product1purchased&quot;)}
          if prodID == &quot;example.productID2&quot; {
              UserDefaults.standard.setValue(true, forKey: &quot;product2purchased&quot;) }
      }
      purchaseUIFeedback()
  }
  
  
  //MARK: UI FEEDBACK
  
  @IBOutlet weak var product1BuyButton: UIImageView!
  @IBOutlet weak var product1Image: UIButton!
  @IBOutlet weak var product2BuyButton: UIImageView!
  @IBOutlet weak var product2Image: UIButton!
  
  
  func purchaseUIFeedback() {
      if (UserDefaults.standard.value(forKey: &quot;product1purchased&quot;) != nil) == true {
          product1Image.image = UIImage(named: &quot;Product 1 In-App Purchase Logo PURCHASED&quot;)
          product1BuyButton.isEnabled = false
          product1BuyButton.setTitle(&quot;Purchased&quot;, for: .normal)
      } else {
          product1Image.image = UIImage(named: &quot;Product 1  In-App Purchase NOT PURCHASED&quot;)
          product1BuyButton.isEnabled = true }
      
      if (UserDefaults.standard.value(forKey: &quot;product2purchased&quot;) != nil) == true {
          product2Image.image = UIImage(named: &quot;Product 2  In-App Purchase PURCHASED&quot;)
          product2BuyButton.isEnabled = false
          product2BuyButton.setTitle(&quot;Purchased&quot;, for: .normal)
      } else {
          product2Image.image = UIImage(named: &quot;Product 2  In-App Purchase NOT PURCHASED&quot;)
          product2BuyButton.isEnabled = true }
  }
  </code></pre>