📜  iOS-应用程式内购买

📅  最后修改于: 2020-12-08 06:23:55             🧑  作者: Mango


应用内购买用于购买应用程序的其他内容或升级功能。

涉及的步骤

步骤1-在iTunes connect中,请确保您具有唯一的应用程序ID,并且当我们使用捆绑ID和使用相应的配置文件在Xcode中对代码进行签名来创建应用程序更新时。

步骤2-创建一个新的应用程序并更新应用程序信息。您可以在Apple的“添加新应用”文档中了解有关此内容的更多信息。

步骤3-在应用程序页面的“管理应用内购买”中添加用于应用内购买的新产品。

步骤4-确保为您的应用程序设置银行详细信息。需要进行设置才能使应用内购买正常工作。另外,使用应用程序的iTunes连接页面中的“管理用户”选项创建一个测试用户帐户。

步骤5-下一步与处理代码和为我们的应用内购买创建UI有关。

步骤6-创建一个单一视图应用程序,然后输入捆绑标识符,该标识符是在iTunes connect中指定的标识符。

步骤7-更新ViewController.xib ,如下所示-

iOS教程

步骤8-为这三个标签和分别将其分别命名为productTitleLabel,productDescriptionLabel,productPriceLabel和purchaseButton的按钮创建IBOutlets

步骤9-选择您的项目文件,然后选择目标,然后添加StoreKit.framework

步骤10-更新ViewController.h如下-

#import 
#import 

@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

步骤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教程