2017-06-16 14:20:34 +00:00
|
|
|
#import "BannerComponent.h"
|
|
|
|
#import "RNFirebaseAdMob.h"
|
2017-08-17 16:25:51 +00:00
|
|
|
#import <React/UIView+React.h>
|
2017-06-16 14:20:34 +00:00
|
|
|
|
|
|
|
@implementation BannerComponent
|
|
|
|
|
2017-06-20 12:22:09 +00:00
|
|
|
#if __has_include(<GoogleMobileAds/GADMobileAds.h>)
|
|
|
|
|
2017-06-16 14:20:34 +00:00
|
|
|
- (void)initBanner:(GADAdSize)adSize {
|
2017-06-16 14:35:12 +00:00
|
|
|
if (_requested) {
|
|
|
|
[_banner removeFromSuperview];
|
|
|
|
}
|
2017-06-16 14:20:34 +00:00
|
|
|
_banner = [[GADBannerView alloc] initWithAdSize:adSize];
|
|
|
|
_banner.rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
|
|
|
|
_banner.delegate = self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setUnitId:(NSString *)unitId {
|
|
|
|
_unitId = unitId;
|
|
|
|
[self requestAd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setSize:(NSString *)size {
|
|
|
|
_size = size;
|
|
|
|
[self requestAd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setRequest:(NSDictionary *)request {
|
|
|
|
_request = request;
|
|
|
|
[self requestAd];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)requestAd {
|
|
|
|
if (_unitId == nil || _size == nil || _request == nil) {
|
2017-06-16 14:35:12 +00:00
|
|
|
[self setRequested:NO];
|
2017-06-16 14:20:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self initBanner:[RNFirebaseAdMob stringToAdSize:_size]];
|
|
|
|
[self addSubview:_banner];
|
|
|
|
|
|
|
|
[self sendEvent:@"onSizeChange" payload:@{
|
|
|
|
@"width": @(_banner.bounds.size.width),
|
|
|
|
@"height": @(_banner.bounds.size.height),
|
|
|
|
}];
|
|
|
|
|
|
|
|
_banner.adUnitID = _unitId;
|
2017-06-16 14:35:12 +00:00
|
|
|
[self setRequested:YES];
|
2017-06-16 14:20:34 +00:00
|
|
|
[_banner loadRequest:[RNFirebaseAdMob buildRequest:_request]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)sendEvent:(NSString *)type payload:(NSDictionary *_Nullable)payload {
|
|
|
|
self.onBannerEvent(@{
|
|
|
|
@"type": type,
|
|
|
|
@"payload": payload != nil ? payload : [NSNull null],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)adViewDidReceiveAd:(GADBannerView *)adView {
|
|
|
|
[self sendEvent:@"onAdLoaded" payload:@{
|
|
|
|
@"width": @(adView.bounds.size.width),
|
|
|
|
@"height": @(adView.bounds.size.height),
|
|
|
|
@"hasVideoContent": @NO,
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error {
|
|
|
|
[self sendEvent:@"onAdFailedToLoad" payload:[RNFirebaseAdMob errorCodeToDictionary:error]];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)adViewWillPresentScreen:(GADBannerView *)adView {
|
|
|
|
[self sendEvent:@"onAdOpened" payload:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)adViewWillDismissScreen:(GADBannerView *)adView {
|
|
|
|
// not in use
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)adViewDidDismissScreen:(GADBannerView *)adView {
|
|
|
|
[self sendEvent:@"onAdClosed" payload:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)adViewWillLeaveApplication:(GADBannerView *)adView {
|
|
|
|
[self sendEvent:@"onAdLeftApplication" payload:nil];
|
|
|
|
}
|
|
|
|
|
2017-06-20 12:22:09 +00:00
|
|
|
#endif
|
|
|
|
|
2017-08-17 16:25:51 +00:00
|
|
|
@end
|