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 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>
		<!-- 必要に応じてスキームを追加登録できます -->
</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 スキーム/ドメインの設定と、API キー/SDK の初期化状況を確認します
  • Android のビルドエラー:flutter clean && flutter pub get を実行してから再ビルドし、Manifest の設定を確認します
  • iOS のビルドエラー:cd ios && pod install を実行してから再ビルドし、Info.plist の設定を確認します

目次