iOS 用 Cast Application Framework(CAF)を使用した開発のセットアップ

キャスト フレームワークは iOS 13 以降をサポートしており、静的フレームワークと動的フレームワークの両方が用意されています。

すべてのクラスとメソッドの説明については、Google Cast iOS API リファレンスをご覧ください。

Xcode の設定

iOS 14

  1. Cast iOS SDK 4.8.0 をプロジェクトに追加する

    CocoaPods を使用している場合は、pod update を使用して 4.8.0 SDK をプロジェクトに追加します。

    それ以外の場合は、 SDK を手動で取得します。

  2. NSBonjourServicesInfo.plist に追加する

    iOS 14 でローカル ネットワークの検出が成功できるようにするには、Info.plistNSBonjourServices を指定します。

    デバイス検出を正しく動作させるには、_googlecast._tcp_<your-app-id>._googlecast._tcp の両方をサービスとして追加する必要があります。

    appID は receiverID(GCKDiscoveryCriteria で定義されている ID と同じ ID)です。

    次の NSBonjourServices 定義の例を更新し、「ABCD1234」を実際の appID に置き換えます。

    <key>NSBonjourServices</key>
    <array>
      <string>_googlecast._tcp</string>
      <string>_ABCD1234._googlecast._tcp</string>
    </array>
  3. Add NSLocalNetworkUsageDescription to your Info.plist

    We strongly recommend that you customize the message shown in the Local Network prompt by adding an app-specific permission string in your app's Info.plist file for the NSLocalNetworkUsageDescription such as to describe Cast discovery and other discovery services, like DIAL.

    <key>NSLocalNetworkUsageDescription</key>
    <string>${PRODUCT_NAME} uses the local network to discover Cast-enabled devices on your WiFi
    network.</string>

    This message will appear as part of the iOS Local Network Access dialog as shown in the mock.

    Cast Local Network Access permissions dialog image
  4. Re-release your app to the Apple App Store

    We recommend you also re-release your app using 4.8.0 as soon as possible.

iOS 13

iOS 12

Ensure that the Access WiFi Information switch in the Capabilities section of the target is set to "On".

Additionally, your provisioning profile will need to support the Access WiFi Information capability. This can be added in the Apple Developer Portal.

CocoaPods setup

The recommended way of integrating Google Cast is using CocoaPods. CocoaPods is supported for both the Universal Framework and XCFramework.

For a Universal Framework integration, use the google-cast-sdk and google-cast-sdk-no-bluetooth CocoaPods.

For an XCFramework integration, use the google-cast-sdk-xcframework and google-cast-sdk-no-bluetooth-xcframework CocoaPods.

To get started, follow the getting started guide.

Once CocoaPods is set up, follow the using CocoaPods guide to get your Podfile created and your project ready to use with the Google Cast SDK.

Here's an example of how to add the google-cast-sdk CocoaPod to your Podfile:

use_frameworks!

platform :ios, '13.0'

def target_pods
  pod 'google-cast-sdk'
end

target 'CastVideos-objc' do
  target_pods
end
target 'CastVideos-swift' do
  target_pods
end

google-cast-sdk-no-bluetooth CocoaPod を Podfile に追加する方法の例を次に示します。

use_frameworks!

platform :ios, '13.0'

def target_pods
  pod 'google-cast-sdk-no-bluetooth'
end

target 'CastVideos-objc' do
  target_pods
end
target 'CastVideos-swift' do
  target_pods
end

予期しない破壊的変更を防ぐために、プロジェクトには Pod の範囲を指定する必要があります。詳細については、podfile ガイドをご覧ください。

このスニペットでは、バージョン 4.8.0 と、次のメジャー バージョン(major.minor.patch)以前のバージョンが許可されます。

pod 'google-cast-sdk', '~> 4.8.0'

たとえば、「~> 1.6.7」と指定すると、1.6.7 とその前のすべてのバージョンが対象となります(バージョン 2.0.0 は含みません)。

手動セットアップ

CocoaPods を使用せずに Cast iOS SDK をプロジェクトに追加する手順は次のとおりです。

ダウンロード

