YouTip LogoYouTip

Ios In App Purchase

* * * ## Introduction In-App Purchase is used by applications to purchase additional content or upgrade functionality. ### Example Steps 1. In iTunes Connect make sure you have a unique App ID. When creating bundle ID for application updating, the code gets signed with the corresponding configuration file in Xcode 2. Create a new application and update the application information. You can learn more about it in Apple's Adding New Application documentation. 3. In the application's page under Manage In-App Purchase, add new products for in-app purchases 4. Ensure the application is set up with banking details. It needs to be set up for In-App Purchase. Also in iTunes Connect use the Manage Users option to create a test user account linked to your application's page. 5. Next is to handle the code and create the UI for in-app purchases. 6. Create a single view application and enter the bundle identifier specified in iTunes Connect 7. Update ViewController.xib as shown below ![Image 1: InAppPurchase_OutputInterface](#) 8. Create IBOutlets for three labels and name the buttons productTitleLabel, productDescriptionLabel, productPriceLabel and purchaseButton 9. Select the project file, then select the target, and add StoreKit.framework 10. Update ViewController.h as follows ```objc #import #import @interface ViewController : UIViewController{ 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 11. Update ViewController.m as follows ```objc #import "ViewController.h" #define kTutorialPointProductID @"com.tutorialPoints.testApp.testProduct" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { ; // Adding activity indicator activityIndicatorView = [ initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; activityIndicatorView.center = self.view.center; ; [self.view addSubview:activityIndicatorView]; ; //Hide purchase button initially purchaseButton.hidden = YES; ; } - (void)didReceiveMemoryWarning { ; // Dispose of any resources that can be recreated. } -(void)fetchAvailableProducts{ NSSet *productIdentifiers = [NSSet setWithObjects:kTutorialPointProductID,nil]; productsRequest = [ initWithProductIdentifiers:productIdentifiers]; productsRequest.delegate = self; ; } - (BOOL)canMakePurchases { return ; } - (void)purchaseMyProduct:(SKProduct*)product{ if () { SKPayment *payment = [SKPayment paymentWithProduct:product]; [ addTransactionObserver:self]; [ addPayment:payment]; } else{ UIAlertView *alertView = [initWithTitle: @"Purchases are disabled in your device" message:nil delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; ; } } -(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:kTutorialPointProductID]) { NSLog(@"Purchased "); UIAlertView *alertView = [initWithTitle: @"Purchase is completed succesfully" message:nil delegate: self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; ; } [ finishTransaction:transaction]; break; case SKPaymentTransactionStateRestored: NSLog(@"Restored "); [ 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:kTutorialPointProductID]) { [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 = [ initWithTitle:@"Not Available" message:@"No products to purchase" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Ok", nil]; ; } ; purchaseButton.hidden = NO; } @end **Note:** You need to modify the kTutorialPointProductID for the In-App Purchase you created. By modifying the NSSet of product identifiers in fetchAvailableProducts, you can add multiple products. ### Output When you run the application, the output will be as follows ![Image 2: InAppPurchase_Output1](#) Make sure you are already logged in. Click Purchase and select an existing Apple ID. Enter a valid test account username and password. After a few seconds, the following message will be displayed ![Image 3: InAppPurchase_Output2](#) Once the product is successfully purchased, you will receive the following information. You can update your application functionality related code where this information is displayed ![Image 4: InAppPurchase_Output3](#)
← Ios IadIos Maps β†’