Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

John Levy
John Levy
1,451 Points

Having trouble adding a second in app purchase with swift

Hello I have added one in app purchase to my code which worked well (remove ads). I am trying to add a second (unlockPhrases). My problem is I dont know where to start in adding the second in app purchase or what changes I need to make to my code. I have attached my code below. What changers should I make to make the secod in app purchase work like the first one? Thanks in advance

class FourthViewController: UIViewController, SKProductsRequestDelegate, SKPaymentTransactionObserver {

@IBOutlet var unlockPhrasesLabel: UILabel!

@IBOutlet var unlockPhrasesResult: UILabel!

@IBOutlet var removeAdsLabel: UILabel!

@IBOutlet var removeAdsResult: UILabel!

var activeProduct: SKProduct?


    @IBAction func removeAds(_ sender: Any) {

    if let activeProduct = activeProduct {

    print("Buying \(activeProduct.productIdentifier)")

        let payment = SKPayment(product: activeProduct)

        SKPaymentQueue.default().add(payment)

    } else {

          print("No product")
    }
}

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

    print("Loaded products")

    for product in response.products {

     print("Product: \(product.productIdentifier), \(product.localizedTitle), \(product.price.floatValue)")

        removeAdsLabel.text = "Buy \(product.localizedTitle)"

        activeProduct = product


    }

}

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {

for transaction in transactions {

    switch (transaction.transactionState) {

    case .purchased:
        SKPaymentQueue.default().finishTransaction(transaction)
        print("Purchased")
        removeAdsResult.text = "Purchased"
    case .failed:
        SKPaymentQueue.default().finishTransaction(transaction)
        print("Failed")
        removeAdsResult.text = "Failed"
    default:
        break
    }
}

}

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.


    SKPaymentQueue.default().add(self)

    let productIdentifiers: Set<String> = ["removeAds", "mensPhrases"]

    let productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)

   productsRequest.delegate = self

    productsRequest.start()


}


override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}