adstage
移动 SDK深度链接

Flutter

本文是在 Flutter 应用中使用 NBase AdStage SDK 实现深度链接的指南。

1. 概述

  • 深度链接是把用户直接带到应用内特定页面的 URL。
  • AdStage SDK 提供深度链接的创建、处理与跟踪功能。

2. 快速开始

2.1 安装 SDK

# pubspec.yaml
dependencies:
	nbase_ad_flutter_sdk: ^1.0.1
flutter pub get

2.2 基本设置

Android 设置

android/app/src/main/kotlin/.../MainActivity.kt:

import android.content.Intent
import android.os.Bundle
import io.flutter.embedding.android.FlutterActivity
import io.nbase.adapter.adstage.AdStageSDK
 
class MainActivity : FlutterActivity() {
		override fun onCreate(savedInstanceState: Bundle?) {
				super.onCreate(savedInstanceState)
 
				AdStageSDK.initializeAdStage("YOUR_API_KEY")
		}
 
		override fun onNewIntent(intent: Intent) {
				super.onNewIntent(intent)
				setIntent(intent)
				handleDeepLink(intent)
		}
 
		private fun handleDeepLink(intent: Intent) {
				AdStageSDK.handleDeepLink(intent)
		}
}

android/app/src/main/AndroidManifest.xml:

<activity
		android:name=".MainActivity"
		android:exported="true"
		android:launchMode="singleTop">
 
		<!-- 深度链接 intent filter -->
		<intent-filter android:autoVerify="true">
				<action android:name="android.intent.action.VIEW" />
				<category android:name="android.intent.category.DEFAULT" />
				<category android:name="android.intent.category.BROWSABLE" />
				<data android:scheme="your-app-scheme" />
		</intent-filter>
</activity>

iOS 设置

ios/Runner/Info.plist:

<key>CFBundleURLTypes</key>
<array>
		<dict>
				<key>CFBundleURLName</key>
				<string>nbase-adstage</string>
				<key>CFBundleURLSchemes</key>
				<array>
						<string>your-app-scheme</string>
				</array>
		</dict>
		<!-- 需要时可额外注册其他 scheme -->
</array>

ios/Runner/AppDelegate.swift:

import UIKit
import Flutter
 
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
		override func application(
				_ application: UIApplication,
				didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
		) -> Bool {
				GeneratedPluginRegistrant.register(with: self)
 
				AdStageSDK.shared.initializeAdStage(apiKey: "YOUR_API_KEY")
				return super.application(application, didFinishLaunchingWithOptions: launchOptions)
		}
}

3. 基本用法

3.1 处理深度链接接收

void _setupDeepLinkListener() {
	NbaseAdFlutterSdk.onDeepLinkReceived.listen((event) {
		print('收到深度链接: ${event.path}');
		print('参数: ${event.parameters}');
 
		// 根据深度链接跳转页面
		_handleDeepLink(event);
	});
}
 
void _handleDeepLink(DeepLinkEvent event) {
	switch (event.path) {
		case '/product':
			Navigator.pushNamed(context, '/product', arguments: event.parameters);
			break;
		case '/user':
			Navigator.pushNamed(context, '/user', arguments: event.parameters);
			break;
		default:
			Navigator.pushNamedAndRemoveUntil(context, '/', (route) => false);
	}
}

3.2 处理 Pending 深度链接

当应用处于已终止状态并通过深度链接启动时,需要在启动时检查此前挂起的链接。

Future<void> checkPendingDeepLink() async {
	try {
		final result = await NbaseAdFlutterSdk.handleDeepLinkIntent();
		if (result != null) {
			_handleDeepLink(result);
		}
	} catch (e) {
		print('Pending 深度链接处理失败: $e');
	}
}
 
@override
void initState() {
	super.initState();
	_setupDeepLinkListener();
 
	// 应用启动后检查 Pending 深度链接
	WidgetsBinding.instance.addPostFrameCallback((_) {
		checkPendingDeepLink();
	});
}

4. 问题排查

  • 收不到深度链接:确认 URL scheme/域名设置,确认 API 密钥与 SDK 初始化是否完成
  • Android 构建错误:执行 flutter clean && flutter pub get 后重新构建,并确认 Manifest 设置
  • iOS 构建错误:执行 cd ios && pod install 后重新构建,并确认 Info.plist 设置

目录