In-App Purchase Tutorial: Getting Started

I can confirm that the Prefix is no longer required to add before the product ID. Also I suggest to download the tutorial project because it have a better IAHelper.swift class


Thank for the great tutorial. But:

private var purchasedProductIdentifiers = Set()

This causes an error: Generic parameter ‘Element’ could not be inferred

what’s the matter?

And another serious problem, Apple had modified the format of product identifier, now we don’t need the Prefix in this tutorial anymore, just use the product name. This issue confused me a long time


This tutorial has been updated for Swift 3, iOS 10, and Xcode 8. Enjoy!

Where can I see the old version of this tutorial(with swift 2.3 and Xcode 7.3)?

Unfortunately, we don’t publish old versions of tutorials - just the latest & greatest.

Hello
Great tutorial, but how can we use it for auto renewable subscription.
Thank you

Thanks for this great tutorial.
I had one doubt. In the function :

public func requestProducts(completionHandler: @escaping ProductsRequestCompletionHandler) {

productsRequest?.cancel()
productsRequestCompletionHandler = completionHandler

productsRequest = SKProductsRequest(productIdentifiers: productIdentifiers)
productsRequest!.delegate = self
productsRequest!.start()
}

Why are we cancelling the productsRequest in the beginning?

Hi,

Thank you for your good tutorial. I am a beginner for IAP and first time doing this. Then, my app is in development phase. Currently i am stuck at retrieving the product info. I just add the product at Xcode->Open Developer Tool->Application Loader->New In-App purchase. And then it show prepare for submission in itunes connect. So, i would like to ask is it i need to submit my app even though my app still in development phase in order to retrieve the product successfully?

Thanks.

From,
Wen Rong

sorry discovered the problem

Hi, thanks for the tutorial! I have a question:

When a restored purchase comes in, if the guard condition is false, i.e. there is nothing in .original, we return early from the function. Will there be another opportunity to supply that restore purchase to finishTransaction or call deliverPurchaseNotificationFor(identifier:)? What circumstances allow receiving a restored purchase with nothing in .original? Maybe it gets replayed with the original purchase in that property?

Sorry, that was 3 questions :slight_smile:

How can I add more products.
I tried to add them in RageProducts,swift but its not working, Any help plz
Thank you

How can I show Progress Indicator Untill Inapp Purchase or Apple response???

I get a compile error in function

public func requestProducts(completionHandler: @escaping ProductsRequestCompletionHandler)

it says:

@escaping attribute only applies to function types

I was getting several compile errors involving productsRequest in the requestProducts function (and one in the clearRequestAndHandler function). I realized that they were happening because the name of the fileprivate var productsRequest: SKProductsRequest? was identical to the SKProductsRequestDelegate productsRequest function. I renamed the var (and all the references to it), and all the compile errors went away.

Hi!

This is a great tutorial, easy implementation and great explanations for first time IAP implementors!

I have 1 question after going through the tutorial and successfully completing everything you’ve laid out - How to show purchases already made offline? I see that we fetch info about purchases already made, but is there a way of storing that and presenting users with their already purchased items regardless of whether they have an internet connection or not?

Thanks again!

@nickvr : you could store a value in the Keychain, as using UserDefaults for that makes it rather easy to circumvent. Apple has recently published an updated wrapper which is very easy to implement. It even allows you to share Keychain access between apps.

To get this to work properly in iOS10 Xcode 8.2 beta I required adding an observer to the SKPaymentQueue to receive proper delegate callbacks. Replace restorePurchases function in IAPHelper.swift with the following:

public func restorePurchases() {
if (SKPaymentQueue.canMakePayments()) {
SKPaymentQueue.default().add(self)
SKPaymentQueue.default().restoreCompletedTransactions()
}
}

All, great tutorial. As always, very thorough.
One question: Your tutorial refers to a single product IAP. I am trying to change the code to allow several IAP products. This is really stumped me. Can you please offer a suggestion?
Thank you

For those that want to add multiple products. here is the code:

public struct RageProducts {

public static let Product1 = "FullProductID1"
public static let Product2 = "FullProductID2"

fileprivate static let productIdentifiers: Set<ProductIdentifier> = [RageProducts.Product1, RageProducts.Product2]

public static let store = IAPHelper(productIds: RageProducts.productIdentifiers)