macOS 基本集成

👍

SDK 统计

Open Source Github Repo: https://github.com/BranchMetrics/mac-branch-deep-linking

🚧

先决条件

Before you implement the SDK, please ensure you have Configured Link Behaviors for Redirects and Link Domains.

1.安装 Branch SDK

请通过以下集成方法之一在您的应用中安装 Branch Framework。


Branch macOS SDK 1.3.0+ 支持 Swift Package Manager


要把 Branch macOS SDK GitHub repo 作为 Swift Package 依赖项添加,您需要

  1. 在项目导航器和项目编辑器中选择项目名称后,点击 Swift Packages 选项卡。
  2. 点击 + 键添加软件包。
  3. 使用 Branch macOS SDK 的程序包 URL,“ https://github.com/BranchMetrics/mac-branch-deep-linking”,然后继续。
  4. 确保将 Branch Swift Package 添加到正确的 target,然后点击 完成
  5. 如果还有其他需要使用 Branch Swift Package 的 target,请选择该 target 并将其添加到常规选项卡的 Frameworks, Libraries 和 Embedded Content下。

项目的 Swift Package 选项卡现在应如下所示:



Branch macOS SDK 1.3.0+ 支持 CocoaPods


  1. 如果您的项目目录中还没有 Pod 文件,请通过运行 init以初始化一个 Pod 文件。
  2. 现在,您的项目目录中应该有一个名为Podfile的文件。
  3. 您的Podfile应该如下所示:
  4. platform :osx, '10.14' target 'Example' do use_frameworks! pod 'BranchMacOS' end
  5. 由于您的依赖项已列在 Podfile,请通过运行 pod install 以安装依赖项。如果您还没有工作区,CocoaPods 将创建一个包含您刚刚安装的依赖项的工作区。CocoaPods 生成的框架应该已经添加到您在 Podfile 指定的 target。

Branch macOS SDK 1.3.0+ 支持 Carthage


github "BranchMetrics/mac-branch-deep-linking"


需要 Manual Team Signing


  1. 克隆 Branch macOS SDK:git clone https://github.com/BranchMetrics/mac-branch-deep-linking
  2. 在 Xcode 的 Project Navigator 的右下角,点击 + 键,然后选择将文件添加到工作区的选项。
  3. 选择在步骤1中克隆的 Branch macOS SDK 目录中的Branch.xcodeproj文件。现在,您应该在左侧的 Project Navigator 中看到 Branch 项目。
  4. 对于您的主项目,转到需要将 Branch macOS SDK 作为依赖项的 target,然后点击Frameworks,Libraries 和 Embedded Content 项下的+按钮将其添加。

2.配置 Info.plist

在此步骤中,您将在应用中添加 URL Sheme,以便 macOS 知道与该应用相关的方案。

  1. 首先,在 target 的 Info.plist 中添加 URL types 属性。打开 Info.plist 文件,点击将鼠标悬停在“信息属性列表(Information Property List)”上时显示的“+”按钮,并添加 “URL types”(如果不存在)。
23622362
  1. 完全展开 “URL types” 之后,在 “Item 0” 下添加 “URL Scheme” 属性。
23622362
  1. 在 “URL Scheme” 下为 “Item 0” 提供一个值。这是您的应用可以处理的方案。
23622362

📘

注意

If you want to edit the Info.plist as a XML document, right click on your Info.plist and open it as source code.

11661166

You can paste this snippet before the final </dict> tag. Remember to change YOUR-APP-SCHEME-HERE to the app scheme for your app.

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>YOUR-APP-SCHEME-HERE</string>
            </array>
        </dict>
    </array>

If you wish to use Universal Links with a custom link domain, add branch_universal_link_domains with your live key domain. This is only necessary if you do not use the default app.link domain.

这是带有自定义域的示例 xml。

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>YOUR-APP-SCHEME-HERE</string>
            </array>
                    <key>branch_universal_link_domains</key>
                    <array>
                <string>YOUR-CUSTOM-APP-LINK-DOMAIN</string>
                        </array>
        </dict>
    </array>

🚧

定义第一个 Scheme

您的应用的 URI scheme 必须是列表中定义的第一个方案(项目0)。

The Branch SDK will use the first URI Scheme from your list that does not start with fbdbtwitterkit-pin, or com.googleusercontent.apps. These schemes are ignored by Branch since they are commonly used by other app kits for oauth and other uses.

3.配置 macOS Universal Link

🚧

Chrome 或 Firefox 不支持 macOS Universal Link。

在这些浏览器上打开 Branch Link 时,我们将重定向到 URI 并打开本机应用。

Universal Link 可在 macOS Catalina 或更高版本上使用。配置后,从 Safari 或 WebKit Web 视图点击的 Branch Link 可以打开本机应用而无需 URI 重定向。

a. 配置关联域 (Associated Domains)

  1. Add your link domains from your Branch Dashboard.
  2. 如果 target 的 “Signing 和 Capabilities” 选项卡中没有关联域(Associated Domain)部分,请继续并通过点击+号将其添加。
11661166 19361936
  1. Add the associated domains from your Branch Dashboard.
19361936

📘

注意

-alternate is needed to ensure proper functioning of Universal Links & Deep Views for users that do not have your app installed. test- is needed if you need to use a 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

