You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Branch.io 是否支持与 NativeScript 集成?NS 应用能否使用 Branch?

Can I use Branch.io in a NativeScript project?

Absolutely! Even though there’s no official Branch.io library or documentation built specifically for NativeScript, you can absolutely integrate Branch into your NS project—you just need to leverage NativeScript’s core superpower: direct access to native Android and iOS APIs. Here’s how to do it step by step:

Step 1: Set up Branch.io dependencies

First, grab your Branch app keys (live and test keys) from your Branch dashboard. Then add the native SDKs to your project:

  • Android: Open App_Resources/Android/app/build.gradle and add the Branch SDK dependency to the dependencies block:
    dependencies {
        implementation 'io.branch.sdk.android:library:5.+'
    }
    
  • iOS: Create or update App_Resources/iOS/Podfile with the Branch pod:
    pod 'Branch'
    
    Then run pod install from the App_Resources/iOS directory to install the SDK.

Step 2: Initialize Branch.io on app launch

You’ll need to initialize Branch when your app starts, using platform-specific code to call the native SDKs.

Android Initialization

Add this to your main app file (like app.ts or main.ts) to handle launch initialization:

import { Application } from '@nativescript/core';

Application.on(Application.launchEvent, (args) => {
    if (args.android) {
        // Initialize Branch with auto-instance (pulls key from AndroidManifest)
        const branch = io.branch.sdk.android.Branch.getAutoInstance(args.android.context);
        
        // Optional: Handle deep link data here
        branch.initSession((params, error) => {
            if (!error) {
                console.log('Branch deep link params:', params);
            }
        }, args.android.intent.data);
    }
});

Don’t forget to add your Branch keys to App_Resources/Android/src/main/AndroidManifest.xml inside the <application> tag:

<meta-data android:name="io.branch.sdk.BranchKey" android:value="your_live_branch_key" />
<meta-data android:name="io.branch.sdk.BranchKey.test" android:value="your_test_branch_key" />

iOS Initialization

For iOS, initialize Branch in the app launch event:

import { Application } from '@nativescript/core';

Application.on(Application.launchEvent, (args) => {
    if (args.ios) {
        const branch = Branch.getInstance();
        branch.initSessionWithLaunchOptionsAndRegisterDeepLinkHandler(
            args.ios.launchOptions,
            (params, error) => {
                if (error) {
                    console.error('Branch init failed:', error.localizedDescription);
                    return;
                }
                console.log('Branch deep link params:', params);
            }
        );
    }
});

Add your Branch keys to App_Resources/iOS/Info.plist:

<key>branch_key</key>
<string>your_live_branch_key</string>
<key>branch_test_key</key>
<string>your_test_branch_key</string>

Step 3: Implement core Branch features

Once initialized, you can call any native Branch SDK methods directly from your NativeScript code. Here are common examples:

// Android example
if (Application.android) {
    const buo = new io.branch.sdk.android.BranchUniversalObject()
        .setCanonicalIdentifier('content/article-123')
        .setTitle('My Awesome Article')
        .setContentDescription('Check out this must-read content!');
    
    const linkProps = new io.branch.sdk.android.LinkProperties()
        .setChannel('twitter')
        .setFeature('share');
    
    buo.generateShortUrl(Application.android.context, linkProps, (url, error) => {
        if (!error) {
            console.log('Generated Branch link:', url);
        }
    });
}

// iOS example
if (Application.ios) {
    const buo = BranchUniversalObject.alloc().initWithCanonicalIdentifier('content/article-123');
    buo.title = 'My Awesome Article';
    buo.contentDescription = 'Check out this must-read content!';
    
    const linkProps = BranchLinkProperties.alloc().init();
    linkProps.channel = 'twitter';
    linkProps.feature = 'share';
    
    buo.getShortUrlWithLinkPropertiesAndCallback(linkProps, (url, error) => {
        if (!error) {
            console.log('Generated Branch link:', url);
        }
    });
}

Track user events

// Android
if (Application.android) {
    io.branch.sdk.android.Branch.getInstance().userCompletedAction('article_shared');
}

// iOS
if (Application.ios) {
    Branch.getInstance().userCompletedAction('article_shared');
}

Key Tips

  • Refer to the official Branch Android and iOS SDK docs for full method details—almost every native method translates directly to what you can call in NativeScript.
  • Always wrap platform-specific code in Application.android/Application.ios checks to avoid runtime errors.
  • Test deep links thoroughly on both platforms, including web-to-app redirects and app-to-app sharing.

内容的提问来源于stack exchange,提问作者João Neto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:39:59