Skip to content
This repository has been archived by the owner on Apr 27, 2022. It is now read-only.

Commit

Permalink
fix: fix AppOpenAd showed after disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
wjaykim committed Nov 3, 2021
1 parent a371d3f commit 237ba9d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
7 changes: 7 additions & 0 deletions ios/Ads/RNAdMobAppOpen.swift
Expand Up @@ -33,6 +33,13 @@ class RNAdMobAppOpen: RNAdMobFullScreenAd<GADAppOpenAd> {
self.options = options
}

override func destroyAd(_ requestId: Int) {
super.destroyAd(requestId)
self.requestId = nil
unitId = nil
options = nil
}

override func load(unitId: String, adRequest: GAMRequest, adLoadDelegate: RNAdMobFullScreenAd<GADAppOpenAd>.AdLoadDelegate, fullScreenContentDelegate: RNAdMobFullScreenAd<GADAppOpenAd>.FullScreenContentDelegate) {
GADAppOpenAd.load(withAdUnitID: unitId, request: adRequest, orientation: UIInterfaceOrientation.portrait) {
(ad, error) in
Expand Down
28 changes: 18 additions & 10 deletions src/ads/AppOpenAd.ts
Expand Up @@ -25,10 +25,9 @@ export default class AppOpenAd extends FullScreenAd<
options: AppOpenAdOptions
) {
super('AppOpen', requestId, unitId, options);
this.load();
}

static sharedInstance: AppOpenAd;
private static sharedInstance: AppOpenAd | null = null;

private static checkInstance() {
if (!this.sharedInstance) {
Expand All @@ -37,16 +36,17 @@ export default class AppOpenAd extends FullScreenAd<
}

/**
* Creates a AppOpenAd instance. Ad is loaded automatically after created and dismissed.
* Creates a AppOpenAd instance. Ad is loaded automatically after created and after dismissed.
* @param unitId The Ad Unit ID for the App Open Ad. You can find this on your Google AdMob dashboard.
* @param options Optional AppOpenAdOptions object.
*/
static createAd(unitId: string, options?: AppOpenAdOptions): AppOpenAd {
static createAd(unitId: string, options?: AppOpenAdOptions) {
const _options = { ...defaultOptions, ...options };

if (this.sharedInstance) {
if (this.sharedInstance.unitId === unitId) {
this.sharedInstance.options = _options;
this.sharedInstance.load();
return this.sharedInstance;
}
this.sharedInstance.destroy();
Expand All @@ -57,29 +57,37 @@ export default class AppOpenAd extends FullScreenAd<
return this.sharedInstance;
}

/**
* Returns loaded App Open Ad instance.
*/
static getAd() {
return this.sharedInstance;
}

/**
* Loads a new App Open ad.
* @param requestOptions Optional RequestOptions used to load the ad.
*/
static load(requestOptions?: RequestOptions) {
this.checkInstance();
return this.sharedInstance.load(requestOptions);
return this.sharedInstance!.load(requestOptions);
}

/**
* Shows loaded App Open Ad.
*/
static show() {
this.checkInstance();
return this.sharedInstance.show();
return this.sharedInstance!.show();
}

/**
* Destroys the App Open Ad.
*/
static destroy() {
this.checkInstance();
this.sharedInstance.destroy();
this.sharedInstance!.destroy();
this.sharedInstance = null;
}

/**
Expand All @@ -88,7 +96,7 @@ export default class AppOpenAd extends FullScreenAd<
*/
static setRequestOptions(requestOptions?: RequestOptions) {
this.checkInstance();
this.sharedInstance.setRequestOptions(requestOptions);
return this.sharedInstance!.setRequestOptions(requestOptions);
}

/**
Expand All @@ -98,14 +106,14 @@ export default class AppOpenAd extends FullScreenAd<
*/
static addEventListener(event: AppOpenAdEvent, handler: HandlerType) {
this.checkInstance();
return this.sharedInstance.addEventListener(event, handler);
return this.sharedInstance!.addEventListener(event, handler);
}

/**
* Removes all registered event handlers for this ad.
*/
static removeAllListeners() {
this.checkInstance();
return this.sharedInstance.removeAllListeners();
return this.sharedInstance!.removeAllListeners();
}
}

0 comments on commit 237ba9d

Please sign in to comment.