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。

15901590

配置 Bundle ID

19121912

Branch 假设您对所有 Apple 平台使用相同的 Bundle ID。如果您使用不同的 Bundle ID,则可以在初始化 Branch SDK 时将 Bundle ID 设置为一致的值。

配置 Associated Domain

  • In the Xcode Signing & Capabilities tab, add Associated Domains
  • Add your link domains from your Branch Dashboard
  • -alternate is needed for Universal Linking with the Web SDK inside your Website
  • test- 要使用 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
19181918

配置 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
17161716

确认应用前缀(App Prefix)

19841984

安装 Branch

选项1

CocoaPods

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.

Carthage 安装说明

Carthage

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 and CoreServices 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 and CoreServices, into Linked Frameworks
20922092

初始化 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
27822782

以下代码演示了如何从广告合作伙伴应用链接到目标应用。

  • 目标应用的 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)
        }
    }

这个页面对您有帮助吗?