# SDK /SDK

> Tapjoy の iOS SDK リファレンスの中を見て回り、プレースメントのリクエスト、コールバックの処理、広告コンテンツの表示、コア SDK 機能の統合を行う方法を確認できます。

## プレースメントのリクエスト##requesting-a-placement

まず、ファイルの先頭に`TJPlacement.h` をインポートします。

1. **Objective-C**

   ```objective-c title="Objective-C"
   #import <Tapjoy/TJPlacement.h>
   ```

2. **Swift**

   ```swift
   // Import already happens automatically through your bridging header
   ```

次に、TJPlacement クラスのインスタンスを作成し、プレースメント名で初期化します。コード内のプレースメント名の文字列が、ダッシュボード内のプレースメント名と正確に一致していることを確認します。コールバックを受け取るデリゲートを実装することもできます (後述)。

1. **Objective-C**

   ```objective-c title="Objective-C"
   TJPlacement *p = [TJPlacement placementWithName:@"Main Menu" delegate:self];
   ```

2. **Swift**

   ```swift
   let p = TJPlacement(name: "Main Menu", delegate: self)
   ```

最後に、コンテンツをリクエストします。

1. **Objective-C**

   ```objective-c title="Objective-C"
   [p requestContent];
   ```

2. **Swift**

   ```swift
   p?.requestContent()
   ```

コンテンツをリクエストする前に、Tapjoy の接続呼び出しが成功したことを確認してください。TJC\_CONNECT\_SUCCESS 通知を確認する前にリクエストしないでください。

## コンテンツの事前キャッシュ##pre-caching-the-content

最高のユーザー体験を提供するために、ユーザーにそのコンテンツが表示されるコンテンツを事前にリクエストします。例えば、プレースメントがメインメニューのボタンの場合、アプリケーションの起動時に、Tapjoy Connect の呼び出しが成功した直後にコンテンツをリクエストすることをお勧めします。コンテンツをリクエストするまで待つと、多くの場合、ユーザーはコンテンツがロードされるまで、ローディングスピナーを見ながら待たされることになります。広告の場合、ユーザーが広告に関与する可能性が低くなり、収益を得る可能性が低くなります。

## TJPlacement コールバック##tjplacement-callbacks

コンテンツリクエストの状態に関するフィードバックを取得するには、以下の TJPlacementDelegate メソッドを実装します。

1. **Objective-C**

   ```objective-c title="Objective-C"
   - (void)requestDidSucceed:(TJPlacement*)placement{}  // Called when the content request returns from the Offerwall servers. Does not necessarily mean that content is available.
   - (void)requestDidFail:(TJPlacement*)placement error:(NSError*)error{}
   - (void)contentIsReady:(TJPlacement*)placement{} //This is called when the content is actually available to display. 
   - (void)contentDidAppear:(TJPlacement*)placement{} 
   - (void)contentDidDisappear:(TJPlacement*)placement{}
   ```

2. **Swift**

   ```swift
   func requestDidSucceed(_ placement: TJPlacement!) {} // コンテンツリクエストが Offerwall のサーバーから返されるときに呼び出されます。Does not necessarily mean that content is available.
   func requestDidFail(_ placement: TJPlacement!, error:Error!) {}
   func contentIsReady(_ placement: TJPlacement!) {} //This is called when the content is actually available to display.
   func contentDidAppear(_ placement: TJPlacement!) {}
   func contentDidDisappear(_ placement: TJPlacement!) {}
   ```

## プレースメントの表示##displaying-a-placement

コンテンツを実際に表示するには、以下のように show を呼び出します。

1. **Objective-C**

   ```objective-c title="Objective-C"
   [p showContentWithViewController: nil];
   ```

2. **Swift**

   ```swift
   p.showContent(with: nil)
   ```

`showContentWithViewController` に "nil" を渡すと、Offerwall SDK は広告の表示を処理する独自のビューコントローラーを作成します。ほとんどの場合、これが最も安全なオプションです。複雑なビュー階層があり、何らかの理由で表示を自分で管理する必要がある場合は、任意で独自のビューコントローラーを `showContentWithViewController` に渡すことができます。このメソッドに渡される ViewController が最上位のビューであり、他のビューによって邪魔されず、Tapjoy のコンテンツが削除されるまで他のビューがこのビューの上に配置されないことがきわめて重要です。

show を呼び出す前にコンテンツの準備ができていることを確認します。

1. **Objective-C**

   ```objective-c title="Objective-C"
   if (p.isContentReady) {
      [p showContentWithViewController: nil];
   }
   else {
     //handle situation where there is no content to show, or it has not yet downloaded.
   }    
   ```

