您的位置:首页 > 移动开发 > IOS开发

iOS开发之iAd苹果广告

2014-03-03 14:44 501 查看


1. 简述。

iAd是苹果提供给开发者嵌入广告的advertising
platform。苹果通过iAd向用户推送广告,广告产生的收益跟开发者分成。

下面波波手把手教你如何通过几行简单的代码,嵌入iAd(以ADBannerView为示例)


2.下载示例项目。

去苹果官网:https://developer.apple.com/library/ios/samplecode/iAdSuite/Introduction/Intro.html

下载iAdSuite项目。


3.
iAd详细解说。

打开BasicBanner子项目,BasicBanner项目是一个Single
View Application,打开TextViewController类。


3.1初始化广告视图

TextViewController初始化时将广告视图ADBannerView也初始化,ADBannerView的delegate是TextViewController:





                                                                 【1-1】

<1>行API是iOS6开始才有的!

…没错,你猜对了ADBannerView就是将要展示给用户的广告,顾名思义banner['bænə]
n. 旗帜,横幅,
所以ADBannerView是小型“横幅”广告,用户点击ADBannerView才展示具体内容。下面来预览一下ADBannerView最终效果,满足你的好奇心:





【1-2】


3.2广告视图加到View视图体系上

言归正传,TextViewController加载完成时,将ADBannerView  Add 到 self.view上:





这行代码执行后,苹果开始推送广告。


3.3重要 delegate方法

苹果推送广告,无论 成功 or 失败 都会通知TextViewController。为什么通知TextViewController? 因为之前 ADBannerView  的delegate 设为TextViewController!下面所有delegate方法体,不贴出具体代码实现,因为我们注重的是理念。具体代码请查看示例。


3.3.1苹果推送广告成功delegate方法

 -(void)bannerViewDidLoadAd:(ADBannerView *)banner{

           //显示ADBannerView的
代码(具体代码,请看示例)

}

这时候就会看到广告:





【3-1-1】


3.3.2苹果推送广告失败delegate方法

 

- (void)bannerView:(ADBannerView *)banner
didFailToReceiveAdWithError:(NSError *)error{

      //隐藏ADBannerView的
代码(具体代码,请看示例)

}

苹果强烈建议:如果ADBannerView正在显示,苹果推送广告失败,应该隐藏它。


3.3.3点击广告视图后, 显示指定内容之前,的delegate方法:

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner
willLeaveApplication:(BOOL)willLeave{

           //做一些逻辑操作(具体代码,请看示例)

           return takeAction;

}

上面方法隐含信息量比较大,波波下面慢慢跟你解说:

(1)当用户点击ADBannerView后,willLeave
= NO代表以modal形式全屏显示ADBannerView指定的内容, 依然在App的控制范围之内;willLeave
= YES 代表App被打入冷宫(退到后台),ADBannerView指定的内容以“另一个App”的形式出现。willLeave
= NO时,开发者要规划好App,停止一些渲染、逻辑 。

(2)上面的返回值takeAction也是需要解析一下,takeAction
= YES,代表ADBannerView指定内容要显示出来;takeAction
= NO,代表ADBannerView指定内容不要显示出来。takeAction 应该一直等于 YES,除非App发生不可打断的情况。因为takeAction
= NO,广告显示不出来,会影响到开发者广告收益。


3.3.4查看完指定内容,退回到原来的 App后,马上执行的delegate方法

- (void)bannerViewActionDidFinish:(ADBannerView *)banner{

    //恢复因为用户点击ADBannerView被打断的App逻辑(具体代码,请看示例)

}

Important: If
your application was moved into the background because the willLeave parameter
was

YES,
then the application’s user interface is never covered by the banner view and your application
does

not receive a call to bannerViewActionDidFinish:.
However, if your interface was covered by the

banner view, your application could still be moved into the background later, either because the

advertisement launched another application or because the user chose to do so. In all cases, if your user

interface was covered by the banner view, it is uncovered and your delegate’s

bannerViewActionDidFinish: is
invoked before your application moves to the background. Because

the application may be moving into the background, your delegate should return quickly from its

bannerViewActionDidFinish: method.【摘自苹果开发者官网,因为如果我翻译出来后,会变味,所以我没有翻译。这些英文单词很简单,很容易看的】

原文:http://blog.sina.com.cn/s/blog_6a99c8bc0101oap0.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息