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 新增到專案:

下載內容

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

投放 iOS Sender SDK 4.8.1 程式庫:

靜態 動態

設定步驟

安裝程式庫的步驟如下:

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

如要設定靜態程式庫,除了上述步驟之外,請一併按照下列步驟操作:

  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 目標建構時會導致連結器錯誤。