iOS - 应用内购买

  • 简述

    应用内购买用于购买与应用相关的附加内容或升级功能。
  • 涉及步骤

    Step 1 − 在 iTunes 连接中,确保您有一个 unique App ID 当我们创建应用程序更新时 bundle ID 并使用相应的配置文件在 Xcode 中进行代码签名。
    Step 2− 创建新应用程序并更新应用程序信息。你可以在苹果的Add new apps 文档。
    Step 3 - 添加新产品用于应用内购买 Manage In-App Purchase 的应用程序页面。
    Step 4− 确保为您的应用程序设置银行详细信息。这需要设置为In-App purchase上班。此外,使用创建一个测试用户帐户Manage Users 应用程序的 iTunes 连接页面中的选项。
    Step 5 − 下一步与处理代码和为我们的应用内购买创建 UI 相关。
    Step 6 − 创建一个 single view application 并输入捆绑标识符是在 iTunes 连接中指定的标识符。
    Step 7 − 更新 ViewController.xib 如下图所示 -
    iOS 教程
    Step 8 − 创建 IBOutlets 三个标签和按钮分别将它们命名为 productTitleLabel、productDescriptionLabel、productPriceLabel 和 purchaseButton。
    Step 9 - 选择您的项目文件,然后选择目标,然后添加 StoreKit.framework.
    Step 10 − 更新 ViewController.h 如下 -
    
    #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 − 更新 ViewController.m 如下 -
    
    #import "ViewController.h"
    #define kTutorialPointProductID 
    @"com.tutorialPoints.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:kTutorialPointProductID,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:kTutorialPointProductID]) {
                   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: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 = [[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
    
  • 笔记

    您必须将 kTutorialPointProductID 更新为您为应用内购买创建的 productID。您可以通过更新 fetchAvailableProducts 中 productIdentifiers 的 NSSet 添加多个产品。类似地,处理您添加的产品 ID 的购买相关操作。
  • 输出

    当我们运行应用程序时,我们将获得以下输出 -
    iOS 教程
    确保您已在设置屏幕中注销您的帐户。单击“开始购买”后,选择“使用现有 Apple ID”。输入您的有效测试帐户用户名和密码。几秒钟后,您将看到以下警报。
    iOS 教程
    成功购买产品后,您将收到以下提醒。您可以在我们显示此警报的地方看到用于更新应用程序功能的相关代码。
    iOS 教程