Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.
}