Skip to content

Commit

Permalink
Show app open on first load on iOS
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Jul 2, 2024
1 parent 088f1f5 commit 16f8e5a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 11 deletions.
2 changes: 1 addition & 1 deletion extension-admob/src/admob.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
14 changes: 12 additions & 2 deletions extension-admob/src/admob_android.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
Expand Down
23 changes: 16 additions & 7 deletions extension-admob/src/admob_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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.");
Expand All @@ -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;
}

Expand All @@ -158,7 +161,10 @@ void LoadAppOpen(const char* unitId) {
}
SetAppOpenAd(ad);
SendSimpleMessage(MSG_APPOPEN, EVENT_LOADED);
}];
if (showImmediately) {
ShowAppOpen();
}
}];
}

bool IsAppOpenLoaded() {
Expand All @@ -173,7 +179,7 @@ void ShowAppOpen() {
}

if (!IsAppOpenLoaded()) {
LoadAppOpen(appOpenAdId);
LoadAppOpen(appOpenAdId, false);
return;
}
isShowingAppOpenAd = true;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -628,14 +636,14 @@ - (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad
- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)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<GADFullScreenPresentingAd>)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]]);
}
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion extension-admob/src/admob_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 16f8e5a

Please sign in to comment.