iOS 適用的 Cast 應用程式架構 (CAF) 開發設定

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

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

Xcode 設定

iOS 14

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

    如果使用 CocoaPods,請使用 pod update 將 4.8.1 SDK 新增至專案。

    否則,請 手動提取 SDK

  2. Info.plist 中新增 NSBonjourServices

    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.8.1 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. For integration, use the google-cast-sdk 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, '14.0'

def target_pods
  pod 'google-cast-sdk'
end

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

您應該為專案指定 Pod 範圍,以免發生意外的破壞性變更;詳情請參閱 podfile 指南

在這個程式碼片段中,4.8.1 版和下一個主要版本 (major.minor.patch) 的版本都允許使用:

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

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

手動設定

以下操作說明是如何在不使用 CocoaPods 的情況下,將 Cast iOS SDK 新增至專案:

下載內容

下載下方的適當程式庫後,請按照設定步驟將架構新增至專案中。

Cast iOS Sender SDK 4.8.1 程式庫:

靜態 動態

設定步驟

如何安裝程式庫:

  1. 下載並擷取適用於專案的 SDK。
  2. 設定動態 GoogleCastSDK 程式庫:
  3. 將解壓縮後的 .xcframework 拖曳至 Xcode 專案導覽工具中的主要專案 (如果您有 Pod 專案,而不是 Pod 專案)。 勾選「視需要複製所有項目」,並新增到所有目標中。
  4. 在 Xcode 目標的 General 分頁下,為 GoogleCast.xcframework 選取 Embed and Sign

如果您要設定靜態程式庫,則除了前述步驟外,也請按照下列步驟操作:

  1. 設定最低版本為 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 程式庫 3.13 以上版本。
  2. 在 Xcode 專案中,將標記 -ObjC -lc++ 加到「Build Settings」 >「Other Linker Flags」中。
  3. 在解壓縮的目錄中找出「Resources」資料夾,然後將 GoogleCastCoreResources.bundleGoogleCastUIResources.bundleMaterialDialogs.bundle 拖曳到專案裡 (您先前新增的 GoogleCast.xcframework 旁邊)。勾選「視需要複製所有項目」,並新增至所有目標。

Mac Catalyst 設定

如果應用程式支援 Mac Catalyst,請使用 Cast SDK 的動態程式庫。 按照手動設定程序,將架構新增至專案。接著按照 Apple 說明文件中所述,有條件地從 Mac 目標中排除 Cast SDK。靜態程式庫已針對 iOS 架構預先編譯,因此針對 Mac 目標進行建構時,會導致連結器錯誤。