Skip to content

Commit

Permalink
Set plugin (googleads#916)
Browse files Browse the repository at this point in the history
* add plist and pbxproj to gitignore

add plist and pbxproj to gitignore

* Revert "add plist and pbxproj to gitignore"

This reverts commit e9715c9.

* * Add option to launch Ad Inspector from example app and add plugin version.
* Update `RequestConfiguration` with test device ID.

* * Add option to launch Ad Inspector from example app and add plugin version.
* Update `RequestConfiguration` with test device ID.
  • Loading branch information
srichakradhar authored Sep 11, 2023
1 parent fef7540 commit 87bceca
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import io.flutter.plugins.googlemobileads.FlutterAd.FlutterOverlayAd;
import io.flutter.plugins.googlemobileads.nativetemplates.FlutterNativeTemplateStyle;
import io.flutter.plugins.googlemobileads.usermessagingplatform.UserMessagingPlatformManager;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -670,6 +671,13 @@ public void onInitializationComplete(@NonNull InitializationStatus initializatio
if (isInitializationCompleted) {
return;
}
try {
Class<?> clazz = Class.forName("com.google.android.gms.ads.MobileAds");
Method method = clazz.getDeclaredMethod("setPlugin", String.class);
method.setAccessible(true);
method.invoke(null, Constants.REQUEST_AGENT_PREFIX_VERSIONED);
} catch (Exception ignored) {
}
result.success(new FlutterInitializationStatus(initializationStatus));
isInitializationCompleted = true;
}
Expand Down
15 changes: 15 additions & 0 deletions packages/google_mobile_ads/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'dart:io' show Platform;

import 'package:flutter/material.dart';
import 'package:google_mobile_ads/google_mobile_ads.dart';
import 'dart:developer';

import 'anchored_adaptive_example.dart';
import 'fluid_example.dart';
Expand Down Expand Up @@ -57,6 +58,7 @@ class _MyAppState extends State<MyApp> {
static const anchoredAdaptiveButtonText = 'Anchored adaptive';
static const nativeTemplateButtonText = 'Native template';
static const webviewExampleButtonText = 'Register WebView';
static const adInspectorButtonText = 'Ad Inspector';

InterstitialAd? _interstitialAd;
int _numInterstitialLoadAttempts = 0;
Expand All @@ -70,6 +72,8 @@ class _MyAppState extends State<MyApp> {
@override
void initState() {
super.initState();
MobileAds.instance.updateRequestConfiguration(
RequestConfiguration(testDeviceIds: [testDevice]));
_createInterstitialAd();
_createRewardedAd();
_createRewardedInterstitialAd();
Expand Down Expand Up @@ -288,6 +292,13 @@ class _MyAppState extends State<MyApp> {
builder: (context) => WebViewExample()),
);
break;
case adInspectorButtonText:
MobileAds.instance.openAdInspector((error) => log(
'Ad Inspector ' +
(error == null
? 'opened.'
: 'error: ' + (error.message ?? ''))));
break;
default:
throw AssertionError('unexpected button: $result');
}
Expand Down Expand Up @@ -325,6 +336,10 @@ class _MyAppState extends State<MyApp> {
value: webviewExampleButtonText,
child: Text(webviewExampleButtonText),
),
PopupMenuItem<String>(
value: adInspectorButtonText,
child: Text(adInspectorButtonText),
),
],
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#import "FLTGoogleMobileAdsPlugin.h"
#import "FLTAdUtil.h"
#import "FLTAppStateNotifier.h"
#import "FLTConstants.h"
#import "FLTNSString.h"
#import "UserMessagingPlatform/FLTUserMessagingPlatformManager.h"
@import webview_flutter_wkwebview;
Expand All @@ -31,6 +32,10 @@ - (instancetype)initWithResult:(FlutterResult)result;
- (void)handleInitializationComplete:(GADInitializationStatus *_Nonnull)status;
@end

@interface GADMobileAds (Plugin)
- (void)setPlugin:(nullable NSString *)plugin;
@end

@implementation FLTInitializationHandler {
FlutterResult _result;
BOOL _isInitializationCompleted;
Expand All @@ -51,6 +56,10 @@ - (void)handleInitializationComplete:(GADInitializationStatus *_Nonnull)status {
}
_result([[FLTInitializationStatus alloc] initWithStatus:status]);
_isInitializationCompleted = true;
GADMobileAds *mobileAds = GADMobileAds.sharedInstance;
if ([mobileAds respondsToSelector:@selector(setPlugin:)]) {
[mobileAds setPlugin:FLT_REQUEST_AGENT_VERSIONED];
}
}

@end
Expand Down

0 comments on commit 87bceca

Please sign in to comment.