iOS In-App Purchase

Ios In-App purchase is used in order to purchase additional content or to upgrade features with respect to an application.

Steps Involved

  • Step 1. In iTunes connect, ensure that we have a unique App ID and when we create the application update with the bundle ID and code signing in Xcode with corresponding provisioning profile.
  • Step 2.Create a new application and update application information. we can know more about this in apple’s Add new apps documentation.
  • Step 3. Add a new product for in-app purchase in Manage In-App Purchase of our application’s page.
  • Step 4. Ensure we setup the bank details for our application. This needs to be setup for In-App purchase to work. Also, create a test user account using Manage Users option in iTunes connect page of our app.
  • Step 5. The next steps are related to handling code and creating UI for our In-App purchase.
  • Step 6. Create a single view application and enter the bundle identifier is the identifier specified in iTunes connect.
  • Step 7. Update the ViewController.xib as displayed below −

iOS In-App Purchase

  • Step 8. Create IBOutlets for the three labels and the button naming them as productTitleLabel, productDescriptionLabel, productPriceLabel and purchaseButton respectively.
  • Step 9. Select our project file, then select targets and then add StoreKit.framework.
  • Step 10. Update ViewController.h as follows −

#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
@interface ViewController : UIViewController<
SKProductsRequestDelegate,SKPaymentTransactionObserver>
{
SKProductsRequest *productsRequest;
NSArray *validProducts;
UIActivityIndicatorView *activityIndicatorView;
IBOutlet UILabel *productTitleLabel;
IBOutlet UILabel *productDescriptionLabel;
IBOutlet UILabel *productPriceLabel;
IBOutlet UIButton *purchaseButton;
}
– (void)fetchAvailableProducts;
– (BOOL)canMakePurchases;
– (void)purchaseMyProduct:(SKProduct*)product;
– (IBAction)purchase:(id)sender;
@end

Step 11. Update ViewController.m as follows −
#import “ViewController.h”
#define kwisdomjobsProductID
@”com.wisdomjobs.testApp.testProduct”
@interface ViewController ()
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
// Adding activity indicator
activityIndicatorView = [[UIActivityIndicatorView alloc]
initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
activityIndicatorView.center = self.view.center;
[activityIndicatorView hidesWhenStopped];
[self.view addSubview:activityIndicatorView];
[activityIndicatorView startAnimating];
//Hide purchase button initially
purchaseButton.hidden = YES;
[self fetchAvailableProducts];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void)fetchAvailableProducts{
NSSet *productIdentifiers = [NSSet
setWithObjects:kwisdomjobsProductID,nil];
productsRequest = [[SKProductsRequest alloc]
initWithProductIdentifiers:productIdentifiers];
productsRequest.delegate = self;
[productsRequest start];
}
– (BOOL)canMakePurchases
{
return [SKPaymentQueue canMakePayments];
}
– (void)purchaseMyProduct:(SKProduct*)product{
if ([self canMakePurchases]) {
SKPayment *payment = [SKPayment paymentWithProduct:product];
[[SKPaymentQueue defaultQueue] addTransactionObserver:self];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
else{
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:
@”Purchases are disabled in your device” message:nil delegate:
self cancelButtonTitle:@”Ok” otherButtonTitles: nil];
[alertView show];
}
}
-(IBAction)purchase:(id)sender{
[self purchaseMyProduct:[validProducts objectAtIndex:0]];
purchaseButton.enabled = NO;
}
#pragma mark StoreKit Delegate
-(void)paymentQueue:(SKPaymentQueue *)queue
updatedTransactions:(NSArray *)transactions {
for (SKPaymentTransaction *transaction in transactions) {
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchasing:
NSLog(@”Purchasing”);
break;
case SKPaymentTransactionStatePurchased:
if ([transaction.payment.productIdentifier
isEqualToString:kwisdomjobsProductID]) {
NSLog(@”Purchased “);
UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:
@”Purchase is completed succesfully” message:nil delegate:
self cancelButtonTitle:@”Ok” otherButtonTitles: nil];
[alertView show];
}
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
NSLog(@”Restored “);
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(@”Purchase failed “);
break;
default:
break;
}
}
}
-(void)productsRequest:(SKProductsRequest *)request
didReceiveResponse:(SKProductsResponse *)response
{
SKProduct *validProduct = nil;
int count = [response.products count];
if (count>0) {
validProducts = response.products;
validProduct = [response.products objectAtIndex:0];
if ([validProduct.productIdentifier
isEqualToString:kwisdomjobsProductID]) {
[productTitleLabel setText:[NSString stringWithFormat:
@”Product Title: %@”,validProduct.localizedTitle]];
[productDescriptionLabel setText:[NSString stringWithFormat:
@”Product Desc: %@”,validProduct.localizedDescription]];
[productPriceLabel setText:[NSString stringWithFormat:
@”Product Price: %@”,validProduct.price]];
}
} else {
UIAlertView *tmp = [[UIAlertView alloc]
initWithTitle:@”Not Available”
message:@”No products to purchase”
delegate:self
cancelButtonTitle:nil
otherButtonTitles:@”Ok”, nil];
[tmp show];
}
[activityIndicatorView stopAnimating];
purchaseButton.hidden = NO;
}
@end

Note

We have to update kwisdomjobsProductID to the productID we have created for our In-App Purchase. We can add more than one product by updating the productIdentifiers’s NSSet in fetchAvailableProducts. Similary, handle the purchase related actions for product IDs we add.

Output

On executing the application, we’ll get the below result −

iOS In-App Purchase

Make sure that we had logged out of our account in the settings screen. On clicking the Initiate Purchase, select Use Existing Apple ID. Enter our valid test account username and password. We will be displayed the below alert.

iOS In-App Purchase

Once our product is purchased successfully, we get the below alert. We can see relevant code for updating the application features where we will be showing this below alert.

iOS In-App Purchase