Cast 框架支持 iOS 13 及更高版本,具有静态和动态框架两种类型。
如需查看所有类和方法的说明,请参阅 Google Cast iOS API 参考文档。
Xcode 设置
iOS 14
-
将 Cast iOS SDK 4.8.0 添加到您的项目中
如果使用 CocoaPods,请使用
pod update
将 4.8.0 SDK 添加到您的项目中。否则,请 手动拉取 SDK。
-
将
NSBonjourServices
添加到您的Info.plist
在
Info.plist
中指定NSBonjourServices
以允许本地网络发现在 iOS 14 上成功。您需要同时添加
_googlecast._tcp
和_<your-app-id>._googlecast._tcp
作为服务,才能使设备发现功能正常运行。appID 是您的接收器 ID,与
GCKDiscoveryCriteria
中定义的 ID 相同。更新以下示例
NSBonjourServices
定义,并将“ABCD1234”替换为您的应用 ID。 -
Add
NSLocalNetworkUsageDescription
to yourInfo.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 theNSLocalNetworkUsageDescription
such as to describe Cast discovery and other discovery services, like DIAL.This message will appear as part of the iOS Local Network Access dialog as shown in the mock.
-
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 版的所有版本(但不包括版本 2.0.0)。
手动设置
以下说明介绍了如何在不使用 CocoaPods 的情况下将 Cast iOS SDK 添加到您的项目中:
下载
下载下面的相应库后,请按照设置步骤将框架添加到您的项目中。XCFrameworks 的设置要求与旧版 Universal Frameworks 的设置要求相同。
通用框架
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
设置步骤
如需安装其中一个 static 库,请执行以下操作:
- 下载并解压缩适合您项目的静态 SDK。
- 设置具有最低版本 v3.13 的 Protobuf 库。
- 如果您的项目使用 CocoaPods:
-
打开
Podfile
并移除google-cast-sdk
(如果存在):pod 'google-cast-sdk'
-
如果不存在,请添加
Protobuf
库:pod 'Protobuf', '3.13'
-
在项目的根文件夹中运行
pod install
。 - 如果您的项目未使用 CocoaPods:
-
移除当前版本的
GoogleCastSDK
(如果存在)。 - 按照 Protobuf GitHub 代码库上的说明添加 Protobuf 库 v3.13 或更高版本。
-
将解压缩的
.framework
或.xcframework
拖动到 Xcode 项目导航器中的主项目中(如果您有 Pod 项目,则不要将其拖入)。选中“Copy all items if needed”,然后将其添加到所有目标。 -
在您的 Xcode 项目中,将标志
-ObjC -lc++
添加到 Build Settings(构建设置)> Other Linker Flags(其他链接器标志)中。 -
右键点击项目中的
GoogleCast.framework
或GoogleCast.xcframework
,然后选择“在 Finder 中显示”。 -
将
GoogleCastCoreResources.bundle
和GoogleCastUIResources.bundle
拖动到您先前添加的GoogleCast.framework
或GoogleCast.xcframework
旁边的项目中,选中“Copy all items if needed”,然后将其添加到所有目标中。
如需安装其中一个动态库,请执行以下操作:
- 下载并解压缩适合您的项目的动态 SDK。
- 设置动态 GoogleCastSDK 库:
- 如果您的项目使用 CocoaPods:
-
打开
Podfile
并移除:pod 'google-cast-sdk'
-
在项目的根文件夹中运行
pod install
。 - 如果您的项目未使用 CocoaPods,请移除当前的 Google Cast 库。
-
将解压缩的
.framework
或.xcframework
拖动到 Xcode 项目导航器中的主项目中(如果您有 Pod 项目,则不要将其拖入)。选中“Copy all items if needed”,然后将其添加到所有目标。 -
在您的 Xcode 目标中的
General
标签页下,选择Embed and Sign
(代表GoogleCast.framework
)或GoogleCast.xcframework
。
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 中。