以下の該当するライブラリをダウンロードしたら、設定手順に沿ってフレームワークをプロジェクトに追加します。XCFrameworks の設定要件は、従来のユニバーサル フレームワークと同じです。

ユニバーサル フレームワーク

Cast iOS Sender SDK 4.8.0 ライブラリ:

Bluetooth ありで静的 Bluetooth なしで静的

Bluetooth で動的 Bluetooth なしで動的

XCFramework

Cast iOS SDK は XCFrameworks をサポートしているため、Apple シリコンを搭載した Mac コンピュータでの開発や、iOS Simulator を使用したデバッグが可能です。

Cast iOS Sender SDK XCFramework 4.8.0 ライブラリ:

Bluetooth を使用する静的 XCFramework Bluetooth なしの静的 XCFramework

Bluetooth を使用する Dynamic XCFramework Bluetooth なしの Dynamic XCFramework

設定の手順

静的ライブラリのいずれかをインストールするには:

  1. プロジェクトに適した静的 SDK をダウンロードして解凍します。
  2. バージョン v3.13 以降で Protobuf ライブラリを設定します。
    1. プロジェクトで CocoaPods を使用している場合:
      1. Podfile を開き、google-cast-sdk が存在する場合は削除します。
        pod 'google-cast-sdk'
      2. Protobuf ライブラリが存在しない場合は追加します。
        pod 'Protobuf', '3.13'
      3. プロジェクトのルートフォルダで pod install を実行します。
    2. プロジェクトで CocoaPods を使用していない場合:
      1. GoogleCastSDK の現在のバージョンが存在する場合は削除します。
      2. Protobuf GitHub リポジトリの手順に沿って、Protobuf ライブラリ v3.13 以降を追加します。
  3. 解凍した .framework または .xcframework を Xcode プロジェクト ナビゲータのメイン プロジェクトにドラッグします(Pod プロジェクトがある場合は移動しません)。[Copy all items if needed] チェックボックスをオンにして、すべてのターゲットに追加します。
  4. Xcode プロジェクトで、-ObjC -lc++ フラグを [Build Settings] > [Other Linker Flags] に追加します。
  5. プロジェクトで GoogleCast.framework または GoogleCast.xcframework を右クリックし、[Find In Finder] を選択します。
  6. GoogleCastCoreResources.bundleGoogleCastUIResources.bundle を、先ほど追加した GoogleCast.framework または GoogleCast.xcframework の横のプロジェクトにドラッグし、[Copy all items if needed] をオンにして、すべてのターゲットに追加します。

動的ライブラリのいずれかをインストールするには:

  1. プロジェクトに適した動的 SDK をダウンロードして解凍します。
  2. 動的な GoogleCastSDK ライブラリをセットアップします。
    1. プロジェクトで CocoaPods を使用している場合:
      1. Podfile を開き、次を削除します。
        pod 'google-cast-sdk'
      2. プロジェクトのルートフォルダで pod install を実行します。
    2. プロジェクトで CocoaPods を使用していない場合は、現在の Google Cast ライブラリを削除します。
  3. 解凍した .framework または .xcframework を Xcode プロジェクト ナビゲータのメイン プロジェクトにドラッグします(Pod プロジェクトがある場合は移動しません)。[Copy all items if needed] チェックボックスをオンにして、すべてのターゲットに追加します。
  4. Xcode ターゲットの [General] タブで、GoogleCast.framework または GoogleCast.xcframeworkEmbed and Sign を選択します。

Mac Catalyst のセットアップ

Mac Catalyst をサポートするアプリの場合は、Cast SDK の動的ライブラリを使用します。 手動設定プロセスに沿って、フレームワークをプロジェクトに追加します。次に、Apple のドキュメントに記載されているように、Mac ターゲットから Cast SDK を条件付きで除外します。静的ライブラリは iOS アーキテクチャ用にプリコンパイルされているため、Mac ターゲットに対してビルドするとリンカーエラーが発生します。

アプリを App Store に公開する

アプリを App Store に公開する前に、シェル スクリプト strip_unused_archs.sh を実行して、App Bundle から未使用のアーキテクチャを削除する必要があります。このスクリプトは Cast SDK for iOS に含まれています。