From 656e8a54bea9a05747868817b73d6c42c148e1d9 Mon Sep 17 00:00:00 2001 From: AGulev Date: Tue, 7 Nov 2023 14:29:05 +0100 Subject: [PATCH] fix https://github.com/defold/extension-admob/issues/37 --- extension-admob/src/admob.cpp | 3 +- extension-admob/src/admob_android.cpp | 4 +++ extension-admob/src/admob_ios.mm | 41 ++++++++++++++++++++++++++- extension-admob/src/admob_private.h | 1 + main/ads.gui_script | 2 +- 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/extension-admob/src/admob.cpp b/extension-admob/src/admob.cpp index 982605f..8f633cf 100644 --- a/extension-admob/src/admob.cpp +++ b/extension-admob/src/admob.cpp @@ -13,7 +13,7 @@ namespace dmAdmob { -static const char DEFOLD_USERAGENT[] = "defold-3.2.0"; +static const char DEFOLD_USERAGENT[] = "defold-3.3.1"; static int Lua_Initialize(lua_State* L) { @@ -263,6 +263,7 @@ static dmExtension::Result InitializeAdmob(dmExtension::Params* params) static dmExtension::Result AppFinalizeAdmob(dmExtension::AppParams* params) { + Finalize_Ext(); return dmExtension::RESULT_OK; } diff --git a/extension-admob/src/admob_android.cpp b/extension-admob/src/admob_android.cpp index 8c5cd87..db2b820 100644 --- a/extension-admob/src/admob_android.cpp +++ b/extension-admob/src/admob_android.cpp @@ -128,6 +128,10 @@ void Initialize_Ext() g_admob.m_AdmobJNI = env->NewGlobalRef(env->NewObject(cls, jni_constructor, threadAttacher.GetActivity()->clazz)); } +void Finalize_Ext() +{ +} + void Initialize(const char* defoldUserAgent) { CallVoidMethodChar(g_admob.m_AdmobJNI, g_admob.m_Initialize, defoldUserAgent); diff --git a/extension-admob/src/admob_ios.mm b/extension-admob/src/admob_ios.mm index 21aafa3..489a0e5 100755 --- a/extension-admob/src/admob_ios.mm +++ b/extension-admob/src/admob_ios.mm @@ -20,11 +20,15 @@ @interface AdmobExtRewardedAdDelegate : NSObject @interface AdmobExtBannerAdDelegate : NSObject @end +@interface AdMobAppDelegate : NSObject +@end + namespace dmAdmob { static const char* m_DefoldUserAgent = nil; static UIViewController *uiViewController = nil; + static AdMobAppDelegate *admobAppDelegate = nil; void SendSimpleMessage(MessageId msg, id obj) { NSError* error; @@ -316,6 +320,10 @@ void UpdatePosition(BannerPosition bannerSizeConst) { bannerAd.center = bannerPos; } + void UpdatePosition() { + UpdatePosition(lastBannerPos); + } + bool IsBannerLoaded() { return bannerAd != nil; } @@ -342,7 +350,7 @@ void ShowBanner(BannerPosition bannerPos) { if (bannerPos != POS_NONE) { lastBannerPos = bannerPos; } - UpdatePosition(lastBannerPos); + UpdatePosition(); } } @@ -373,6 +381,17 @@ void Initialize_Ext() { admobExtInterstitialAdDelegate = [[AdmobExtInterstitialAdDelegate alloc] init]; admobExtRewardedAdDelegate = [[AdmobExtRewardedAdDelegate alloc] init]; admobExtBannerAdDelegate = [[AdmobExtBannerAdDelegate alloc] init]; + admobAppDelegate = [[AdMobAppDelegate alloc] init]; + + dmExtension::RegisteriOSUIApplicationDelegate(admobAppDelegate); +} + +void Finalize_Ext() { + dmExtension::UnregisteriOSUIApplicationDelegate(admobAppDelegate); + [admobExtInterstitialAdDelegate dealloc]; + [admobExtRewardedAdDelegate dealloc]; + [admobExtBannerAdDelegate dealloc]; + [admobAppDelegate dealloc]; } void SetPrivacySettings(bool enable_rdp) { @@ -530,4 +549,24 @@ - (void)bannerViewDidRecordClick:(GADBannerView *)bannerView { @end +@implementation AdMobAppDelegate + +- (instancetype)init { + self = [super init]; + if (self) { + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil]; + } + return self; +} + +- (void) UpdatePosition { + dmAdmob::UpdatePosition(); +} + +- (void)orientationDidChange:(NSNotification *)notification { + [self performSelector:@selector(UpdatePosition) withObject:nil afterDelay:0.1]; +} + +@end + #endif diff --git a/extension-admob/src/admob_private.h b/extension-admob/src/admob_private.h index 0b8be39..3bd88ea 100644 --- a/extension-admob/src/admob_private.h +++ b/extension-admob/src/admob_private.h @@ -43,6 +43,7 @@ enum MaxAdRating }; void Initialize_Ext(); +void Finalize_Ext(); void Initialize(const char* defoldUserAgent); void LoadInterstitial(const char* unitId); diff --git a/main/ads.gui_script b/main/ads.gui_script index 1da2e80..0a8718d 100644 --- a/main/ads.gui_script +++ b/main/ads.gui_script @@ -278,7 +278,7 @@ function on_input(self, action_id, action) admob.show_rewarded() elseif self.ad_type == admob.MSG_BANNER then if self.is_banner_shown then - print("admob.show_banner(admob."..self.banner_positions[self.show_pos]..") --("..self.show_pos..")") + print("admob.show_banner( admob."..self.banner_positions[self.show_pos]..") --("..self.show_pos..")") admob.show_banner(admob[self.banner_positions[self.show_pos]]) self.show_pos = self.show_pos + 1 if self.show_pos > #self.banner_positions then