tvOS 基本集成
SDK 统计
Open Source Github Repo: https://github.com/BranchMetrics/ios-branch-deep-linking
SDK 大小:〜220kb(启用了所有 Branch 功能)
速度 :中值80ms至250ms
XCode 最低版本 :12+
操作系统最低版本:iOS 9+
iOS 14 实施
为了帮助您全面管理用户体验,Branch SDK 将不会触发 IDFA 许可弹窗。
然而,如果您选择触发这一弹窗,在可行的情况下,Branch 仍然可以收集并使用 IDFA。
配置 Branch
tvOS 使用与 iOS 相同的 Universal Link。它不支持重定向到 URI scheme 或 Web。
-
Configure the default link settings for your app
-
Make sure
I have an iOS app
andEnable Universal Links
are selected

配置 Bundle ID
- Make sure Bundle Id matches your Branch Dashboard

Branch 假设您对所有 Apple 平台使用相同的 Bundle ID。如果您使用不同的 Bundle ID,则可以在初始化 Branch SDK 时将 Bundle ID 设置为一致的值。
配置 Associated Domain
- In the Xcode
Signing & Capabilities
tab, addAssociated Domains
- Add your link domains from your Branch Dashboard
-alternate
is needed for Universal Linking with the Web SDK inside your Websitetest-
要使用 test key 时需要- If you use a custom link domain, you will need to include your old link domain, your
-alternate
link domain, and your new link domain

配置 Info.plist
-
Add Branch Dashboard values
- 添加
branch_universal_link_domains
with your live key domain - 添加
branch_key
with your current Branch key - Add your URI scheme as
URL Types
>Item 0
>URL Schemes
- 添加

确认应用前缀(App Prefix)
- From your Apple Developer Account

安装 Branch
选项1
platform :tvos, '9.0'
target 'APP_NAME' do
# if swift
use_frameworks!
pod 'Branch'
end
pod install && pod update
选项2
Carthage 0.36.x不包括对 xcframeworks 的支持
Carthage support for xcframeworks is not included in the 0.36.x release. You can install Carthage from source to gain access to the
--use-xcframeworks
option.
Branch iOS SDK now supports xcframework and requires the --use-xcframeworks
option.
github "BranchMetrics/ios-branch-deep-linking"
- Import the
Branch.xcframework
into Linked Frameworks - Import
AdSupport
andCoreServices
into Linked Frameworks
选项3
From the 0.37.0 release, the Branch iOS SDK github releases page includes a prebuilt xcframework in Branch.zip
and a checksum.
- Drag and drop
Branch.xcframework
into *Embedded Binaries (select Copy items if needed**) - Import
AdSupport
andCoreServices
, into Linked Frameworks

初始化 Branch
在应用的 AppDelegate 中
import UIKit
import Branch
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// if you are using the TEST key
Branch.setUseTestBranchKey(true)
// listener for Branch Deep Link data
Branch.getInstance().initSession(launchOptions: launchOptions, andRegisterDeepLinkHandler: { (params, error) in
// do stuff with deep link data (nav to page, display content, etc)
print(params as? [String: AnyObject] ?? {})
}
return true
}
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
Branch.getInstance().application(app, open: url, options: options)
return true
}
func application(_ application: UIApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([Any]?) -> Void) -> Bool {
// handler for Universal Links
Branch.getInstance().continue(userActivity)
return true
}
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// handler for Push Notifications
Branch.getInstance().handlePushNotification(userInfo)
}
在 tvOS 上的 App to App linking
tvOS 没有 Web 浏览器或 Web 视图。我们通过 App to App programmatic linking 来解决此限制。
在此部分中, "广告合作伙伴应用"指的是希望连接到已启用 Branch 的 tvOS 应用的 tvOS 应用,即"目标应用" 。广告合作伙伴应用无需启用 Branch 功能。
App to App linking 在广告合作伙伴应用中实现。
- Add the
AdSupport
framework. This is used to obtain the device IDFA. - Enable the ad partner app to query for your tvOS app by adding your URI scheme to the Info.plist. See canOpenURL

以下代码演示了如何从广告合作伙伴应用链接到目标应用。
- 目标应用的 Branch Link 附加了设备 IDFA 作为查询参数。
- 我们使用 canOpenURL 查询目标应用。注意,canOpenURL 正在检查 URI scheme,而 Branch Link 是 Universal Link。
- 如果我们检测到应用,则尝试使用 Branch Link 将其打开。如果我们未检测到应用,或者未能通过 Branch Link 打开该应用,我们则将退回并发送 click 到 Branch Server,然后打开应用商店。
// This example opens the target app using a Branch Link. This does get Branch parameters and deferred deeplink data.
@IBAction func testBranchLink() {
// Since tvOS only supports app to app linking, we simply pass the advertising identifier as a query parameter
// Also added the adpartner parameter just to indicate where this came from, not strictly necessary
let branchLink = "https://bnctestbed.app.link/cCWdYYokQ6?$os=tv_os&$idfa=" + self.checkIdfa()
guard let url = URL(string: branchLink) else { return }
guard let uriScheme = URL(string: "branchtest://") else { return }
self.openURL(url: url, uriScheme: uriScheme)
}
func checkIdfa() -> String {
return ASIdentifierManager.shared().advertisingIdentifier.uuidString
}
// We assume the uri scheme is for the same app as the universal link url
func openURL(url:URL, uriScheme:URL) {
// canOpenURL can only check URI schemes listed in the Info.plist. It cannot check Universal Links.
// https://developer.apple.com/documentation/uikit/uiapplication/1622952-canopenurl
if (UIApplication.shared.canOpenURL(uriScheme)) {
if #available(tvOS 10.0, *) {
UIApplication.shared.open(url, options: [:]) { (success) in
if (success == false) {
self.clickBranchLink(url: url)
}
}
} else {
let success = UIApplication.shared.openURL(url)
if (success == false) {
self.clickBranchLink(url: url)
}
}
} else {
self.clickBranchLink(url: url)
}
}
func clickBranchLink(url:URL) {
URLSession.shared.dataTask(with: url) { (data, response, error) in
DispatchQueue.main.async {
self.openAppStore()
}
}.resume()
}
// directly open the app store if we're unable to detect the app
func openAppStore() {
guard let url = URL(string:"https://apps.apple.com/us/app/branch-monster-factory/id917737838?mt=8") else { return }
if #available(tvOS 10.0, *) {
UIApplication.shared.open(url, options: [:]) { (success) in
}
} else {
UIApplication.shared.openURL(url)
}
}
Updated 4 months ago