Flutter Deep Link
A guide to implementing deep links with the NBase AdStage SDK in a Flutter application.
- A deep link is a URL that takes the user directly to a specific screen in your app.
- The AdStage SDK provides deep link creation, handling, and tracking.
# 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">
<!-- Deep link 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/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>
<!-- Add additional schemes if needed -->
</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('Deep link received: ${event.path}');
print('Parameters: ${event.parameters}');
// Navigate based on deep link
_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);
}
}
When the app is launched via a deep link while it is terminated, check for a pending deep link at startup.
Future<void> checkPendingDeepLink() async {
try {
final result = await NbaseAdFlutterSdk.handleDeepLinkIntent();
if (result != null) {
_handleDeepLink(result);
}
} catch (e) {
print('Failed to handle pending deep link: $e');
}
}
@override
void initState() {
super.initState();
_setupDeepLinkListener();
// Check pending deep link after app start
WidgetsBinding.instance.addPostFrameCallback((_) {
checkPendingDeepLink();
});
}
- Deep link not received: verify URL scheme/domain, API key, and SDK initialization.
- Android build errors: run
flutter clean && flutter pub get and verify Manifest setup.
- iOS build errors: run
cd ios && pod install and verify Info.plist setup.