b. 检查权利文件(Entitlements)

  • Confirm entitlements are within target (This file is configured automatically when completing the steps above in Capabilities tab of Xcode)
21002100

c. 配置 AppDelegate 以处理 Universal Link

func application(_ application: NSApplication, continue userActivity: NSUserActivity, restorationHandler: @escaping ([NSUserActivityRestoring]) -> Void) -> Bool {
        Branch.sharedInstance.continue(userActivity)
        return true
    }
- (BOOL)application:(NSApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<NSUserActivityRestoring>> * _Nonnull))restorationHandler {
    [[Branch sharedInstance] continueUserActivity:userActivity];
    return YES;
}

4.初始化 Branch

Branch SDK 需要 macOS 应用中的外发连接。通过转到应用使用的 target 并在 Signing 和 Capabilities选项卡中启用外发连接(客户端) ,确保您的应用具有执行此操作的权限。

24662466

Start Branch when your app first starts up. In your app delegate, make sure to first import the Branch SDK. Then, start Branch in your applicationDidFinishLaunching: method

func applicationDidFinishLaunching(_ aNotification: Notification) {
        // Register for Branch URL notifications
        NotificationCenter.default.addObserver(self, selector: #selector(branchWillStartSession), name: .BranchWillStartSession, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(branchDidStartSession), name: .BranchDidStartSession, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(branchOpenedURLNotification), name: .BranchDidOpenURLWithSession, object: nil)
 
        // Create a Branch configuration object with your key:
        let configuration = BranchConfiguration(key: "YOUR_KEY_HERE")
 
        // Start Branch:
        Branch.sharedInstance.start(with: configuration)
    }
#import <Branch/Branch.h>

// In your app delegate class file add this method to start the Branch SDK:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application

    // Register for Branch URL notifications:
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(branchWillStartSession:) name:BranchWillStartSessionNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(branchDidStartSession:) name:BranchDidStartSessionNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(branchOpenedURLNotification:) name:BranchDidOpenURLWithSessionNotification object:nil];

    // Create a Branch configuration object with your key:
    BranchConfiguration *configuration = [[BranchConfiguration alloc] initWithKey:@"key_live_joQf7gfRz1vebNOoHPFGJhnhFCarsZg0"];

    // Start Branch:
    [[Branch sharedInstance] startWithConfiguration:configuration];
}

Next up add the notification handlers which are used by the observers in the applicationDidFinishLaunching: method

@objc private func branchWillStartSession(_ notification: Notification?) {
        guard let notification = notification else { return }
 
        let url = notification.userInfo?[BranchURLKey] ?? "N/A"
        print("branchWillStartSession: \(notification.name) URL: \(url)")
    }
 
    @objc private func branchDidStartSession(_ notification: Notification?) {
        guard let notification = notification else { return }
 
        let url = notification.userInfo?[BranchURLKey] ?? "N/A"
        let session = notification.userInfo?[BranchSessionKey] as? BranchSession
        let data = (session != nil && session?.data != nil) ? session?.data?.description ?? "" : ""
        print("branchDidStartSession: \(notification.name) URL: \(url) Data: \(data)")
    }
 
    @objc private func branchOpenedURLNotification(_ notification: Notification?) {
        guard let notification = notification else { return }
 
        let url = notification.userInfo?[BranchURLKey] ?? "N/A"
        print("branchOpenedURLNotification: \(notification.name) URL: \(url)")
 
        let session = notification.userInfo?[BranchSessionKey] as? BranchSession
        let linkContent = session?.linkContent
        displayLinkContent(linkContent)
    }
    
    private func displayLinkContent(_ linkContent: BranchUniversalObject?) {
        guard let linkContent = linkContent,
              let viewController = NSApplication.shared.keyWindow?.contentViewController as? ViewController
        else {
            return
        }
 
        viewController.updateContent(title: linkContent.title, contentDescription: linkContent.contentDescription, imageURLString: linkContent.imageUrl)
    }
- (void) branchWillStartSession:(NSNotification*)notification {
    NSLog(@"branchWillStartSession: %@", notification.name);

    NSString *url = notification.userInfo[BranchURLKey] ?: @"";
    NSLog(@"URL: %@", url);
}

- (void) branchDidStartSession:(NSNotification*)notification {
    NSLog(@"branchDidStartSession: %@", notification.name);

    NSString *url = notification.userInfo[BranchURLKey] ?: @"";
    NSLog(@"URL: %@", url);

    BranchSession *session = notification.userInfo[BranchSessionKey];
    NSString *data = (session && session.data) ? session.data.description : @"";
}

- (void) branchOpenedURLNotification:(NSNotification*)notification {
    NSLog(@"branchOpenedURLNotification: %@", notification.name);

    NSString *url = notification.userInfo[BranchURLKey] ?: @"";
    NSLog(@"URL: %@", url);

    BranchSession *session = notification.userInfo[BranchSessionKey];

    // Do something with the link!
    // In this contrived example we'll load a view controller that plays the song that was in the link:
    SongViewController *viewController = [SongViewController loadController];
    viewController.songTitle = branchSession.linkContent.title;
    [viewController.window makeKeyAndOrderFront:self];
    [viewController playSong];
}

这个页面对您有帮助吗?