Skip to content

Commit

Permalink
Update admob_ios.mm
Browse files Browse the repository at this point in the history
  • Loading branch information
britzl committed Jul 1, 2024
1 parent 3b8ec2b commit 7d58d04
Showing 1 changed file with 53 additions and 21 deletions.
74 changes: 53 additions & 21 deletions extension-admob/src/admob_ios.mm
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,49 @@ void Initialize() {
static bool isLoadingAppOpenAd;
static bool isShowingAppOpenAd;

void SetAppOpenAd(GADAppOpenAd *newAd) {
if (appOpenAd == newAd) {
return;
}
if (newAd != nil) {
[newAd retain];
}
if (appOpenAd != nil) {
[appOpenAd release];
}
appOpenAd = newAd;
}

void LoadAppOpen(const char* unitId) {
// Do not load ad if there is an unused ad or one is already loading.
if (isLoadingAppOpenAd || IsAppOpenLoaded()) {
// Do not load ad if one is already loading.
if (isLoadingAppOpenAd) {
NSLog(@"Already loading app open ad.");
return;
}

// Do not load ad if there is an unused ad.
if (IsAppOpenLoaded()) {
SendSimpleMessage(MSG_APPOPEN, EVENT_LOADED);
return;
}

appOpenAdId = (char*)unitId;
isLoadingAppOpenAd = true;

[GADAppOpenAd loadWithAdUnitID:[NSString stringWithUTF8String: unitId]
request:[GADRequest request]
completionHandler:^(GADAppOpenAd *_Nullable ad, NSError *_Nullable error) {
isLoadingAppOpenAd = false;
if (error) {
NSLog(@"Failed to load app open ad: %@", error);
SendSimpleMessage(MSG_APPOPEN, EVENT_FAILED_TO_LOAD, @"code", [error code],
@"error", [NSString stringWithFormat:@"Error domain: \"%@\". %@", [error domain], [error localizedDescription]]);
return;
}
appOpenAd = ad;
appOpenAd.fullScreenContentDelegate = admobExtAppOpenAdDelegate;
SendSimpleMessage(MSG_APPOPEN, EVENT_LOADED);
}];
[GADAppOpenAd
loadWithAdUnitID:[NSString stringWithUTF8String:unitId]
request:createGADRequest()
completionHandler:^(GADAppOpenAd *_Nullable ad, NSError *_Nullable error) {
isLoadingAppOpenAd = false;
if (error) {
SetAppOpenAd(nil);
NSLog([NSString stringWithFormat:@"Error domain: \"%@\". %@", [error domain], [error localizedDescription]]);
SendSimpleMessage(MSG_APPOPEN, EVENT_FAILED_TO_LOAD, @"code", [error code],
@"error", [NSString stringWithFormat:@"Error domain: \"%@\". %@", [error domain], [error localizedDescription]]);
return;
}
SetAppOpenAd(ad);
SendSimpleMessage(MSG_APPOPEN, EVENT_LOADED);
}];
}

bool IsAppOpenLoaded() {
Expand All @@ -152,12 +173,22 @@ void ShowAppOpen() {
}

if (!IsAppOpenLoaded()) {
NSLog(@"The app open ad is not ready yet.");
LoadAppOpen(appOpenAdId);
return;
}
isShowingAppOpenAd = true;
[appOpenAd presentFromRootViewController:nil];
NSError* error;
appOpenAd.fullScreenContentDelegate = admobExtAppOpenAdDelegate;
if ([appOpenAd canPresentFromRootViewController:uiViewController error:&error]) {
[appOpenAd presentFromRootViewController:uiViewController];
} else {
if (error) {
SendSimpleMessage(MSG_APPOPEN, EVENT_NOT_LOADED, @"code", [error code],
@"error", [NSString stringWithFormat:@"Error domain: \"%@\". %@", [error domain], [error localizedDescription]]);
} else {
SendSimpleMessage(MSG_APPOPEN, EVENT_NOT_LOADED, @"error", @"Can't present App Open Ad");
}
}
}


Expand Down Expand Up @@ -595,15 +626,15 @@ - (void)adWillPresentFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad
}

- (void)adDidDismissFullScreenContent:(nonnull id<GADFullScreenPresentingAd>)ad {
dmAdmob::appOpenAd = nil;
dmAdmob::isShowingAppOpenAd = false;
dmAdmob::SetAppOpenAd(nil);
dmAdmob::LoadAppOpen(dmAdmob::appOpenAdId);
dmAdmob::SendSimpleMessage(dmAdmob::MSG_APPOPEN, dmAdmob::EVENT_CLOSED);
}

- (void)ad:(nonnull id<GADFullScreenPresentingAd>)ad didFailToPresentFullScreenContentWithError:(nonnull NSError *)error {
dmAdmob::appOpenAd = nil;
dmAdmob::isShowingAppOpenAd = false;
dmAdmob::SetAppOpenAd(nil);
dmAdmob::LoadAppOpen(dmAdmob::appOpenAdId);
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 @@ -758,6 +789,7 @@ - (void)orientationDidChange:(NSNotification *)notification {
}

- (void) applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"applicationDidBecomeActive.");
dmAdmob::ShowAppOpen();
}

Expand Down

0 comments on commit 7d58d04

Please sign in to comment.