From 16f8e5af926aaabb3f36724cfd7530d9f35e9912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Ritzl?= Date: Tue, 2 Jul 2024 11:52:00 +0200 Subject: [PATCH] Show app open on first load on iOS --- extension-admob/src/admob.cpp | 2 +- extension-admob/src/admob_android.cpp | 14 ++++++++++++-- extension-admob/src/admob_ios.mm | 23 ++++++++++++++++------- extension-admob/src/admob_private.h | 2 +- 4 files changed, 30 insertions(+), 11 deletions(-) diff --git a/extension-admob/src/admob.cpp b/extension-admob/src/admob.cpp index f2c3fb3..6b3f5f4 100644 --- a/extension-admob/src/admob.cpp +++ b/extension-admob/src/admob.cpp @@ -36,7 +36,7 @@ static int Lua_LoadAppOpen(lua_State* L) return DM_LUA_ERROR("Expected string, got %s. Wrong type for App Open Ad UnitId variable '%s'.", luaL_typename(L, 1), lua_tostring(L, 1)); } const char* unitId_lua = luaL_checkstring(L, 1); - LoadAppOpen(unitId_lua); + LoadAppOpen(unitId_lua, false); return 0; } diff --git a/extension-admob/src/admob_android.cpp b/extension-admob/src/admob_android.cpp index de2c2ab..ffaa390 100644 --- a/extension-admob/src/admob_android.cpp +++ b/extension-admob/src/admob_android.cpp @@ -83,6 +83,16 @@ static void CallVoidMethodCharInt(jobject instance, jmethodID method, const char env->DeleteLocalRef(jstr); } +static void CallVoidMethodCharBoolean(jobject instance, jmethodID method, const char* cstr, bool cbool) +{ + dmAndroid::ThreadAttacher threadAttacher; + JNIEnv* env = threadAttacher.GetEnv(); + + jstring jstr = env->NewStringUTF(cstr); + env->CallVoidMethod(instance, method, jstr, cbool); + env->DeleteLocalRef(jstr); +} + static void CallVoidMethodInt(jobject instance, jmethodID method, int cint) { dmAndroid::ThreadAttacher threadAttacher; @@ -153,9 +163,9 @@ void Initialize() CallVoidMethod(g_admob.m_AdmobJNI, g_admob.m_Initialize); } -void LoadAppOpen(const char* unitId) +void LoadAppOpen(const char* unitId, bool showImmediately) { - CallVoidMethodChar(g_admob.m_AdmobJNI, g_admob.m_LoadAppOpen, unitId); + CallVoidMethodCharBoolean(g_admob.m_AdmobJNI, g_admob.m_LoadAppOpen, unitId, showImmediately); } void ShowAppOpen() diff --git a/extension-admob/src/admob_ios.mm b/extension-admob/src/admob_ios.mm index 0a894e0..7954586 100755 --- a/extension-admob/src/admob_ios.mm +++ b/extension-admob/src/admob_ios.mm @@ -129,7 +129,7 @@ void SetAppOpenAd(GADAppOpenAd *newAd) { appOpenAd = newAd; } - void LoadAppOpen(const char* unitId) { + void LoadAppOpen(const char* unitId, bool showImmediately) { // Do not load ad if one is already loading. if (isLoadingAppOpenAd) { NSLog(@"Already loading app open ad."); @@ -139,6 +139,9 @@ void LoadAppOpen(const char* unitId) { // Do not load ad if there is an unused ad. if (IsAppOpenLoaded()) { SendSimpleMessage(MSG_APPOPEN, EVENT_LOADED); + if (showImmediately) { + ShowAppOpen(); + } return; } @@ -158,7 +161,10 @@ void LoadAppOpen(const char* unitId) { } SetAppOpenAd(ad); SendSimpleMessage(MSG_APPOPEN, EVENT_LOADED); - }]; + if (showImmediately) { + ShowAppOpen(); + } + }]; } bool IsAppOpenLoaded() { @@ -173,7 +179,7 @@ void ShowAppOpen() { } if (!IsAppOpenLoaded()) { - LoadAppOpen(appOpenAdId); + LoadAppOpen(appOpenAdId, false); return; } isShowingAppOpenAd = true; @@ -542,7 +548,9 @@ void Initialize_Ext(dmExtension::Params* params, const char* defoldUserAgent) { admobAppDelegate = [[AdMobAppDelegate alloc] init]; appOpenAdId = (char*)dmConfigFile::GetString(params->m_ConfigFile, "admob.app_open_ios", 0); - LoadAppOpen(appOpenAdId); + if (appOpenAdId) { + LoadAppOpen(appOpenAdId, true); + } dmExtension::RegisteriOSUIApplicationDelegate(admobAppDelegate); dmExtension::RegisteriOSUIApplicationDelegate(admobExtAppOpenAdDelegate); @@ -628,14 +636,14 @@ - (void)adWillPresentFullScreenContent:(nonnull id)ad - (void)adDidDismissFullScreenContent:(nonnull id)ad { dmAdmob::isShowingAppOpenAd = false; dmAdmob::SetAppOpenAd(nil); - dmAdmob::LoadAppOpen(dmAdmob::appOpenAdId); + dmAdmob::LoadAppOpen(dmAdmob::appOpenAdId, false); dmAdmob::SendSimpleMessage(dmAdmob::MSG_APPOPEN, dmAdmob::EVENT_CLOSED); } - (void)ad:(nonnull id)ad didFailToPresentFullScreenContentWithError:(nonnull NSError *)error { dmAdmob::isShowingAppOpenAd = false; dmAdmob::SetAppOpenAd(nil); - dmAdmob::LoadAppOpen(dmAdmob::appOpenAdId); + dmAdmob::LoadAppOpen(dmAdmob::appOpenAdId, false); dmAdmob::SendSimpleMessage(dmAdmob::MSG_APPOPEN, dmAdmob::EVENT_FAILED_TO_SHOW, @"code", [error code], @"error", [NSString stringWithFormat:@"Error domain: \"%@\". %@", [error domain], [error localizedDescription]]); } @@ -790,7 +798,8 @@ - (void)orientationDidChange:(NSNotification *)notification { - (void) applicationDidBecomeActive:(UIApplication *)application { NSLog(@"applicationDidBecomeActive."); - dmAdmob::ShowAppOpen(); + //dmAdmob::ShowAppOpen(); + dmAdmob::LoadAppOpen(dmAdmob::appOpenAdId, true); } - (void)dealloc { diff --git a/extension-admob/src/admob_private.h b/extension-admob/src/admob_private.h index 7c59f60..68d15e3 100644 --- a/extension-admob/src/admob_private.h +++ b/extension-admob/src/admob_private.h @@ -48,7 +48,7 @@ void Initialize_Ext(dmExtension::Params* params, const char* defoldUserAgent); void Finalize_Ext(); void Initialize(); -void LoadAppOpen(const char* unitId); +void LoadAppOpen(const char* unitId, bool showImmediately); void ShowAppOpen(); void LoadInterstitial(const char* unitId); void ShowInterstitial();