optimizing in-app purchases€¦ · store product sheet. storekit features in-app purchases ......

193
© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple. #WWDC14 Optimizing In-App Purchases Using StoreKit Session 303 James Wilson Software Engineering Services

Upload: others

Post on 27-Sep-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

© 2014 Apple Inc. All rights reserved. Redistribution or public display not permitted without written permission from Apple.

#WWDC14

Optimizing In-App PurchasesUsing StoreKit

Session 303James Wilson Software Engineering

Services

Page 2: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

StoreKit

Page 3: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

StoreKit Features

Page 4: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

StoreKit Features

In-App Purchases • Consumables and non-consumable

• Subscriptions

Page 5: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

StoreKit Features

In-App Purchases • Consumables and non-consumable

• Subscriptions

Store Product Sheet

Page 6: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

StoreKit Features

In-App Purchases • Consumables and non-consumable

• Subscriptions

Store Product Sheet

Receipt renewal

Page 7: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

What’s New

Page 8: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

What’s New

StoreKit product sheet supports affiliate program

Page 9: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

What’s New

StoreKit product sheet supports affiliate program

New transaction state—Deferred

• Ask to Buy feature of Family Sharing

Page 10: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateSKPaymentTransactionStateDeferred

Page 11: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateSKPaymentTransactionStateDeferred

The payment is neither purchased nor failed, yet

Page 12: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateSKPaymentTransactionStateDeferred

The payment is neither purchased nor failed, yet • Further update will be received

Page 13: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateSKPaymentTransactionStateDeferred

The payment is neither purchased nor failed, yet • Further update will be received

• Indeterminate time

Page 14: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateSKPaymentTransactionStateDeferred

The payment is neither purchased nor failed, yet • Further update will be received

• Indeterminate time

Must allow the user to continue to use the app

Page 15: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateSKPaymentTransactionStateDeferred

The payment is neither purchased nor failed, yet • Further update will be received

• Indeterminate time

Must allow the user to continue to use the app

• Repurchasing the item is allowed

Page 16: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateSKPaymentTransactionStateDeferred

The payment is neither purchased nor failed, yet • Further update will be received

• Indeterminate time

Must allow the user to continue to use the app

• Repurchasing the item is allowed

• Let StoreKit handle the interaction

Page 17: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateAsk to Buy

Child Parent

Page 18: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateAsk to Buy

Child ParentAttempts In-App

Purchase

Page 19: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateAsk to Buy

Child ParentAttempts In-App

PurchaseNotified of request

Page 20: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateAsk to Buy

Child ParentAttempts In-App

PurchaseNotified of request

SKPaymentTransactionStateDeferred

Page 21: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateAsk to Buy

Child Parent

Page 22: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateAsk to Buy

Child ParentApproves or declines

Page 23: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateAsk to Buy

Child ParentTransaction updated Approves or declines

Page 24: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Deferred Transaction StateAsk to Buy

Child ParentTransaction updated Approves or declines

SKPaymentTransactionStatePurchased SKPaymentTransactionStateFailed

Page 25: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Optimizing In-App Purchases

Page 26: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Page 27: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Load In-AppIdentifiers

Page 28: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

FetchProduct Info

Page 29: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ShowIn-App UI

Page 30: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

MakePurchase

Page 31: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ProcessTransaction

Page 32: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Make Asset Available

Page 33: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

FinishTransaction

Page 34: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

FinishTransaction

Danger Zones

In-App Purchase Process

Page 35: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

User Interaction

Page 36: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Page 37: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ShowIn-App UI

Page 38: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Product Identifier

Page 39: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Product Identifier

Options for storing the list of product identifiers

Page 40: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Product Identifier

Options for storing the list of product identifiers• Baked-in product identifier

Page 41: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Product Identifier

Options for storing the list of product identifiers• Baked-in product identifier

• Fetch from server

Page 42: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Product Identifier

Options for storing the list of product identifiers• Baked-in product identifier

• Fetch from server

- Cache strategy

Page 43: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Product Identifier

Options for storing the list of product identifiers• Baked-in product identifier

