Interstitial Ads
In the examples below, an interstitial ad is displayed when triggered by a user action. When the ad status changes, feedback is displayed with the corresponding message.
In a real application, you could use the dismissed, display error, or timeout events to trigger navigation to another screen.
iOS
final class InterstitialAdManager: NSObject, ObservableObject, FuseFullScreenAdViewDelegate {
private let ad: FuseFullScreenAdView
init(code: String) {
ad = FuseFullScreenAdView(code: code)
ad.delegate = self
}
func showAd() {
ad.show()
}
func onEvent(adView: FuseFullScreenAdView, event: FuseAdViewEvent) {
print("Ad event: \(event)")
}
}
struct MyView: View {
@StateObject private var adManager = InterstitialAdManager(code: "<zone_code>")
var body: some View {
NavigationStack {
ScrollView {
VStack {
// ...
Button("Show interstitial ad") {
adManager.showAd()
}
// ...
}
}
}
}
}
For a complete working example, see
MainView.swiftin the iOS example repository.
Android
class MainActivity : AppCompatActivity() {
private var interstitialAd = FuseFullScreenAdView(this, "<zone_code>").apply {
listener = object : FuseFullScreenAdViewListener {
override fun onEvent(
adView: FuseFullScreenAdView,
event: FuseAdViewEvent,
) {
println("Ad event: $event")
}
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
R.id.action_interstitial -> {
interstitialAd.show(this)
true
}
else -> super.onOptionsItemSelected(item)
}
}
For a complete working example, see
MainActivity.ktin the Android example repository.
Flutter
import 'package:fuseapp_plugin/fuseapp.dart';
import 'package:flutter/services.dart';
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
FuseFullScreenAdView? interstitialAd;
@override
void initState() {
super.initState();
initializeSDK();
}
Future<void> initializeSDK() async {
try {
await FuseAppPlugin.initializeSDK();
} on PlatformException catch (e) {
debugPrint("SDK init failed: ${e.message}");
}
if (!mounted) return;
setState(() {
interstitialAd = FuseFullScreenAdView(code: "interstitial_zone_code");
});
}
@override
void dispose() {
interstitialAd?.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Fuse App Example')),
body: ElevatedButton(
onPressed: () async {
final ad = interstitialAd;
if (ad == null) {
debugPrint("Ad not ready yet - SDK still initializing");
return;
}
try {
await ad.show();
} catch (e) {
debugPrint("Unable to show ad: $e");
}
},
child: const Text("Show Ad"),
),
),
);
}
}