返回介绍

将 SDK 添加到你的 Xcode 项目

发布于 2025-04-26 18:09:31 字数 4363 浏览 0 评论 0 收藏

创建 Podfile 文件。

在与 BannerExample.xcodeproj 文件相同的目录下,新建一个名为 Podfile 的文件,内容如下:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '7.0'
pod 'Google-Mobile-Ads-SDK', '~> 7.0'

你的项目会引入一个 Pods 项目到 Pods/Google-Mobile-Ads-SDK 目录中。

你需要在 Build Phases 中链接 Google Admob SDK。

使用下面的命令安装依赖:

pod install

要在你的应用中显示广告,需要将 RCTRootView 嵌入到你的自定义 ViewController 中并命名为 MyViewController:

MyViewController.h



#import <UIKit/UIKit.h>
@import GoogleMobileAds;
#import "RCTRootView.h"
@interface MyViewController : UIViewController<
    GADBannerViewDelegate, RCTBridgeModule, GADInterstitialDelegate>
@property (strong, nonatomic) GADBannerView *bannerView;
@property(nonatomic, strong) GADInterstitial *interstitial;
@property (weak, nonatomic) NSDictionary *launchOptions;
@end

为了初始化 RCTRootView 组件,我们可以创建 MyViewController.h 文件,需要使用一些属性来显示 banner、interstitial ads 和一个可选的 launchOptions。

MyViewController.m



#import "MyViewController.h"
RCT_EXPORT_MODULE();
@implementation MyViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.interstitial = [self createAndLoadInterstitial];
    CGRect viewRect = [[UIScreen mainScreen] bounds];
    self.bannerView = [[GADBannerView alloc] initWithAdSize:
        kGADAdSizeBanner];
    self.bannerView.delegate = self;
    self.bannerView.rootViewController = self;
    self.bannerView.adUnitID =@"ca-app-pub
        -1026571410838165/1538784763";
    GADRequest *request = [GADRequest request];
    request.testDevices = @[@"cbdadd858f8df9137ecc259559d3f613"];
    // 这是

 Eric 的

 iPod Touch 上的信息



    [self.bannerView loadRequest:request];
    NSURL *jsCodeLocation;
    jsCodeLocation = [[NSBundle mainBundle]
            URLForResource:@"main"
              withExtension:@"jsbundle"];
    RCTRootView *rootView = [[RCTRootView alloc]
            initWithBundleURL:jsCodeLocation
                    moduleName:@"AdmobTest"
                launchOptions:self.launchOptions];
    rootView.frame = CGRectMake(0, 50, viewRect.size.width,
                                        viewRect.size.height - 50);
    [self.view addSubview:rootView];
}

在 viewDidLoad 方法中,我们初始化 bannerView 实例和 interstitial 实例。

注意事项:

- 我们使用 RCT_EXPORT_MODULE 方法来将模块导出为 React Native 模块,从而可以在视图中使用。视图可以调用导出的方法。

- bannerView 的 rootViewControllerntroller 是 MyViewController 的实例。

- 我们使用 request.testDevices 数组创建一个测试 Request 的实例。

- 如果 rootView 中 frame 的属性 y 不等于 banner 的大小,banner ads 将覆盖我们的 rootView。

- 最后,我们需要把 RCTRootView 添加到 MyViewController 的实例 view 中。

每当将 bannerView 添加到 view 中时,adViewDidReceiveAd 方法就会准备好显示。

- (void)adViewDidReceiveAd:(GADBannerView *)adView {
    NSLog(@"adViewDidReceiveAd");
    [self.view addSubview:adView];
}

- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

接下来的方法是 displayInterstitial,这是从我们的模块中导出的方法,以创建一个新的 Interstial 请求。记得把它放在单独的线程中,这样就不会挂起我们的应用程序。

- (void)displayInterstitial {
    RCT_EXPORT_METHOD();
    NSLog(@"display Interstial");

    dispatch_async(dispatch_get_main_queue(), ^{
        self.interstitial = [self createAndLoadInterstitial];
    });
}

- (GADInterstitial *)createAndLoadInterstitial {
    GADInterstitial *interstitial = [[GADInterstitial alloc] init];
    interstitial.adUnitID = @"ca-app-pub
        -1026571410838165/6761580765";
    interstitial.delegate = self;
    GADRequest *request = [GADRequest request];
    request.testDevices = @[@"cbdadd858f8df9137ecc259559d3f613"];
    // 这是

 Eric 的

 iPod Touch 上的信息



    [interstitial loadRequest:request];
    return interstitial;
}

- (void)interstitialDidReceiveAd:(GADInterstitial *)ad
{
    NSLog(@"interstitial received");
    UIViewController *viewController = [[[[UIApplication
        sharedApplication] delegate] window]      rootViewController
        ];
    if ([self.interstitial isReady]) {
         [self.interstitial presentFromRootViewController:
            viewController];
    }
}

- (void)interstitialDidDismissScreen:(GADInterstitial *)
    interstitial {

}

@end

要调用并显示 InterstitialAd,只需要在 React Native View 中调用 NativeModules.MyViewController. displayInterstitial 方法。

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。