適用於 iOS 的投放應用程式架構 (CAF) 開發作業

Cast 架構支援 iOS 12 以上版本,同時提供靜態和動態架構。

如需所有類別和方法的說明,請參閱 Google Cast iOS API 參考資料

請注意,新一代的投放裝置支援 iOS 的訪客模式,舊版裝置則不支援訪客模式。詳情請參閱「訪客模式 - 支援的投放裝置」。

Xcode 設定

iOS 14

  1. 將 Cast iOS SDK 4.7.0 新增至專案

    如果使用 CocoaPods,請使用 pod update 將 4.7.0 SDK 加進專案。

    否則,請 手動提取 SDK

  2. NSBonjourServices 新增到 Info.plist

    Info.plist 中指定 NSBonjourServices,讓本機網路探索可以在 iOS 14 上成功執行。

    您必須同時新增 _googlecast._tcp_<your-app-id>._googlecast._tcp 做為服務,裝置探索才能正常運作。

    appID 是接收器 ID,也就是您在 GCKDiscoveryCriteria 中定義的 ID。

    更新下列 NSBonjourServices 定義,並將「ABCD1234」改成您的應用程式 ID。

    <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.7.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.

The google-cast-sdk and google-cast-sdk-no-bluetooth CocoaPods are available for download.

Use the google-cast-sdk-no-bluetooth if your app does not require guest mode or you do not wish to require Bluetooth® permission.

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, '12.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, '12.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.7.0 和下一個主要版本 (major.minor.patch) 的版本:

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

舉例來說,「~> 1.6.7」會包含 1.6.7 以上版本中的所有版本,但不包含 2.0.0 版。

手動設定

請按照下列操作說明,在不使用 CocoaPods 的情況下將 Cast iOS SDK 新增至專案:

下載內容

在下方下載適當的程式庫後,按照設定步驟將架構新增至專案。XCFrameworks 的設定需求與舊版通用架構相同。

提供了訪客模式的程式庫,即應用程式不需要這項功能,或是您不想要求在 iOS 13 中引入的藍牙®權限。詳情請參閱 iOS 13 異動文件

通用架構

投放 iOS send SDK 4.7.0 程式庫:

具有訪客模式的靜態模式 靜態模式 (無訪客模式)

使用訪客模式的動態模式 動態模式 (不使用訪客模式)

XCFramework (Beta 版)

從 4.7.1 版開始,Cast iOS SDK 支援 XCFrameworks,允許在 Apple M1 電腦上進行開發,並使用 M1 iOS 模擬器進行偵錯。

這些程式庫目前為 Beta 版。如果發現任何問題,請向 Cast SDK Issue Tracker 回報問題。

投放 iOS 寄件者 SDK XCFramework 4.7.1 程式庫:

提供訪客模式的靜態 XCFramework 無訪客模式的靜態 XCFramework

使用訪客模式的動態 XCFramework 不使用訪客模式的動態 XCFramework

設定步驟

如要安裝其中一個靜態程式庫:

  1. 為專案下載並解壓縮適當的靜態 SDK。
  2. 設定 Protobuf 程式庫,最低版本為 v3.13。
    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 程式庫 3.13 以上版本。
  3. 將解壓縮後的 .framework.xcframework 拖曳到 Xcode 專案導覽器的主要專案中 (如有 Pod,則將其拖曳至 Pod 專案)。勾選「視需要複製所有項目」並新增至所有指定目標。
  4. 在 Xcode 專案中,將旗標 -ObjC -lc++ 新增至 「Build Settings」 > 其他連結器標記
  5. 在專案中按一下滑鼠右鍵 GoogleCast.frameworkGoogleCast.xcframework,然後選取「在 Finder 中顯示」。
  6. GoogleCastCoreResources.bundleGoogleCastUIResources.bundle 拖曳至您先前新增的 GoogleCast.frameworkGoogleCast.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,則將其拖曳至 Pod 專案)。勾選「視需要複製所有項目」並新增至所有指定目標。
  4. 在 Xcode 目標的「General」分頁中,為 GoogleCast.frameworkGoogleCast.xcframework 選取「Embed and Sign」。

設定 Mac Catalyst

如果應用程式支援 Mac Catalyst,請使用 Cast SDK 的動態程式庫。按照手動設定程序將架構新增至專案。然後依 Apple 說明文件所述,從 Mac 目標中剔除投放 SDK。靜態資料庫會針對 iOS 架構預先編譯,因此在根據 Mac 目標進行建構時發生連接器錯誤。

將應用程式發布到 App Store

將應用程式發布至 App Store 之前,您必須執行殼層指令碼 strip_unused_archs.sh,藉此移除應用程式套件中未使用的架構。這個指令碼位於 Cast SDK for iOS 中。