[ios][admob] Implement Banner

This commit is contained in:
Elliot Hesp 2017-06-16 15:20:34 +01:00
parent 538b80a3f2
commit af5bf655fe
7 changed files with 141 additions and 6 deletions

View File

@ -290,8 +290,6 @@ Returns an object of config data related to the loaded advert:
hasVideoContent: boolean,
width: number,
height: number,
top: number,
left: number,
}
```

View File

@ -0,0 +1,27 @@
#import <React/RCTComponent.h>
#import <React/RCTBridgeModule.h>
#if __has_include(<GoogleMobileAds/GADMobileAds.h>)
#import "GoogleMobileAds/GADBannerView.h"
@interface BannerComponent : UIView <GADBannerViewDelegate>
@property GADBannerView *banner;
@property (nonatomic, copy) NSString *size;
@property (nonatomic, copy) NSString *unitId;
@property (nonatomic, copy) NSDictionary *request;
@property (nonatomic, copy) RCTBubblingEventBlock onBannerEvent;
- (void)requestAd;
@end
#else
@interface RNFirebaseAdMobBanner : NSObject <RCTBridgeModule> {
}
@end
#endif

View File

@ -0,0 +1,80 @@
#import "BannerComponent.h"
#import "RNFirebaseAdMob.h"
#import "React/UIView+React.h"
@implementation BannerComponent
- (void)initBanner:(GADAdSize)adSize {
_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) {
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;
[_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];
}
@end

View File

@ -0,0 +1,5 @@
#import <React/RCTViewManager.h>
@interface RNFirebaseAdMobBannerManager : RCTViewManager
@end

View File

@ -0,0 +1,27 @@
#import "RNFirebaseAdMobBannerManager.h"
#import "BannerComponent.h"
@implementation RNFirebaseAdMobBannerManager
RCT_EXPORT_MODULE();
@synthesize bridge = _bridge;
- (UIView *)view
{
return [[BannerComponent alloc] init];
}
- (dispatch_queue_t)methodQueue
{
return dispatch_get_main_queue();
}
RCT_EXPORT_VIEW_PROPERTY(size, NSString);
RCT_EXPORT_VIEW_PROPERTY(unitId, NSString);
RCT_EXPORT_VIEW_PROPERTY(request, NSDictionary);
RCT_EXPORT_VIEW_PROPERTY(onBannerEvent, RCTBubblingEventBlock);
@end

View File

@ -49,10 +49,9 @@ class AdMobComponent extends React.Component {
this.nativeView = requireNativeComponent(props.class, AdMobComponent, {
nativeOnly: {
onSizeChange: true,
onBannerEvent: true,
},
});
}
/**
@ -91,7 +90,7 @@ class AdMobComponent extends React.Component {
<this.nativeView
{...this.props}
style={[this.props.style, { ...this.state }]}
bannerEvent={this.onBannerEvent}
onBannerEvent={this.onBannerEvent}
/>
);
}

View File

@ -1,5 +1,4 @@
import React from 'react';
import { requireNativeComponent } from 'react-native';
import AdMobComponent from './AdMobComponent';
function Banner({ ...props }) {