2. **Swift**

   ```swift
   if (p.isContentReady) {
       p.showContent(with: nil)
   } else {
     //handle situation where there is no content to show, or it has not yet downloaded.
   }
   ```

お勧めのベストプラクティスは、ビデオを含む可能性のあるプレースメントからコンテンツを表示する前にアプリのオーディオをミュートすることです。ミュートしないと、ビデオとアプリのオーディオが競合する可能性があります。

多くの場合、contentIsReady デリゲートが発生するまで待ってから、ユーザーにコンテンツを表示することをお勧めします。こうすることにより、コンテンツが実際にデバイス上にあること、遅延なくユーザーに表示できることを確認できます。もう 1 つの同等の方法は、上の例のように、`p.isContentReady` が true かどうかを確認することです。

## コンテンツの再リクエスト##re-requesting-the-content

プレースメントからユーザーにコンテンツを正常に表示したら、コンテンツを再度リクエストして (例えば `[p requestContent];` を再度呼び出し)、別のコンテンツ単位でそのプレースメントを "リロード" する必要があります。`[p showContentWithViewController: self];` を再度呼び出すことはできません。コンテンツをリクエストする前にコンテンツを表示しようとすると、コンテンツの表示に失敗します。

## Tapjoy コンテンツアクションリクエストの処理##handling-tapjoy-content-action-requests

Reward (ゲーム内報酬) や IAP Promotion (アプリ内課金プロモーション) などの Tapjoy コンテンツタイプでは、そのコンテンツ単位で渡されたパラメーターに基づいてコードがアクションを実行する必要があります。例えば、Reward コンテンツ単位はアイテム名 (string) と数量 (integer) がユーザーに渡されることを指定します。ゲーム内報酬を反映するためにユーザーの広告在庫を実際に調整するのはアプリケーションです。以下の TJActionRequestDelegate メソッドが呼び出され、コンテンツ単位から適切な情報が渡されます。

1. **Objective-C**

   ```objective-c title="Objective-C"
   - (void)placement:(TJPlacement*)placement didRequestPurchase:(TJActionRequest*)request productId:(NSString*)productId {
           //called when user clicks the product link in IAP promotion content
           //implement code here to trigger IAP purchase flow for item  here
   }
   - (void)placement:(TJPlacement*)placement didRequestReward:(TJActionRequest*)request itemId:(NSString*)itemId quantity:(int)quantity {
           //called when the reward content is closed by the user
           //implement code here to give the player copies of item 
   }    
   ```

2. **Swift**

   ```swift
   func placement(_ placement:TJPlacement!, didRequestPurchase request:TJActionRequest!, productId:String!) {
       //called when user clicks the product link in IAP promotion content
       //implement code here to trigger IAP purchase flow for item  here
   }

   func placement(_ placement:TJPlacement!, didRequestReward request:TJActionRequest!, itemId:String!, quantity:Int32) {
       //called when the reward content is closed by the user
       //implement code here to give the player copies of item
   }
   ```

## エントリーポイントの設定##setting-the-entry-point

任意で、各プレースメントの "エントリーポイント" を Tapjoy に伝えることができます。以下のさまざまなプリセット値から選択できます。

1. **Objective-C**

   ```objective-c title="Objective-C"
   TJEntryPointUnknown //Not set, but removes any value that was already set
   TJEntryPointOther
   TJEntryPointMainMenu
   TJEntryPointHud
   TJEntryPointExit
   TJEntryPointFail
   TJEntryPointComplete
   TJEntryPointInbox 
   TJEntryPointInitialisation
   TJEntryPointStore 
   ```

2. **Swift**

   ```swift
   TJEntryPoint.unknown
   TJEntryPoint.other
   TJEntryPoint.mainMenu
   TJEntryPoint.hud
   TJEntryPoint.exit
   TJEntryPoint.fail
   TJEntryPoint.complete
   TJEntryPoint.inbox
   TJEntryPoint.initialisation
   TJEntryPoint.store
   ```

プレースメントオブジェクトの作成後、かつコンテンツをリクエストする前にエントリーポイントを設定します。

1. **Objective-C**

   ```objective-c title="Objective-C"
   TJPlacement *placement = [TJPlacement placementWithName:@"myPlacement" delegate:nil];
   [placement setEntryPoint:TJEntryPointMainMenu];
   [placement requestContent]; 
   ```

2. **Swift**

   ```swift
   let placement = TJPlacement(name: "myPlacement", delegate: nil)
   placement?.entryPoint = TJEntryPoint.mainMenu
   placement?.requestContent() 
   ```
