使用 Cast 应用框架 (CAF) 开发 iOS 版应用的设置

Cast 框架支持 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. 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.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 的设置要求与旧版 Universal Framework 相同。

通用框架

Cast iOS Sender SDK 4.8.0 库:

静态(使用蓝牙) 静态(不使用蓝牙)

动态(使用蓝牙) 动态(不使用蓝牙)

XCFramework

Cast iOS SDK 支持 XCFrameworks,支持在使用 Apple Silicon 的 Mac 计算机上进行开发,以及使用 iOS 模拟器进行调试。

Cast iOS Sender SDK XCFramework 4.8.0 库:

带蓝牙的静态 XCFramework 不带蓝牙的静态 XCFramework

带蓝牙的动态 XCFramework 不带蓝牙的动态 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 库 3.13 版或更高版本。
  3. 将解压缩的 .framework.xcframework 拖动到 Xcode 项目导航器中的主项目中(如果有 Pod 项目,则不要将其拖动到其中)。选中“Copy all items if needed”,然后添加到所有目标中。
  4. 在您的 Xcode 项目中,将标志 -ObjC -lc++ 添加到 Build Settings(构建设置)> Other Linker Flags(其他链接器标志)中。
  5. 右键点击项目中的 GoogleCast.frameworkGoogleCast.xcframework,然后选择“Show In 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 项目,则不要将其拖动到其中)。选中“Copy all items if needed”,然后添加到所有目标中。
  4. 在 Xcode 目标的 General 标签页下,为 GoogleCast.frameworkGoogleCast.xcframework 选择 Embed and Sign

Mac Catalyst 设置

对于支持 Mac Catalyst 的应用,请使用 Cast SDK 的动态库。 按照手动设置流程将框架添加到您的项目中。然后,有条件地从 Mac 目标中排除 Cast SDK,如 Apple 文档中所述。静态库针对 iOS 架构进行了预编译,这会导致在针对 Mac 目标进行构建时引发链接器错误。

将应用发布到 App Store

在将应用发布到 App Store 之前,您需要运行 Shell 脚本 strip_unused_archs.sh 以从 app bundle 中移除未使用的架构。此脚本包含在 Cast SDK for iOS 中。