• Fetch from server

- Cache strategy

- Reliability

Page 44: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed
Page 45: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed
Page 46: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Not the way to start an In-App Purchase

Page 47: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Product Information

Fetch the product information using StoreKit

!

SKProductsRequest* request = [[SKProductsRequest alloc]

initWithProductIdentifiers:identifierSet];

Page 48: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Product Information

Fetch the product information using StoreKit

!

SKProductsRequest* request = [[SKProductsRequest alloc]

initWithProductIdentifiers:identifierSet];

Anticipate the presentation

Page 49: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Product Information

Fetch the product information using StoreKit

!

SKProductsRequest* request = [[SKProductsRequest alloc]

initWithProductIdentifiers:identifierSet];

Anticipate the presentation• Fetch product info just-ahead-of-time

Page 50: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

SKProduct Object

SKProduct properties • Localized title and description

• Price and locale

• Content size and version (hosted)

Page 51: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

Page 52: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

1.234,56 €

Page 53: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

1.234,56 €

£1,234.56

Page 54: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

1.234,56 €

£1,234.56

€1,234.56

Page 55: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

1.234,56 €

£1,234.56

€1,234.56

1.234,56 kn

Page 56: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

1.234,56 €

£1,234.56

€1,234.56

1.234,56 kn

R$ 1.234,56

Page 57: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

1.234,56 €

£1,234.56

€1,234.56

1.234,56 kn

R$ 1.234,56

฿1,234.56

Page 58: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

1.234,56 €

£1,234.56

€1,234.56

1.234,56 kn

R$ 1.234,56

฿1,234.56

$1,234.56

Page 59: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

Showing localized price

Page 60: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

Showing localized price

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];

Page 61: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

Showing localized price

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];

Page 62: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

Showing localized price

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];[numberFormatter setLocale:product.priceLocale];

Page 63: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

Showing localized price

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];[numberFormatter setLocale:product.priceLocale];NSString *formattedString = [numberFormatter stringFromNumber:product.price];

Page 64: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Price and Currency

Showing localized price

NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];[numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];[numberFormatter setLocale:product.priceLocale];NSString *formattedString = [numberFormatter stringFromNumber:product.price];

Do not perform currency conversion!

Page 65: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Handling Errors

Page 66: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Handling Errors

Not all errors are equal

Page 67: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Handling Errors

Not all errors are equal

Check the error code

Page 68: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Handling Errors

Not all errors are equal

Check the error code• Don’t show an error alert unless necessary

Page 69: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Handling Errors

Not all errors are equal

Check the error code• Don’t show an error alert unless necessary

• User canceling a payment will result in an error

Page 70: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Handling Errors

Not all errors are equal

Check the error code• Don’t show an error alert unless necessary

• User canceling a payment will result in an error

Let StoreKit handle the transaction flow as much as possible

Page 71: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Making the Purchase

Page 72: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ShowIn-App UI

Page 73: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

MakePurchase

Page 74: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

The Payment QueueObserve it, always

Page 75: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

The Payment QueueObserve it, always

The center of your In-App Purchase implementation • The only source of truth for state

Page 76: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

The Payment QueueObserve it, always

The center of your In-App Purchase implementation • The only source of truth for state

Rely on the queue, and only the queue • For transactions in progress

• Payment status updates

• Download status

Page 77: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

The Payment QueueObserve it, always

The center of your In-App Purchase implementation • The only source of truth for state

Rely on the queue, and only the queue • For transactions in progress

• Payment status updates

• Download status

Any and all transactions in the queue are valid and real

Page 78: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

On Launch

Start observing the payment queue

Page 79: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

On Launch

Start observing the payment queue

[[SKPaymentQueue defaultQueue] addTransactionObserver:yourObserver];

Page 80: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleFetch product info

Get information about your products from the store

Page 81: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleFetch product info

Get information about your products from the store

NSSet* identifierSet = [NSSet setWithArray:productIdentifiers];

Page 82: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleFetch product info

Get information about your products from the store

NSSet* identifierSet = [NSSet setWithArray:productIdentifiers];

