1、广告不是必须的,如果存在,就必须设计合理。iOS并没有禁止添加广告,但使用某些广告可能会导致审核无法通过。这里介绍iAd,adMob。
2、关于iAd,直接导入iAd库就可以了,iAd广告很多,横幅、插屏、视频广告都有,但我这里测试只有横幅是正常的,其他都没加载出来,可能是网速原因,也可能是本人过菜。
所以,iAd只采用横幅广告。(从官网下载的插屏广告demo也时常不能显示出广告来,故放弃之)
(1)添加iAd.framework
(2)在AppController.h,
#import <iAd/iAd.h>
@interface AppController : NSObject <UIAccelerometerDelegate,...,ADBannerViewDelegate>
(3)在@end前添加一个属性
@property (nonatomic,strong)ADBannerView *iadBanner;
(4)实现某些方法
-(void)bannerViewWillLoadAd:(ADBannerView *)banner
{
}
-(void)bannerViewDidLoadAd:(ADBannerView *)banner
{
}
-(void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
}
-(void)showIADBanner
{
self.iadBanner=[[ADBannerView alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];//
self.iadBanner.requiredContentSizeIdentifiers=[NSSet setWithObjects:ADBannerContentSizeIdentifierPortrait, nil];
self.iadBanner.currentContentSizeIdentifier=ADBannerContentSizeIdentifierPortrait;
self.iadBanner.delegate=self;
[viewController.view addSubview:self.iadBanner];
}
(5)在
didFinishLaunchingWithOptions函数的return YES前调用[self showIADBanner];
3、使用adMob的横幅广告
(1)下载广告库,下载后目录如下:
(2)将GoogleMobileAds.framework拖进项目中,另外,再添加一些其他库,具体要哪些,不清楚。给个参考,可以用的。如下
(3)同样的,#import <GoogleMobileAds/GADBannerView.h>
在AppController.h添加协议<GADBannerViewDelegate>
添加属性
@property(nonatomic,strong)GADBannerView *gadBanner;
(4)在AppController.mm中添加方法
- (GADRequest*)createGADRequest
{
GADRequest*request=[GADRequest request];
//下面是开启测试模式,上线后注释掉
request.testDevices=@[@"c85d7e50958ce70a91506be9c576882a",];//后面可以添加更多设备
return request;
}
- (void)adViewDidReceiveAd:(GADBannerView *)bannerView
{
NSLog(@"ads");
}
- (void)adView:(GADBannerView *)bannerView didFailToReceiveAdWithError:(GADRequestError *)error
{
NSLog(@"Fail ads:%@",[error localizedFailureReason]);
}
-(void)showAdmobBanner
{
CGPoint origin=CGPointMake(0.0, viewController.view.frame.size.height-kGADAdSizeBanner.size.height);
self.gadBanner=[[GADBannerView alloc] initWithAdSize:kGADAdSizeBanner origin:origin];
self.gadBanner.adUnitID=@"ca-app-pub-6686321446344812/4987791289";
self.gadBanner.delegate=self;
self.gadBanner.rootViewController=viewController;
[viewController.view addSubview:self.gadBanner];
[self.gadBanner loadRequest:[self createGADRequest]];
}
(5)直接调用
[self
showAdmobBanner
];
4、使用adMob插屏广告
(1)AppController.h,
#import <GoogleMobileAds/GADInterstitial.h>
添加< GADInterstitialDelegate >,添加属性
@property(nonatomic,retain)GADInterstitial *gadInterstitial;
(2)实现方法-(void)interstitial:(GADInterstitial *)ad didFailToReceiveAdWithError:(GADRequestError *)error
{
NSLog(@"Fail ads:%@",[error localizedFailureReason]);
}
-(void)interstitialDidReceiveAd:(GADInterstitial *)ad
{
[self.gadInterstitial presentFromRootViewController:viewController];
}
-(void)interstitialWillDismissScreen:(GADInterstitial *)ad
{
}
-(void)showAdmobInterstitial
{
if(self.gadInterstitial.isReady)
{
[self.gadInterstitial presentFromRootViewController:viewController];
}else{
self.gadInterstitial=[[[GADInterstitial alloc]initWithAdUnitID:@"ca-app-pub-6686321446344812/3511058088"]autorelease];
self.gadInterstitial.delegate=self;
[self.gadInterstitial loadRequest:[self createGADRequest]];
}
}
(3)直接调用[self showAdmobInterstitial];
效果如下