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”替换为您的 appID。 -
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(但不包括 2.0.0)的所有版本。
手动设置
以下说明介绍了如何在不使用 CocoaPods 的情况下将 Cast iOS SDK 添加到您的项目:
下载内容
下载下面的相应库后,请按照设置步骤将该框架添加到您的项目中。
Cast iOS Sender SDK 4.8.1 库:
设置步骤
如需安装该库,请执行以下操作:
- 下载并解压缩适合您的项目的 SDK。
- 设置动态 GoogleCastSDK 库:
-
在 Xcode 项目导航器中将解压缩的
.xcframework
拖动到主项目中(如果您有 Pod 项目,请勿将其拖动到 Pod 项目中)。选中“Copy all items if needed”,并将其添加到所有目标。 -
在 Xcode 目标中的
General
标签页下,为GoogleCast.xcframework
选择Embed and Sign
。
如果您要设置 static 库,则除了之前的步骤外,还执行以下步骤:
- 设置最低版本为 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 或更高版本。
-
在您的 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 的动态库。 按照手动设置流程将该框架添加到您的项目中。然后,按照 Apple 文档中的说明,有条件地从 Mac 目标中排除 Cast SDK。静态库针对 iOS 架构进行了预编译,这会导致在针对 Mac 目标进行构建时出现链接器错误。