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

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. NSBonjourServices 添加到您的 Info.plist

    Info.plist 中指定 NSBonjourServices 以 允许在 iOS 14 上成功发现本地网络。

    您需要同时添加_googlecast._tcp和 “_<your-app-id>._googlecast._tcp”作为设备的服务 才能正常工作

    appID 是您的接收器 ID,也就是您在 GCKDiscoveryCriteria

    更新以下示例 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&#39;将包含从 1.6.7 及更高版本的所有版本,但不包括 包括版本 2.0.0

手动设置

以下说明介绍了如何将 Cast iOS SDK 添加到您的项目 不使用 CocoaPods:

下载内容

下载以下相应库后,请按照 设置步骤,以便将框架添加到您的 项目。

Cast iOS Sender SDK 4.8.1 库:

静态 动态

设置步骤

如需安装该库,请执行以下操作:

  1. 下载并解压缩适用于您的项目的 SDK。
  2. 设置动态 GoogleCastSDK 库:
  3. 将解压缩的 .xcframework 拖动到 Xcode 项目导航器(如果有 Pod 项目,请勿进入)。 选中“复制所有项目(如果需要)”,然后添加到所有目标。
  4. 在 Xcode 目标中的 General 标签页下,为 GoogleCast.xcframework 选择 Embed and Sign

如果您要设置 static 库,除了 之前的步骤:

  1. 使用最低版本设置 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 库 v3.13 或更高版本, 请按照 Protobuf GitHub 代码库
  2. 在您的 Xcode 项目中,将标志 -ObjC -lc++ 添加到 构建设置 >其他链接器标记
  3. 在解压缩的目录中找到 Resources 文件夹,然后 拖动“GoogleCastCoreResources.bundle”, GoogleCastUIResources.bundleMaterialDialogs.bundle 进入您项目中的GoogleCast.xcframework 。选中“复制所有项目(如需要)”,然后添加至所有项目 目标。

Mac Catalyst 设置

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