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 設定

目錄