Ios-in-app-purchase

提供:Dev Guides
移動先:案内検索

iOS-アプリ内購入

アプリ内購入は、アプリケーションに関する追加コンテンツの購入や機能のアップグレードに使用されます。

関与するステップ

  • ステップ1 *-iTunes接続で、*一意のアプリID *があり、*バンドルID *を使用してアプリケーションの更新を作成し、対応するプロビジョニングプロファイルを使用してXcodeでコード署名することを確認します。

ステップ2 *-新しいアプリケーションを作成し、アプリケーション情報を更新します。 これについては、Appleの *Add new apps ドキュメンテーションで詳しく知ることができます。

  • ステップ3 *-アプリケーションのページの[アプリ内購入の管理]でアプリ内購入用の新しい製品を追加します。

ステップ4 *-アプリケーションの銀行口座の詳細を設定してください。 これは、 *In-App purchase が機能するようにセットアップする必要があります。 また、アプリのiTunes接続ページの[ユーザーの管理]オプションを使用してテストユーザーアカウントを作成します。

  • ステップ5 *-次のステップは、アプリ内購入用のコードの処理とUIの作成に関連しています。
  • ステップ6 *-*シングルビューアプリケーション*を作成し、バンドル識別子を入力します。これは、iTunes Connectで指定された識別子です。

ステップ7 *-以下に示すように *ViewController.xib を更新します-

iOSチュートリアル

ステップ8 *-3つのラベルと、それぞれproductTitleLabel、productDescriptionLabel、productPriceLabel、purchaseButtonという名前のボタンの *IBOutlets を作成します。

ステップ9 *-プロジェクトファイルを選択し、ターゲットを選択してから *StoreKit.framework を追加します。

ステップ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

ステップ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

Note

kTutorialPointProductIDをIn-App Purchase用に作成したproductIDに更新する必要があります。 fetchAvailableProductsのproductIdentifiersのNSSetを更新することにより、複数の製品を追加できます。 同様に、追加する製品IDの購入関連アクションを処理します。

出力

アプリケーションを実行すると、次の出力が得られます-

iOSチュートリアル

設定画面でアカウントからログアウトしていることを確認してください。 Initiate Purchaseをクリックして、Use Existing Apple IDを選択します。 有効なテストアカウントのユーザー名とパスワードを入力します。 数秒後に次のアラートが表示されます。

iOSチュートリアル

製品が正常に購入されると、次のアラートが表示されます。 このアラートを表示するアプリケーション機能を更新するための関連コードを確認できます。

iOSチュートリアル