SKProductsRequest* request = [[SKProductsRequest alloc]

initWithProductIdentifiers: identifierSet];

Page 83: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleFetch product info

Get information about your products from the store

NSSet* identifierSet = [NSSet setWithArray:productIdentifiers];

SKProductsRequest* request = [[SKProductsRequest alloc]

initWithProductIdentifiers: identifierSet];

request.delegate = self;

Page 84: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleFetch product info

Get information about your products from the store

NSSet* identifierSet = [NSSet setWithArray:productIdentifiers];

SKProductsRequest* request = [[SKProductsRequest alloc]

initWithProductIdentifiers: identifierSet];

request.delegate = self;

[request start];

Page 85: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleAdd payment to queue

Start the payment transaction

Page 86: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleAdd payment to queue

Start the payment transaction

SKPayment* payment = [SKPayment paymentWithProduct:product];

Page 87: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleAdd payment to queue

Start the payment transaction

SKPayment* payment = [SKPayment paymentWithProduct:product];[[SKPaymentQueue defaultQueue] addPayment:payment];

Page 88: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleHandle events

Page 89: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleHandle events

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

Page 90: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleHandle events

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{

for (SKPaymentTransaction* transaction in transactions)

Page 91: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleHandle events

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{

for (SKPaymentTransaction* transaction in transactions){

switch(transaction.transactionState) {

Page 92: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleHandle events

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{

for (SKPaymentTransaction* transaction in transactions){

switch(transaction.transactionState) { case SKPaymentTransactionStatePurchased:

Page 93: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleHandle events

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions{

for (SKPaymentTransaction* transaction in transactions){

switch(transaction.transactionState) { case SKPaymentTransactionStatePurchased:

NSURL* receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];

NSData* receipt = [NSData dataWithContentsOfURL:receiptURL];

// Process the transaction }}

Page 94: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Don’t Do This

case SKPaymentTransactionStatePurchased:

// Get the local state for this transaction

Page 95: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Don’t Do This

case SKPaymentTransactionStatePurchased:

// Get the local state for this transaction

SKPayment *payment = myCachedPayments[transaction.payment.productIdentifier];

Page 96: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Don’t Do This

case SKPaymentTransactionStatePurchased:

// Get the local state for this transaction

SKPayment *payment = myCachedPayments[transaction.payment.productIdentifier];

if (!payment)

{

Page 97: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Don’t Do This

case SKPaymentTransactionStatePurchased:

// Get the local state for this transaction

SKPayment *payment = myCachedPayments[transaction.payment.productIdentifier];

if (!payment)

{

// No idea where this transaction came from!

// Ignore it

continue;

}

Page 98: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Don’t Do This

case SKPaymentTransactionStatePurchased:

// Get the local state for this transaction

SKPayment *payment = myCachedPayments[transaction.payment.productIdentifier];

if (!payment)

{

// No idea where this transaction came from!

// Ignore it

continue;

}

Tracking your own state or payment cache is unnecessary

Page 99: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Why Not?

Page 100: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Why Not?

Because, what if… • You crash

• Purchase is disrupted

• Or your app didn’t even start the purchase

Page 101: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Why Not?

Because, what if… • You crash

• Purchase is disrupted

• Or your app didn’t even start the purchase

The transaction is just as valid

• Process it always

Page 102: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleHandling deferred transaction

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

{ for (SKPaymentTransaction* transaction in transactions) {

switch(transaction.transactionState) { !

!

!

!

} } }

Page 103: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleHandling deferred transaction

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

{ for (SKPaymentTransaction* transaction in transactions) {

switch(transaction.transactionState) { !

!

!

!

} } }

case SKPaymentTransactionStateDeferred:

Page 104: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

{ for (SKPaymentTransaction* transaction in transactions) {

switch(transaction.transactionState) { !

!

!

!

} } }

ExampleHandling deferred transaction

case SKPaymentTransactionStateDeferred:

Page 105: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

{ for (SKPaymentTransaction* transaction in transactions) {

switch(transaction.transactionState) { !

!

!

!

} } }

ExampleHandling deferred transaction

case SKPaymentTransactionStateDeferred:

// Allow the user to continue to use the app

Page 106: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

{ for (SKPaymentTransaction* transaction in transactions) {

switch(transaction.transactionState) { !

!

!

!

} } }

ExampleHandling deferred transaction

case SKPaymentTransactionStateDeferred:

// Allow the user to continue to use the app

// It may be some time before the transaction is updated

Page 107: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

- (void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions

{ for (SKPaymentTransaction* transaction in transactions) {

switch(transaction.transactionState) { !

!

!

!

} } }

ExampleHandling deferred transaction

case SKPaymentTransactionStateDeferred:

// Allow the user to continue to use the app

// It may be some time before the transaction is updated

// Do not get stuck in a modal “Purchasing…” state!

Page 108: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleFinish the transaction

Page 109: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleFinish the transaction

Always finish the transaction

Page 110: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleFinish the transaction

Always finish the transaction

- (void)finishTransaction:(SKPaymentTransaction *)transaction

Page 111: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ExampleFinish the transaction

Always finish the transaction

- (void)finishTransaction:(SKPaymentTransaction *)transaction

Tells the store that your app has finished processing the transaction • The transaction will be removed from the queue

Page 112: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

SKPaymentQueue Tips

Page 113: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

SKPaymentQueue Tips

@property(nonatomic, readonly) NSArray *transactions;

• No need for you to keep track of transactions in-flight, trust the queue

Page 114: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

SKPaymentQueue Tips

@property(nonatomic, readonly) NSArray *transactions;

• No need for you to keep track of transactions in-flight, trust the queue

+ (BOOL)canMakePayments

• Known if In-App Purchases have been restricted

Page 115: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

DemoTrouble free In-App Purchase

Page 116: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Post-Sale Experience

Page 117: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

MakePurchase

Page 118: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ProcessTransaction

Page 119: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Make Asset Available

Page 120: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

FinishTransaction

Page 121: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Post-Sale Experience

Page 122: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Post-Sale Experience

Validate the purchase • Receipt verification on-device or server-to-server

Page 123: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Post-Sale Experience

Validate the purchase • Receipt verification on-device or server-to-server

Download content • Hosted In-App Purchase content

• Self-hosted

Page 124: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Post-Sale Experience

Validate the purchase • Receipt verification on-device or server-to-server

Download content • Hosted In-App Purchase content

• Self-hosted

Persisting and restoring transactions

Page 125: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Receipt Validation

Page 126: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Receipt Validation

On-device validation • Unlock features and content within the app

Page 127: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Receipt Validation

On-device validation • Unlock features and content within the app

Server-to-server validation • Restrict access to downloadable content

Page 128: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Receipt Validation

Do not use online validation directly from the device!

iOS 6 APIs for receipt validation are deprecated

Page 129: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Downloading Content

Hosted In-App Purchase content

Page 130: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Downloading Content

Hosted In-App Purchase content• Hosted on Apple’s servers

Page 131: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Downloading Content

Hosted In-App Purchase content• Hosted on Apple’s servers

• Scalable and reliable

Page 132: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Downloading Content

Hosted In-App Purchase content• Hosted on Apple’s servers

• Scalable and reliable

• Downloads in background

Page 133: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Downloading Content

Hosted In-App Purchase content• Hosted on Apple’s servers

• Scalable and reliable

• Downloads in background

• Up to 2GB per in-app purchasable product

Page 134: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Hosted Content

Start download

Page 135: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Hosted Content

Start download

for(SKPaymentTransaction* transaction in transactions) { !

!

!

!

}

- (void)paymentQueue:(SKPaymentQueue *)queue

updatedTransactions:(NSArray *)transactions

Page 136: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Hosted Content

Start download

for(SKPaymentTransaction* transaction in transactions) { !

!

!

!

}

- (void)paymentQueue:(SKPaymentQueue *)queue

updatedTransactions:(NSArray *)transactions

if(transaction.downloads)

Page 137: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Hosted Content

Start download

for(SKPaymentTransaction* transaction in transactions) { !

!

!

!

}

- (void)paymentQueue:(SKPaymentQueue *)queue

updatedTransactions:(NSArray *)transactions

!

[[SKPaymentQueue defaultQueue] startDownloads:

transaction.downloads];

if(transaction.downloads)

Page 138: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Hosted Content

Download progress

Page 139: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Hosted Content

Download progress

- (void)paymentQueue:(SKPaymentQueue *)queue

updatedDownloads:(NSArray *)downloads;

Page 140: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Hosted Content

Download progress

- (void)paymentQueue:(SKPaymentQueue *)queue

updatedDownloads:(NSArray *)downloads;

download.progress

download.timeRemaining

Page 141: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Hosted Content

Download progress

- (void)paymentQueue:(SKPaymentQueue *)queue

updatedDownloads:(NSArray *)downloads;

download.progress

download.timeRemaining

download.state

download.error

Page 142: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Hosted Content

Download progress

- (void)paymentQueue:(SKPaymentQueue *)queue

updatedDownloads:(NSArray *)downloads;

download.progress

download.timeRemaining

download.state

download.error

download.contentURL

When download.state is SKDownloadStateFinished

Page 143: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Self-Hosted Content

Self-hosted downloadable content• Use background download APIs

Page 144: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Self-Hosted Content

Self-hosted downloadable content• Use background download APIs

- Content is downloaded even when your app is not active

Page 145: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Self-Hosted Content

Self-hosted downloadable content• Use background download APIs

- Content is downloaded even when your app is not active

- Using class NSURLConnection APIs has limitations

Page 146: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Using NSURLSession for downloading content

Self-Hosted Content

Page 147: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Using NSURLSession for downloading content

NSURLSessionConfiguration *config = [NSURLSessionConfiguration

backgroundSessionConfiguration:@"MyBackgroundSession"];

Self-Hosted Content

Page 148: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Using NSURLSession for downloading content

NSURLSessionConfiguration *config = [NSURLSessionConfiguration

backgroundSessionConfiguration:@"MyBackgroundSession"];

NSURLSession *session = [NSURLSession sessionWithConfiguration:config

delegate:self delegateQueue:queue];

Self-Hosted Content

Page 149: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Using NSURLSession for downloading content

NSURLSessionConfiguration *config = [NSURLSessionConfiguration

backgroundSessionConfiguration:@"MyBackgroundSession"];

NSURLSession *session = [NSURLSession sessionWithConfiguration:config

delegate:self delegateQueue:queue];

NSURLRequest *request = [NSURLRequest requestWithURL:myURL];

Self-Hosted Content

Page 150: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Using NSURLSession for downloading content

NSURLSessionConfiguration *config = [NSURLSessionConfiguration

backgroundSessionConfiguration:@"MyBackgroundSession"];

NSURLSession *session = [NSURLSession sessionWithConfiguration:config

delegate:self delegateQueue:queue];

NSURLRequest *request = [NSURLRequest requestWithURL:myURL];

NSURLSessionDownloadTask *downloadTask = [session

downloadTaskWithRequest:request];

Self-Hosted Content

Page 151: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

NSURLSessionDownloadDelegate

Download progress

!

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite

{ // do something with progress }

Self-Hosted Content

Page 152: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

NSURLSession

Reconnect to session on launch

!

- (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier completionHandler:(void (^)())completionHandler {

NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfiguration:identifier];

NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:queue];

self.completionHandler = completionHandler; // call when done }

Self-Hosted Content

Page 153: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Downloading Content

When the content is downloaded, finish the transaction

!

[[SKPaymentQueue defaultQueue] finishTransaction:transaction];

!

Otherwise, the payment will stay in the queue

Page 154: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Transactions

Page 155: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

Restoring transactions allows the user to restore• Non-consumable in-app purchases

• Auto-renewing subscriptions

Page 156: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

Restoring transactions allows the user to restore• Non-consumable in-app purchases

• Auto-renewing subscriptions

Consumables and non-renewable restrictions

Page 157: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

Restoring transactions allows the user to restore• Non-consumable in-app purchases

• Auto-renewing subscriptions

Consumables and non-renewable restrictions• You must persist the state!

Page 158: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Page 159: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Observe the queue

Page 160: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Observe the queue - paymentQueue:restoreCompletedTransactionsFailedWithError:

Page 161: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Observe the queue - paymentQueue:restoreCompletedTransactionsFailedWithError: - paymentQueueRestoreCompletedTransactionsFinished

Page 162: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Observe the queue - paymentQueue:restoreCompletedTransactionsFailedWithError: - paymentQueueRestoreCompletedTransactionsFinished

Inspect the receipt and unlock content and features accordingly

Page 163: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Page 164: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Requires a network connection

Page 165: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Requires a network connection

May cause sign in prompt

Page 166: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Requires a network connection

May cause sign in prompt

Your app must offer to restore transactions

Page 167: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Restore Completed Transactions

[[SKPaymentQueue defaultQueue] restoreCompletedTransactions]

Requires a network connection

May cause sign in prompt

Your app must offer to restore transactions• But do not call try to restore unless requested by the user

Page 168: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

SummaryRecipe for trouble-free In-App Purchases

Page 169: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Page 170: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Load In-AppIdentifiers

Page 171: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

In-App Purchase Process

!

!

!

!

Server fetch of product identifiers

• Cache appropriately

• Avoid delay in presenting products

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Load In-AppIdentifiers

Page 172: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Load In-AppIdentifiers

In-App Purchase Process

Page 173: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

FetchProduct Info

In-App Purchase Process

Page 174: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

!

!

!

!

Fetch only the products you need

Fetch just ahead of time

• Avoid delay in presenting products

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

FetchProduct Info

In-App Purchase Process

Page 175: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

FetchProduct Info

In-App Purchase Process

Page 176: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ShowIn-App UI

In-App Purchase Process

Page 177: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

!

!

!

!

Take care to ensure proper localization

Do not convert currencies

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ShowIn-App UI

In-App Purchase Process

Page 178: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ShowIn-App UI

In-App Purchase Process

Page 179: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

MakePurchase

In-App Purchase Process

Page 180: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

!

!

!

!

Add the payment to the queue

Then obey the queue, always

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

MakePurchase

In-App Purchase Process

Page 181: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

MakePurchase

In-App Purchase Process

Page 182: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ProcessTransaction

In-App Purchase Process

Page 183: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

!

!

!

!

Verify the receipt

Unlock features and content

Avoid deprecated APIs and unsafe verification

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ProcessTransaction

In-App Purchase Process

Page 184: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

ProcessTransaction

In-App Purchase Process

Page 185: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Make Asset Available

In-App Purchase Process

Page 186: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

!

!

!

!

Content download

• Host content with Apple

• Use background download APIs

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Make Asset Available

In-App Purchase Process

Page 187: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

Make Asset Available

In-App Purchase Process

Page 188: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

FinishTransaction

In-App Purchase Process

Page 189: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

!

!

!

!

Always finish the transaction!

Keep your queue clean

ProcessTransaction

ShowIn-App UI

Make Asset Available

FetchProduct Info

MakePurchase

Load In-AppIdentifiers

FinishTransaction

FinishTransaction

In-App Purchase Process

Page 190: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

More Information

[email protected]

Documentation In-App Purchases Programming Guide https://developer.apple.com

Apple Developer Forums http://devforums.apple.com

Page 191: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Related Sessions

• Preventing Unauthorized Purchases with Receipts Pacific Heights Friday 10:15AM

• Designing a Great In-App Purchase Experience Nob Hill Wednesday 11:30AM

• Kids and Apps Nob Hill Thursday 3:15PM

Page 192: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed

Labs

• StoreKit and Receipts Lab Services Lab A Wednesday 3:15PM

• StoreKit and Receipts Lab Services Lab A Friday 10:15AM

• Open Hours Services Lab A Friday 2:00PM

Page 193: Optimizing In-App Purchases€¦ · Store Product Sheet. StoreKit Features In-App Purchases ... Must allow the user to continue to use the app • Repurchasing the item is allowed