UNPKG

@nativescript/google-mobile-ads

Version:
589 lines 23.9 kB
import { Utils } from '@nativescript/core'; import { MaxAdContentRating, AdEventType, BannerAdBase, RewardedAdEventType, unitIdProperty, BannerAdSizeBase, sizeProperty } from './common'; import { topViewController, toSerializeRequestOptions } from './utils'; export { MaxAdContentRating, AdEventType }; export * from './adsconsent'; export * from './nativead'; import { MobileAdsError } from './error'; export { MobileAdsError }; export class AdRequest { static fromNative(request) { if (request instanceof GADRequest) { const ret = new AdRequest(); ret._native = request; return ret; } return null; } get contentUrl() { return this._native.contentURL; } get keywords() { const kw = this._native.keywords; const count = kw.count; const ret = []; for (let i = 0; i < count; i++) { ret.push(kw.objectAtIndex(i)); } return ret; } get neighboringContentUrls() { const urls = this._native.keywords; const count = urls.count; const ret = []; for (let i = 0; i < count; i++) { ret.push(urls.objectAtIndex(i)); } return ret; } isTestDevice() { // todo return false; } get native() { return this._native; } get ios() { return this.native; } } export class InterstitialAd { constructor() { this._loaded = false; } static createForAdRequest(adUnitId, requestOptions) { const ad = new InterstitialAd(); ad._adUnitId = adUnitId; ad._requestOptions = requestOptions; ad._delegate = GADFullScreenContentDelegateImpl.initWithOwner(new WeakRef(ad)); return ad; } get adUnitId() { return this._adUnitId; } get loaded() { return this._loaded; } load() { const ref = new WeakRef(this); const request = toSerializeRequestOptions(this._requestOptions); GADInterstitialAd.loadWithAdUnitIDRequestCompletionHandler(this._adUnitId, request, (ad, error) => { if (error) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore ref.get()?._onAdEvent?.(AdEventType.FAILED_TO_LOAD_EVENT, MobileAdsError.fromNative(error)); } else { ad.fullScreenContentDelegate = ref.get()?._delegate; ref.get()?._setNative(ad); ref.get()?._setLoaded(true); ref.get()?._onAdEvent?.(AdEventType.LOADED); } }); this._nativeRequest = request; } _setNative(value) { this._native = value; } _setLoaded(value) { this._loaded = value; } onAdEvent(listener) { this._onAdEvent = listener; } show(showOptions) { this._native.presentFromRootViewController(topViewController()); } get request() { return AdRequest.fromNative(this._nativeRequest); } } export class RewardedInterstitialAd { constructor() { this._loaded = false; } static createForAdRequest(adUnitId, requestOptions) { const ad = new RewardedInterstitialAd(); ad._adUnitId = adUnitId; ad._requestOptions = requestOptions; ad._delegate = GADFullScreenContentDelegateImpl.initWithOwner(new WeakRef(ad)); return ad; } get adUnitId() { return this._adUnitId; } get loaded() { return this._loaded; } load() { const ref = new WeakRef(this); const request = toSerializeRequestOptions(this._requestOptions); GADRewardedInterstitialAd.loadWithAdUnitIDRequestCompletionHandler(this._adUnitId, request, (ad, error) => { if (error) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore ref.get()?._onAdEvent?.(AdEventType.FAILED_TO_LOAD_EVENT, MobileAdsError.fromNative(error)); } else { ad.fullScreenContentDelegate = ref.get()?._delegate; ref.get()?._setNative(ad); ref.get()?._setLoaded(true); ref.get()?._onAdEvent?.(AdEventType.LOADED); } }); this._nativeRequest = request; } _setNative(value) { this._native = value; } _setLoaded(value) { this._loaded = value; } onAdEvent(listener) { this._onAdEvent = listener; } show(showOptions) { this.native.presentFromRootViewControllerUserDidEarnRewardHandler(topViewController(), () => { this._onAdEvent?.(RewardedAdEventType.EARNED_REWARD, null, RewardedItem.fromNative(this.native.adReward)); }); } setServerSideVerificationOptions(options) { if (this.native) { const ssvo = GADServerSideVerificationOptions.new(); if (options.customData) { ssvo.customRewardString = options.customData; } if (options.userId) { ssvo.userIdentifier = options.userId; } this.native.serverSideVerificationOptions = ssvo; } } get native() { return this._native; } get ios() { return this.native; } get request() { return AdRequest.fromNative(this._nativeRequest); } } export class RewardedAd { constructor() { this._loaded = false; } get loaded() { return this._loaded; } static createForAdRequest(adUnitId, requestOptions) { const reward = new RewardedAd(); reward._adUnitId = adUnitId; reward._requestOptions = requestOptions; reward._delegate = GADFullScreenContentDelegateImpl.initWithOwner(new WeakRef(reward)); return reward; } get adUnitId() { return this._adUnitId; } load() { const request = toSerializeRequestOptions(this._requestOptions); const ref = new WeakRef(this); GADRewardedAd.loadWithAdUnitIDRequestCompletionHandler(this._adUnitId, request, (ad, error) => { if (error) { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore ref.get()?._onAdEvent?.(AdEventType.FAILED_TO_LOAD_EVENT, MobileAdsError.fromNative(error)); } else { ad.fullScreenContentDelegate = ref.get()?._delegate; ref.get()?._setNative(ad); ref.get()?._setLoaded(true); ref.get()?._onAdEvent?.(AdEventType.LOADED); } }); this._nativeRequest = request; } _setNative(value) { this._native = value; } _setLoaded(value) { this._loaded = value; } onAdEvent(listener) { this._onAdEvent = listener; } show(showOptions) { this.native.presentFromRootViewControllerUserDidEarnRewardHandler(topViewController(), () => { this._onAdEvent?.(RewardedAdEventType.EARNED_REWARD, null, RewardedItem.fromNative(this.native.adReward)); }); } setServerSideVerificationOptions(options) { if (this.native) { const ssvo = GADServerSideVerificationOptions.new(); if (options.customData) { ssvo.customRewardString = options.customData; } if (options.userId) { ssvo.userIdentifier = options.userId; } this.native.serverSideVerificationOptions = ssvo; } } get native() { return this._native; } get ios() { return this.native; } get request() { return AdRequest.fromNative(this._nativeRequest); } } export class RewardedItem { static fromNative(reward) { if (reward instanceof GADAdReward) { const item = new RewardedItem(); item._native = reward; return item; } return null; } get amount() { return this.native?.amount?.floatValue; } get type() { return this.native?.type; } get native() { return this._native; } get android() { return this.native; } } export class BannerAdSize extends BannerAdSizeBase { constructor(width, height, native) { super(); if (typeof width === 'number' && typeof height === 'number') { this._native = GADAdSizeFromCGSize(CGSizeMake(width, height)); } else if (native instanceof GADAdSize) { this._native = native; } else { this._native = GADAdSizeInvalid; } } static fromNative(size) { return new BannerAdSize(null, null, size); } static get BANNER() { return BannerAdSize.fromNative(NSCGA.createBanner(0 /* NSCGABannersSize.Banner */)); } static get FULL_BANNER() { return BannerAdSize.fromNative(NSCGA.createBanner(1 /* NSCGABannersSize.FullBanner */)); } static get LARGE_BANNER() { return BannerAdSize.fromNative(NSCGA.createBanner(2 /* NSCGABannersSize.LargeBanner */)); } static get LEADERBOARD() { return BannerAdSize.fromNative(NSCGA.createBanner(3 /* NSCGABannersSize.LeaderBoard */)); } static get MEDIUM_RECTANGLE() { return BannerAdSize.fromNative(NSCGA.createBanner(4 /* NSCGABannersSize.MediumRectangle */)); } static createAnchoredAdaptiveBanner(width, orientation = 'device') { let nativeOrientation = 2 /* Orientation.Device */; if (orientation === 'portrait') { nativeOrientation = 0 /* Orientation.Portrait */; } else if (orientation === 'landscape') { nativeOrientation = 1 /* Orientation.Landscape */; } return BannerAdSize.fromNative(NSCGA.createAnchoredAdaptiveBanner(width, nativeOrientation)); } static createInLineAdaptiveBanner(width, maxHeight = 0, orientation = 'device') { let nativeOrientation = 2 /* Orientation.Device */; if (orientation === 'portrait') { nativeOrientation = 0 /* Orientation.Portrait */; } else if (orientation === 'landscape') { nativeOrientation = 1 /* Orientation.Landscape */; } return BannerAdSize.fromNative(NSCGA.createInlineAdaptiveBanner(width, maxHeight, nativeOrientation)); } static get FLUID() { return BannerAdSize.fromNative(NSCGA.createBanner(5 /* NSCGABannersSize.Fluid */)); } static get WIDE_SKYSCRAPER() { return BannerAdSize.fromNative(NSCGA.createBanner(6 /* NSCGABannersSize.WideSkyScraper */)); } static get INVALID() { return BannerAdSize.fromNative(NSCGA.createBanner(7 /* NSCGABannersSize.Invalid */)); } get native() { return this._native; } get ios() { return this.native; } } export class BannerAd extends BannerAdBase { constructor() { super(...arguments); this._isLoading = false; } _setIsLoading(value) { this._isLoading = value; } isLoading() { return this._isLoading; } createNativeView() { this._native = GADBannerView.new(); this._delegate = GADBannerViewDelegateImpl.initWithOwner(new WeakRef(this)); return this._native; } initNativeView() { this._native.delegate = this._delegate; } onLoaded() { super.onLoaded(); this._native.rootViewController = topViewController(); } load(options = {}) { this._isLoading = true; const request = toSerializeRequestOptions(options); this._native?.loadRequest?.(request); this._nativeRequest = request; } [sizeProperty.setNative](value) { if (this._native) { this._native.adSize = value?.native; } } [unitIdProperty.setNative](value) { if (this._native) { this._native.adUnitID = value; } } onMeasure(widthMeasureSpec, heightMeasureSpec) { const nativeView = this.nativeView; if (nativeView) { const width = Utils.layout.getMeasureSpecSize(widthMeasureSpec); const height = Utils.layout.getMeasureSpecSize(heightMeasureSpec); this.setMeasuredDimension(width, height); } } get request() { return AdRequest.fromNative(this._nativeRequest); } } export class MobileAds { constructor() { this._requestConfiguration = {}; } static init() { return new Promise((resolve, reject) => { GADMobileAds.sharedInstance().startWithCompletionHandler((status) => { const data = {}; const keys = status.adapterStatusesByClassName.allKeys; const count = keys.count; for (let i = 0; i < count; i++) { const key = keys.objectAtIndex(i); const obj = status.adapterStatusesByClassName.objectForKey(key); data[key] = { description: obj.description, latency: obj.latency, state: obj.state, }; } resolve(data); }); }); } static getInstance() { return this._instance || (this._instance = new MobileAds()); } set requestConfiguration(requestConfiguration) { switch (requestConfiguration?.maxAdContentRating) { case MaxAdContentRating.G: GADMobileAds.sharedInstance().requestConfiguration.maxAdContentRating = GADMaxAdContentRatingGeneral; break; case MaxAdContentRating.MA: GADMobileAds.sharedInstance().requestConfiguration.maxAdContentRating = GADMaxAdContentRatingMatureAudience; break; case MaxAdContentRating.PG: GADMobileAds.sharedInstance().requestConfiguration.maxAdContentRating = GADMaxAdContentRatingParentalGuidance; break; case MaxAdContentRating.T: GADMobileAds.sharedInstance().requestConfiguration.maxAdContentRating = GADMaxAdContentRatingTeen; break; } if (typeof requestConfiguration?.tagForChildDirectedTreatment === 'boolean') { this._requestConfiguration.tagForChildDirectedTreatment = requestConfiguration.tagForChildDirectedTreatment; GADMobileAds.sharedInstance().requestConfiguration.tagForChildDirectedTreatment = Number(requestConfiguration.tagForChildDirectedTreatment); } if (typeof requestConfiguration?.tagForUnderAgeOfConsent === 'boolean') { this._requestConfiguration.tagForUnderAgeOfConsent = requestConfiguration.tagForUnderAgeOfConsent; GADMobileAds.sharedInstance().requestConfiguration.tagForUnderAgeOfConsent = Number(requestConfiguration.tagForUnderAgeOfConsent); } if (Array.isArray(requestConfiguration?.testDevices)) { GADMobileAds.sharedInstance().requestConfiguration.testDeviceIdentifiers = requestConfiguration.testDevices.map((item) => { if (item === 'EMULATOR') { if (typeof GADSimulatorID) { return GADSimulatorID; } return ''; } return item; }); } } get requestConfiguration() { const ret = Object.assign({}, ...this._requestConfiguration); const config = GADMobileAds.sharedInstance().requestConfiguration; switch (config.maxAdContentRating) { case GADMaxAdContentRatingGeneral: ret.maxAdContentRating = MaxAdContentRating.G; break; case GADMaxAdContentRatingMatureAudience: ret.maxAdContentRating = MaxAdContentRating.MA; break; case GADMaxAdContentRatingParentalGuidance: ret.maxAdContentRating = MaxAdContentRating.PG; break; case GADMaxAdContentRatingTeen: ret.maxAdContentRating = MaxAdContentRating.T; break; default: // noop break; } ret.testDevices = []; const devices = config.testDeviceIdentifiers; if (devices) { const count = devices.count; for (let i = 0; i < count; i++) { ret.testDevices.push(devices.objectAtIndex(i)); } } return ret; } setRequestConfiguration(requestConfiguration) { this.requestConfiguration = requestConfiguration; } getRequestConfiguration() { return this.requestConfiguration; } get app() { return global?.__defaultFirebaseApp; } } var GADFullScreenContentDelegateImpl = /** @class */ (function (_super) { __extends(GADFullScreenContentDelegateImpl, _super); function GADFullScreenContentDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } GADFullScreenContentDelegateImpl_1 = GADFullScreenContentDelegateImpl; GADFullScreenContentDelegateImpl.initWithOwner = function (owner) { var delegate = GADFullScreenContentDelegateImpl_1.new(); delegate._owner = owner; return delegate; }; GADFullScreenContentDelegateImpl.prototype.adDidDismissFullScreenContent = function (ad) { var _a, _b, _c, _d, _e, _f, _g; (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b._onAdEvent) === null || _c === void 0 ? void 0 : _c.call(_b, AdEventType.CLOSED, null, (_e = (_d = this._owner).get) === null || _e === void 0 ? void 0 : _e.call(_d)); (_g = (_f = this._owner) === null || _f === void 0 ? void 0 : _f.get()) === null || _g === void 0 ? void 0 : _g._setLoaded(false); }; GADFullScreenContentDelegateImpl.prototype.adDidFailToPresentFullScreenContentWithError = function (ad, error) { var _a, _b, _c, _d, _e; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b._onAdEvent) === null || _c === void 0 ? void 0 : _c.call(_b, AdEventType.FAILED_TO_SHOW_FULL_SCREEN_CONTENT, MobileAdsError.fromNative(error), (_e = (_d = this._owner).get) === null || _e === void 0 ? void 0 : _e.call(_d)); }; GADFullScreenContentDelegateImpl.prototype.adDidPresentFullScreenContent = function (ad) { var _a, _b, _c, _d, _e; (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b._onAdEvent) === null || _c === void 0 ? void 0 : _c.call(_b, AdEventType.OPENED, null, (_e = (_d = this._owner).get) === null || _e === void 0 ? void 0 : _e.call(_d)); }; GADFullScreenContentDelegateImpl.prototype.adDidRecordClick = function (ad) { var _a, _b, _c, _d, _e; (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b._onAdEvent) === null || _c === void 0 ? void 0 : _c.call(_b, AdEventType.CLICKED, null, (_e = (_d = this._owner).get) === null || _e === void 0 ? void 0 : _e.call(_d)); }; GADFullScreenContentDelegateImpl.prototype.adDidRecordImpression = function (ad) { var _a, _b, _c, _d, _e; (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b._onAdEvent) === null || _c === void 0 ? void 0 : _c.call(_b, AdEventType.IMPRESSION, null, (_e = (_d = this._owner).get) === null || _e === void 0 ? void 0 : _e.call(_d)); }; GADFullScreenContentDelegateImpl.prototype.adWillDismissFullScreenContent = function (ad) { // noop }; var GADFullScreenContentDelegateImpl_1; GADFullScreenContentDelegateImpl = GADFullScreenContentDelegateImpl_1 = __decorate([ ObjCClass(GADFullScreenContentDelegate) ], GADFullScreenContentDelegateImpl); return GADFullScreenContentDelegateImpl; }(NSObject)); var GADBannerViewDelegateImpl = /** @class */ (function (_super) { __extends(GADBannerViewDelegateImpl, _super); function GADBannerViewDelegateImpl() { return _super !== null && _super.apply(this, arguments) || this; } GADBannerViewDelegateImpl_1 = GADBannerViewDelegateImpl; GADBannerViewDelegateImpl.initWithOwner = function (owner) { var delegate = GADBannerViewDelegateImpl_1.new(); delegate._owner = owner; return delegate; }; GADBannerViewDelegateImpl.prototype.bannerViewDidDismissScreen = function (bannerView) { var _a, _b, _c, _d; (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b.notify) === null || _c === void 0 ? void 0 : _c.call(_b, { eventName: AdEventType.CLOSED, object: (_d = this._owner) === null || _d === void 0 ? void 0 : _d.get(), }); }; GADBannerViewDelegateImpl.prototype.bannerViewDidFailToReceiveAdWithError = function (bannerView, error) { var _a, _b, _c, _d; (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b.notify) === null || _c === void 0 ? void 0 : _c.call(_b, { eventName: AdEventType.FAILED_TO_LOAD_EVENT, object: (_d = this._owner) === null || _d === void 0 ? void 0 : _d.get(), // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-ignore error: MobileAdsError.fromNative(error), }); }; GADBannerViewDelegateImpl.prototype.bannerViewDidReceiveAd = function (bannerView) { var _a, _b, _c, _d, _e, _f; (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b.notify) === null || _c === void 0 ? void 0 : _c.call(_b, { eventName: AdEventType.LOADED, object: (_d = this._owner) === null || _d === void 0 ? void 0 : _d.get(), }); (_f = (_e = this._owner) === null || _e === void 0 ? void 0 : _e.get()) === null || _f === void 0 ? void 0 : _f._setIsLoading(false); }; GADBannerViewDelegateImpl.prototype.bannerViewDidRecordClick = function (bannerView) { var _a, _b, _c, _d; (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b.notify) === null || _c === void 0 ? void 0 : _c.call(_b, { eventName: AdEventType.CLICKED, object: (_d = this._owner) === null || _d === void 0 ? void 0 : _d.get(), }); }; GADBannerViewDelegateImpl.prototype.bannerViewDidRecordImpression = function (bannerView) { var _a, _b, _c, _d; (_c = (_b = (_a = this._owner) === null || _a === void 0 ? void 0 : _a.get()) === null || _b === void 0 ? void 0 : _b.notify) === null || _c === void 0 ? void 0 : _c.call(_b, { eventName: AdEventType.IMPRESSION, object: (_d = this._owner) === null || _d === void 0 ? void 0 : _d.get(), }); }; GADBannerViewDelegateImpl.prototype.bannerViewWillDismissScreen = function (bannerView) { // noop }; GADBannerViewDelegateImpl.prototype.bannerViewWillPresentScreen = function (bannerView) { // noop }; var GADBannerViewDelegateImpl_1; GADBannerViewDelegateImpl = GADBannerViewDelegateImpl_1 = __decorate([ ObjCClass(GADBannerViewDelegate) ], GADBannerViewDelegateImpl); return GADBannerViewDelegateImpl; }(NSObject)); //# sourceMappingURL=index.ios.js.map