iOS如何添加谷歌广告

在iOS平台上集成谷歌广告,主要是为了在应用中展示广告,增加收入或推广产品。这个过程涉及到Google AdMob,它是Google提供的一个广告网络平台,适用于移动应用程序。以下将详细讲解如何在iOS项目中添加并配置谷歌广告。你需要在Google AdMob官网上创建一个账户,如果没有的话。注册完成后,登录AdMob账号,创建一个新的应用并输入你的iOS应用的基本信息,如应用名称、平台(iOS)、包名等。这将生成一个唯一的应用ID,后面会用到。接下来,你需要在Xcode中设置你的iOS项目。确保你的项目已经配置了CocoaPods,因为我们将通过CocoaPods来引入Google-Mobile-Ads-SDK。如果没有,先安装CocoaPods并添加到你的项目中。打开终端,导航到你的项目目录,运行以下命令: ``` pod init ```编辑`Podfile`,添加以下行: ```ruby pod 'Google-Mobile-Ads-SDK' ```然后运行: ``` pod install ```等待安装完成后,关闭Xcode,重新打开使用`.xcworkspace`文件。现在,Google Mobile Ads SDK已经被引入到你的项目中。接下来是代码实现。在你的Swift或Objective-C文件中,导入必要的库: Swift: ```swift import GoogleMobileAds ``` Objective-C: ```objc #import ```为了显示广告,你需要创建一个广告单元ID。在AdMob控制台上,为你的应用创建一个新的Banner广告或Interstitial广告(全屏广告),并记录下生成的广告单元ID。对于Banner广告,可以在你需要展示广告的地方添加以下代码(Swift): ```swift import UIKit import GoogleMobileAds class ViewController: UIViewController, GADBannerViewDelegate { var bannerView: GADBannerView! override func viewDidLoad() { super.viewDidLoad() let adUnitID = "你的Banner广告单元ID" bannerView = GADBannerView(adSize: kGADAdSizeBanner) bannerView.adUnitID = adUnitID bannerView.rootViewController = self bannerView.delegate = self view.addSubview(bannerView) let request = GADRequest() bannerView.load(request) } //广告加载成功的代理方法func adViewDidReceiveAd(_ adView: GADBannerView) { print("广告加载成功") } //广告加载失败的代理方法func adView(_ adView: GADBannerView, didFailToReceiveAdWithError error: GADRequestError) { print("广告加载失败: (error.localizedDescription)") } } ``` Objective-C: ```objc #import "ViewController.h" #import @interface ViewController () @property (nonatomic, strong) GADBannerView *bannerView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *adUnitID = @"你的Banner广告单元ID"; self.bannerView = [[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner]; self.bannerView.adUnitID = adUnitID; self.bannerView.rootViewController = self; self.bannerView.delegate = self; [self.view addSubview:self.bannerView]; GADRequest *request = [GADRequest request]; [self.bannerView load:request]; } //广告加载成功的代理方法- (void)adViewDidReceiveAd:(GADBannerView *)adView { NSLog(@"广告加载成功"); } //广告加载失败的代理方法- (void)adView:(GADBannerView *)adView didFailToReceiveAdWithError:(GADRequestError *)error { NSLog(@"广告加载失败: %@", error.localizedDescription); } @end ```对于Interstitial广告(全屏广告),可以在合适的位置加载和展示广告: Swift: ```swift import GoogleMobileAds class ViewController: UIViewController, GADInterstitialDelegate { var interstitial: GADInterstitial! override func viewDidLoad() { super.viewDidLoad() let adUnitID = "你的Interstitial广告单元ID" interstitial = GADInterstitial(adUnitID: adUnitID) interstitial.delegate = self loadInterstitial() } func loadInterstitial() { let request = GADRequest() interstitial.load(request) } //广告加载成功的代理方法func interstitialDidReceiveAd(_ interstitial: GADInterstitial) { if interstitial.isReady { interstitial.present(fromRootViewController: self) } } //广告加载失败的代理方法func interstitial(_ interstitial: GADInterstitial, didFailToReceiveAdWithError error: GADRequestError) { print("Interstitial广告加载失败: (error.localizedDescription)") } } ``` Objective-C: ```objc #import "ViewController.h" #import @interface ViewController () @property (nonatomic, strong) GADInterstitial *interstitial; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSString *adUnitID = @"你的Interstitial广告单元ID"; self.interstitial = [[GADInterstitial alloc] initWithAdUnitID:adUnitID]; self.interstitial.delegate = self; [self loadInterstitial]; } - (void)loadInterstitial { GADRequest *request = [GADRequest request]; [self.interstitial load:request]; } //广告加载成功的代理方法- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial { if (interstitial.isReady) { [interstitial presentFromRootViewController:self]; } } //广告加载失败的代理方法- (void)interstitial:(GADInterstitial *)interstitial didFailToReceiveAdWithError:(GADRequestError *)error { NSLog(@"Interstitial广告加载失败: %@", error.localizedDescription); } @end ```以上就是将谷歌广告集成到iOS应用的基本步骤。在实际开发中,还需要考虑广告的显示时机、用户体验以及合规性问题,如避免频繁展示广告,确保用户能正常使用应用。同时,记得遵守Google AdMob的政策,防止被封号。在测试过程中,可以使用测试设备ID和测试广告单元ID,以便在未发布应用时也能查看广告效果。 TheNewadmobDemo-1可能是提供了一个示例项目或者教程,你可以参考其中的代码和配置来进一步理解和实践。在实际操作时,请根据自己的需求和应用结构进行相应的调整。
zip
TheNewadmobDemo-1.zip 预估大小:33个文件
folder
TheNewadmobDemo-1 文件夹
folder
admobDemo 文件夹
file
README.txt 368B
file
main.m 332B
file
GADAdNetworkExtras.h 447B
file
admobDemo-Info.plist 1KB
file
GADAdSize.h 3KB
folder
Add-ons 文件夹
folder
Search 文件夹
folder
DoubleClick 文件夹
folder
Mediation 文件夹
folder
GoogleAnalyticsiOS_2.0beta3 文件夹
folder
Examples 文件夹
folder
CuteAnimals 文件夹
folder
Images 文件夹
folder
Library 文件夹
folder
Documentation 文件夹
folder
css 文件夹
folder
img 文件夹
folder
Classes 文件夹
folder
Protocols 文件夹
file
AppDelegate.h 360B
file
AppDelegate.m 2KB
file
GADInterstitialDelegate.h 2KB
file
admobDemo-Prefix.pch 466B
file
.DS_Store 6KB
file
GADInterstitial.h 3KB
file
GADBannerView.h 4KB
file
GADRequest.h 4KB
file
ViewController.h 311B
file
GADBannerViewDelegate.h 3KB
file
GADAdMobExtras.h 383B
file
libGoogleAdMobAds.a 13.97MB
file
GADRequestError.h 2KB
file
ViewController.m 2KB
folder
en.lproj 文件夹
file
ViewController.xib 5KB
file
InfoPlist.strings 45B
folder
admobDemo.xcodeproj 文件夹
folder
project.xcworkspace 文件夹
file
contents.xcworkspacedata 154B
folder
xcuserdata 文件夹
folder
yanghiroshiqiang.xcuserdatad 文件夹
file
UserInterfaceState.xcuserstate 35KB
folder
sxt.xcuserdatad 文件夹
file
UserInterfaceState.xcuserstate 12KB
folder
yanghongqiang.xcuserdatad 文件夹
file
UserInterfaceState.xcuserstate 23KB
file
project.pbxproj 17KB
folder
xcuserdata 文件夹
folder
yanghiroshiqiang.xcuserdatad 文件夹
folder
xcschemes 文件夹
file
xcschememanagement.plist 481B
file
admobDemo.xcscheme 3KB
folder
sxt.xcuserdatad 文件夹
folder
xcschemes 文件夹
file
xcschememanagement.plist 481B
file
admobDemo.xcscheme 3KB
folder
yanghongqiang.xcuserdatad 文件夹
folder
xcschemes 文件夹
file
xcschememanagement.plist 481B
file
admobDemo.xcscheme 2KB
file
.DS_Store 6KB
zip 文件大小:5.33MB