Flutter
Flutter アプリケーションで NBase AdStage SDK を使ってディープリンクを実装するためのガイドです。
- ディープリンクは、ユーザーをアプリの特定の画面へ直接移動させる URL です。
- AdStage SDK は、ディープリンクの作成・処理・トラッキングの機能を提供します。
# pubspec.yaml
dependencies:
nbase_ad_flutter_sdk: ^1.0.1
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/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)
}
}
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);
}
}
アプリが終了した状態からディープリンクで起動された場合、起動時に保留中だったリンクを確認します。
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();
});
}
- ディープリンクを受信できない:URL スキーム/ドメインの設定と、API キー/SDK の初期化状況を確認します
- Android のビルドエラー:
flutter clean && flutter pub get を実行してから再ビルドし、Manifest の設定を確認します
- iOS のビルドエラー:
cd ios && pod install を実行してから再ビルドし、Info.plist の設定を確認します