Skip to content

Commit

Permalink
Add Ad inspector button to Interstitial example (#1126)
Browse files Browse the repository at this point in the history
* Added ad inspector code to interstitial example

* iOS 12

* pause/resume updated

* pause/resume

---------

Co-authored-by: Justin Malandruccolo <[email protected]>
  • Loading branch information
malandr2 and Justin Malandruccolo authored Jul 16, 2024
1 parent a1ef9f5 commit 596bf16
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>11.0</string>
<string>12.0</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@

/* Begin PBXBuildFile section */
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
331C808B294A63AB00263BE5 /* RunnerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 331C807B294A618700263BE5 /* RunnerTests.swift */; };
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FD1CF9000F007C117D /* Assets.xcassets */; };
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */; };
331C808B294A63AB00263BE5 /* RunnerTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 331C807B294A618700263BE5 /* RunnerTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -42,6 +42,8 @@
/* Begin PBXFileReference section */
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
331C807B294A618700263BE5 /* RunnerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RunnerTests.swift; sourceTree = "<group>"; };
331C8081294A63A400263BE5 /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
74858FAD1ED2DC5600515810 /* Runner-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Runner-Bridging-Header.h"; sourceTree = "<group>"; };
74858FAE1ED2DC5600515810 /* AppDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
Expand All @@ -53,8 +55,6 @@
97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
331C807B294A618700263BE5 /* RunnerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RunnerTests.swift; sourceTree = "<group>"; };
331C8081294A63A400263BE5 /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -68,6 +68,14 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
331C8082294A63A400263BE5 /* RunnerTests */ = {
isa = PBXGroup;
children = (
331C807B294A618700263BE5 /* RunnerTests.swift */,
);
path = RunnerTests;
sourceTree = "<group>";
};
9740EEB11CF90186004384FC /* Flutter */ = {
isa = PBXGroup;
children = (
Expand All @@ -79,14 +87,6 @@
name = Flutter;
sourceTree = "<group>";
};
331C8082294A63A400263BE5 /* RunnerTests */ = {
isa = PBXGroup;
children = (
331C807B294A618700263BE5 /* RunnerTests.swift */,
);
path = RunnerTests;
sourceTree = "<group>";
};
97C146E51CF9000F007C117D = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -129,7 +129,6 @@
buildConfigurationList = 331C8087294A63A400263BE5 /* Build configuration list for PBXNativeTarget "RunnerTests" */;
buildPhases = (
331C807D294A63A400263BE5 /* Sources */,
331C807E294A63A400263BE5 /* Frameworks */,
331C807F294A63A400263BE5 /* Resources */,
);
buildRules = (
Expand Down Expand Up @@ -363,6 +362,7 @@
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -377,7 +377,6 @@
};
331C8088294A63A400263BE5 /* Debug */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = AE0B7B92F70575B8D7E0D07E /* Pods-RunnerTests.debug.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
Expand All @@ -395,7 +394,6 @@
};
331C8089294A63A400263BE5 /* Release */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 89B67EB44CE7B6631473024E /* Pods-RunnerTests.release.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
Expand All @@ -411,7 +409,6 @@
};
331C808A294A63A400263BE5 /* Profile */ = {
isa = XCBuildConfiguration;
baseConfigurationReference = 640959BDD8F10B91D80A66BE /* Pods-RunnerTests.profile.xcconfig */;
buildSettings = {
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
Expand Down Expand Up @@ -541,6 +538,7 @@
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -563,6 +561,7 @@
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
ENABLE_BITCODE = NO;
INFOPLIST_FILE = Runner/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 12.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
9 changes: 9 additions & 0 deletions samples/admob/interstitial_example/lib/app_bar_item.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AppBarItem {
static const adInpsectorText = 'Ad Inspector';
static const privacySettingsText = 'Privacy Settings';

final String label;
final int value;

AppBarItem(this.label, this.value);
}
90 changes: 51 additions & 39 deletions samples/admob/interstitial_example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';

import 'app_bar_item.dart';
import 'consent_manager.dart';

void main() {
Expand All @@ -13,7 +14,7 @@ void main() {
));
}

/// A simple app that loads an interstitial ad.
/// An example app that loads an interstitial ad.
class InterstitialExample extends StatefulWidget {
const InterstitialExample({super.key});

Expand All @@ -22,14 +23,13 @@ class InterstitialExample extends StatefulWidget {
}

class InterstitialExampleState extends State<InterstitialExample> {
static const privacySettingsText = 'Privacy Settings';

InterstitialAd? _interstitialAd;
final _consentManager = ConsentManager();
final _gameLength = 5;
var _gamePaused = false;
var _gameOver = false;
var _isMobileAdsInitializeCalled = false;
var _isPrivacyOptionsRequired = false;
late var _counter = _gameLength;
Timer? _timer;

Expand All @@ -51,6 +51,9 @@ class InterstitialExampleState extends State<InterstitialExample> {
// Kick off the first play of the "game".
_startNewGame();

// Check if a privacy options entry point is required.
_getIsPrivacyOptionsRequired();

// Attempt to initialize the Mobile Ads SDK.
_initializeMobileAdsSDK();
});
Expand Down Expand Up @@ -90,9 +93,7 @@ class InterstitialExampleState extends State<InterstitialExample> {
home: Scaffold(
appBar: AppBar(
title: const Text('Interstitial Example'),
actions: _isMobileAdsInitializeCalled
? _privacySettingsAppBarAction()
: null,
actions: _appBarActions(),
),
body: Stack(
children: [
Expand Down Expand Up @@ -129,35 +130,39 @@ class InterstitialExampleState extends State<InterstitialExample> {
);
}

List<Widget> _privacySettingsAppBarAction() {
List<Widget> _appBarActions() {
var array = [AppBarItem(AppBarItem.adInpsectorText, 0)];

if (_isPrivacyOptionsRequired) {
array.add(AppBarItem(AppBarItem.privacySettingsText, 1));
}

return <Widget>[
// Regenerate the options menu to include a privacy setting.
FutureBuilder(
future: _consentManager.isPrivacyOptionsRequired(),
builder: (context, snapshot) {
final bool visibility = snapshot.data ?? false;
return Visibility(
visible: visibility,
child: PopupMenuButton<String>(
onSelected: (String result) {
if (result == privacySettingsText) {
_pauseGame();
_consentManager.showPrivacyOptionsForm((formError) {
if (formError != null) {
debugPrint(
"${formError.errorCode}: ${formError.message}");
}
_resumeGame();
});
}
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<String>>[
const PopupMenuItem<String>(
value: privacySettingsText,
child: Text(privacySettingsText))
],
));
PopupMenuButton<AppBarItem>(
itemBuilder: (context) => array
.map((item) => PopupMenuItem<AppBarItem>(
value: item,
child: Text(
item.label,
),
))
.toList(),
onSelected: (item) {
_pauseGame();
switch (item.value) {
case 0:
MobileAds.instance.openAdInspector((error) {
// Error will be non-null if ad inspector closed due to an error.
_resumeGame();
});
case 1:
_consentManager.showPrivacyOptionsForm((formError) {
if (formError != null) {
debugPrint("${formError.errorCode}: ${formError.message}");
}
_resumeGame();
});
}
})
];
}
Expand Down Expand Up @@ -234,21 +239,28 @@ class InterstitialExampleState extends State<InterstitialExample> {
});
}

/// Redraw the app bar actions if a privacy options entry point is required.
void _getIsPrivacyOptionsRequired() async {
if (await _consentManager.isPrivacyOptionsRequired()) {
setState(() {
_isPrivacyOptionsRequired = true;
});
}
}

/// Initialize the Mobile Ads SDK if the SDK has gathered consent aligned with
/// the app's configured messages.
void _initializeMobileAdsSDK() async {
if (_isMobileAdsInitializeCalled) {
return;
}

var canRequestAds = await _consentManager.canRequestAds();
if (canRequestAds) {
setState(() {
_isMobileAdsInitializeCalled = true;
});
if (await _consentManager.canRequestAds()) {
_isMobileAdsInitializeCalled = true;

// Initialize the Mobile Ads SDK.
MobileAds.instance.initialize();

// Load an ad.
_loadAd();
}
Expand Down

0 comments on commit 596bf16

Please sign in to comment.