Cast 架構支援 iOS 12 以上版本,同時提供靜態和動態架構。
如需所有類別和方法的說明,請參閱 Google Cast iOS API 參考資料。
請注意,新一代的投放裝置支援 iOS 的訪客模式,舊版裝置則不支援訪客模式。詳情請參閱「訪客模式 - 支援的投放裝置」。
Xcode 設定
iOS 14
-
將 Cast iOS SDK 4.7.0 新增至專案
如果使用 CocoaPods,請使用
pod update
將 4.7.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.7.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.
The google-cast-sdk and
google-cast-sdk-no-bluetooth
CocoaPods are available for download.
Use the google-cast-sdk-no-bluetooth
if your app does not require
guest mode or you do not wish to require Bluetooth®
permission.
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, '12.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, '12.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.7.0 和下一個主要版本 (major.minor.patch) 的版本:
pod 'google-cast-sdk', '~> 4.7.0'
舉例來說,「~> 1.6.7」會包含 1.6.7 以上版本中的所有版本,但不包含 2.0.0 版。
手動設定
請按照下列操作說明,在不使用 CocoaPods 的情況下將 Cast iOS SDK 新增至專案:
下載內容
在下方下載適當的程式庫後,按照設定步驟將架構新增至專案。XCFrameworks 的設定需求與舊版通用架構相同。
提供了訪客模式的程式庫,即應用程式不需要這項功能,或是您不想要求在 iOS 13 中引入的藍牙®權限。詳情請參閱 iOS 13 異動文件。
通用架構
投放 iOS send SDK 4.7.0 程式庫:
XCFramework (Beta 版)
從 4.7.1 版開始,Cast iOS SDK 支援 XCFrameworks,允許在 Apple M1 電腦上進行開發,並使用 M1 iOS 模擬器進行偵錯。
這些程式庫目前為 Beta 版。如果發現任何問題,請向 Cast SDK Issue Tracker 回報問題。
投放 iOS 寄件者 SDK XCFramework 4.7.1 程式庫:
提供訪客模式的靜態 XCFramework 無訪客模式的靜態 XCFramework
使用訪客模式的動態 XCFramework 不使用訪客模式的動態 XCFramework
設定步驟
如要安裝其中一個靜態程式庫:
- 為專案下載並解壓縮適當的靜態 SDK。
- 設定 Protobuf 程式庫,最低版本為 v3.13。
- 如果專案使用 CocoaPods:
-
開啟
Podfile
並移除google-cast-sdk
(如有):pod 'google-cast-sdk'
-
如果沒有,請加入
Protobuf
程式庫:pod 'Protobuf', '3.13'
-
在專案的根目錄中執行
pod install
。 - 如果專案未使用 CocoaPods:
-
移除目前的
GoogleCastSDK
版本 (如有)。 - 按照 Protobuf GitHub 存放區中的操作說明新增 Protobuf 程式庫 3.13 以上版本。
-
將解壓縮後的
.framework
或.xcframework
拖曳到 Xcode 專案導覽器的主要專案中 (如有 Pod,則將其拖曳至 Pod 專案)。勾選「視需要複製所有項目」並新增至所有指定目標。 -
在 Xcode 專案中,將旗標
-ObjC -lc++
新增至 「Build Settings」 > 其他連結器標記。 -
在專案中按一下滑鼠右鍵
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,則將其拖曳至 Pod 專案)。勾選「視需要複製所有項目」並新增至所有指定目標。 -
在 Xcode 目標的「
General
」分頁中,為GoogleCast.framework
或GoogleCast.xcframework
選取「Embed and Sign
」。
設定 Mac Catalyst
如果應用程式支援 Mac Catalyst,請使用 Cast SDK 的動態程式庫。按照手動設定程序將架構新增至專案。然後依 Apple 說明文件所述,從 Mac 目標中剔除投放 SDK。靜態資料庫會針對 iOS 架構預先編譯,因此在根據 Mac 目標進行建構時發生連接器錯誤。
將應用程式發布到 App Store
將應用程式發布至 App Store 之前,您必須執行殼層指令碼 strip_unused_archs.sh
,藉此移除應用程式套件中未使用的架構。這個指令碼位於 Cast SDK for iOS 中。