Cast 框架支持 iOS 14 及更高版本,既有静态框架,也有动态框架。
如需查看所有类和方法的说明,请参阅 Google Cast iOS API 参考文档。
Xcode 设置
iOS 14
-
将 Cast iOS SDK 4.8.1 添加到您的项目
如果使用 CocoaPods,请使用
pod update
将 4.8.1 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.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 库:
设置步骤
如需安装该库,请执行以下操作:
- 下载并解压缩适用于您的项目的 SDK。
- 设置动态 GoogleCastSDK 库:
-
将解压缩的
.xcframework
拖动到 Xcode 项目导航器中的主项目中(如果有 Pod 项目,则不要拖动到其中)。选中“Copy all items if needed”,然后添加到所有目标中。 -
在 Xcode 目标中的
General
标签页下,选择Embed and Sign
以表示GoogleCast.xcframework
。
如果要设置静态库,除了上述步骤外,还需执行以下步骤:
- 设置最低版本为 v3.13 的 Protobuf 库。
- 如果您的项目使用 CocoaPods:
-
打开
Podfile
并移除google-cast-sdk
(如果存在):pod 'google-cast-sdk'
-
如果
Protobuf
库不存在,请添加:pod 'Protobuf', '3.13'
-
在项目的根文件夹中运行
pod install
。 - 如果您的项目未使用 CocoaPods:
-
移除当前版本的
GoogleCastSDK
(如果存在)。 - 按照 Protobuf GitHub 代码库上的说明添加 Protobuf 库 3.13 版或更高版本。
-
在您的 Xcode 项目中,将标志
-ObjC -lc++
添加到 Build Settings(构建设置)> Other Linker Flags(其他链接器标志)中。 -
在解压缩的目录中找到“Resources”文件夹,然后将
GoogleCastCoreResources.bundle
、GoogleCastUIResources.bundle
和MaterialDialogs.bundle
拖动到您先前添加的GoogleCast.xcframework
旁边的项目中。选中“Copy all items if needed”,然后添加到所有目标中。
Mac Catalyst 设置
对于支持 Mac Catalyst 的应用,请使用 Cast SDK 的动态库。 按照手动设置流程将框架添加到您的项目中。然后,有条件地从 Mac 目标中排除 Cast SDK,如 Apple 文档中所述。静态库针对 iOS 架构进行了预编译,这会导致在针对 Mac 目标进行构建时引发链接器错误。