码迷,mamicode.com
首页 > 移动开发 > 详细

iOS开发——高级技术&广告功能的实现

时间:2015-06-24 22:12:16      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:

广告功能的实现

 

iPhone/iPad的程序,即使是Free的版本,也可以通过广告给我们带来收入。前提是你的程序足够吸引人,有足够的下载量。
这里,我将介绍一下程序中集成广告的方法。主要有两种广告iAd和AdMob。(还有其他多种可被植入的广告SDK,这里就不都一一介绍了)
一:iAd
 技术分享

从iOS 4开始,Apple增加了叫做 iAd 的架构,通过它我们可以在程序中提供Apple的广告服务。Apple会支付给开发者60%的广告收入。
iAd Framework中有例程,我们可以下载学习。这里,把简单的步骤说明一下 :
追加iAD Framework
1:首先,在Xcode的[Frameworks]中添加[iAd.framework]。
 技术分享

2:创建ADBannerView
广告的显示是在一个叫做ADBannerView的窗口中的。通过控制这个窗口可以控制广告的显示/隐藏。ADBannerView和一般的UIView没有什么两样,将其作为某个画面的subView,然后通过「frame」控制其显示的位置,大小。一般情况下,缺省iPhone上的话,竖屏是:横320pt, 竖50pt;横屏是:横480pt, 竖32pt。
下面的程序显示了ADBannerView的初始化过程,以父窗口的viewDidLoad中实现为例。

 1 - (void)viewDidLoad {
 2 
 3 ……【省略】……
 4 
 5     // 初始化ADBannerView
 6     ADBannerView *adView = [[[ADBannerView alloc] initWithFrame:CGRectZero] autorelease];
 7     adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
 8     // 登陆ADBannerView的delegate,这里我们设定其父窗口自己
 9     adView.delegate = self;
10     // 在父窗口下方表示
11     adView.frame = CGRectOffset(adView.frame, 0, self.view.frame.size.height - self.adView.frame.size.height);
12     // 添加到父窗口中
13     [self.view addSubview:adView];
14 }


3:接下来,我们来实现ADBannerView的delegate。这里可以实现在父窗口的UIViewController子类中,也可以单独写一个ViewController。这里面实现了ADBannerView广告的读取,错误处理,全画面表示等delegate的处理设定。下面实现在父窗口的ViewController中。

1 #import <UIKit/UIKit.h>
2 #import <iAd/iAd.h>
3 
4 @interface XXXViewController : UIViewController<UITextFieldDelegate, ADBannerViewDelegate> {
5 
6 ……【省略】……
7 }


4:如上所示,这里增加了「ADBannerViewDelegate」protocol的实现。接下来看看都有哪些delegate接口。

 1 // 广告读取过程中出现错误
 2 - (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError * )error{
 3     // 切换ADBannerView表示状态,显示→隐藏
 4     // adView.frame = CGRectOffset(adView.frame, 0, self.view.frame.size.height);
 5 }
 6 
 7 // 成功读取广告
 8 - (void)bannerViewDidLoadAd:(ADBannerView *)banner{
 9     // 切换ADBannerView表示状态,隐藏→显示
10     // adView.frame = CGRectOffset(adView.frame, 0, self.view.frame.size.height - adView.frame.size.height);
11 }
12 
13 // 用户点击广告是响应,返回值BOOL指定广告是否打开
14 // 参数willLeaveApplication是指是否用其他的程序打开该广告
15 // 一般在该函数内让当前View停止,以及准备全画面表示广告
16 - (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
17     NSLog(@"bannerViewActionShouldBegin:willLeaveApplication: is called.");
18 }
19 
20 // 全画面的广告表示完了后,调用该接口
21 // 该接口被调用之后,当前程序一般会作为后台程序运行
22 // 该接口中需要回复之前被中断的处理(如果有的话)
23 - (void)bannerViewActionDidFinish:(ADBannerView *)banner {
24     NSLog(@"bannerViewActionDidFinish: is called.");
25 }

5:上都实现之后,我们来看看iAd广告的效果。

技术分享

 

二:AdMob

AdMobAdMob是另一家移动电话广告市场,现在已被Google收购。借助AdMob, 谷歌计划将其网络搜索主导地位从电脑平台扩展向手机平台。正因为AdMob现在是Google的了,所以在Android上得到广泛的应用,不过在iPhone上也是同样可以使用的。下面就介绍一下使用方法。其实步骤很简单:在AdMob上注册用户→登陆你的程序→得到程序固有的Publisher ID→下载并在程序中组入相应代码。登陆首先注册用户
技术分享

注册并登陆之后,需要登记你准备添加广告的程序(这里,即使程序还没有开发完毕也没有关系)。点击下图marketplace→添加site/Application

技术分享
选择iPhone/iPad程序,如果是Android的话就选择Android应用程序。

技术分享
接下来填写程序的详细信息。
技术分享

其中关于程序的URL的填写,如果程序已经发布,那么填写App Store上的URL,否则随便先填一个,以后可以修改。接下来就可以得到程序的Publisher ID,其使用方法会在下面介绍。

技术分享
同时,我们可以下载最新的SDK。(其实也可以通过这里下载)

技术分享
下载并解压之后,会得到如下图的几个文件。其中README.txt有环境说明,文档及例程的下载URL。

技术分享
注意点接下来我们看看程序中怎么使用该SDK。首先我们看看例程中有哪些需要注意的。MY BANNER_UNIT_ID的设定例程中有下面这样的定义

1 (BannerExampleViewController.m)。
2 #if !defined(MY_BANNER_UNIT_ID)
3   #error "You must define MY_BANNER_UNIT_ID as your AdMob Publisher ID"
4 #endif


里就需要上面介绍的注册时得到的程序专有的Publisher ID。没有它,编译的时候将报错。类似下面的样子,我们设定一下。

1 #define MY_BANNER_UNIT_ID   @"xxxxxxxxxxxxxxx"
2 
3 #if !defined(MY_BANNER_UNIT_ID)
4   #error "You must define MY_BANNER_UNIT_ID as your AdMob Publisher ID"
5 #endif

 


另外,还有一个测试时用的属性测试的时候将 GADRequest::testing 属性置为 YES。如下For Testing的设置。

 1 // Let the runtime know which UIViewController to restore after taking
 2   // the user wherever the ad goes and add it to the view hierarchy.
 3   bannerView_.rootViewController = self;
 4   [self.view addSubview:bannerView_];
 5 
 6   // For Testing
 7   GADRequest *rq = [GADRequest request];
 8   rq.testing = YES;
 9 
10   // Initiate a generic request to load it with an ad.
11   [bannerView_ loadRequest:rq];

 


示的广告如下图。

技术分享
不过,该设定只对模拟器有效,在实际设备上运行时,仍然显示真实的广告。如下图。
技术分享

例程接下来,通过一个例程说明一下AdMob广告的添加过程。1. Xcode 4中创建一个「Tab Bar Application」新程序「AdMobTabBar」
技术分享

2. 将 AdMob SDK 放到该工程中。

技术分享
3. 添加 AdMob 所必须的 Framework( AudioToolbox, MediaPlayer, MessageUI, SystemConfiguration。)
技术分享

4. 创建GADBannerView与iAD中的ADBannerView类似,AdMob也有一个GADBannerView,用来显示广告。其创建过程如下。

 1 // Create a view of the standard size at the bottom of the screen.
 2     bannerView_ = [[GADBannerView alloc]
 3                             initWithFrame:CGRectMake(0.0,
 4                                             self.view.frame.size.height -
 5                                             TABBAR_HEIGHT,
 6                                             GAD_SIZE_320x50.width,
 7                                             GAD_SIZE_320x50.height)];
 8 
 9     // delegate の設定
10     bannerView_.delegate = self;
11 
12     // Specify the ad‘s "unit identifier." This is your AdMob Publisher ID.
13     bannerView_.adUnitID = MY_BANNER_UNIT_ID;
14 
15     // Let the runtime know which UIViewController to restore after taking
16     // the user wherever the ad goes and add it to the view hierarchy.
17     bannerView_.rootViewController = self;
18     [self.view addSubview:bannerView_];
19 
20     // For Testing
21     GADRequest *rq = [GADRequest request];
22     rq.testing = YES;
23 
24     // Initiate a generic request to load it with an ad.
25     [bannerView_ loadRequest:rq];


同样,也有一个叫做GADBannerViewDelegate的delegate。可以实现在父窗口的UIViewController子类中,也可以单独写一个ViewController。

 1 #import <UIKit/UIKit.h>
 2 
 3 #import "GADBannerView.h"
 4 
 5 @interface FirstViewController : UIViewController <GADBannerViewDelegate> {
 6     GADBannerView *bannerView_;
 7 
 8 }
 9 
10 @end
11 
12 或者
13 #import "GADBannerView.h"
14 #import "GADBannerViewDelegate.h"
15 
16 @interface MyBannerView : GADBannerView <GADBannerViewDelegate> {
17 }
18 
19 @end


这里的delegate接口与ADBannerViewDelegate其实很类似。

 1 // Admob成功取得
 2 - (void)adViewDidReceiveAd:(GADBannerView *)adMobView {
 3     NSLog(@"Admob:adViewDidReceiveAd");
 4 
 5     // 动画表示
 6     adMobAd.frame = CGRectMake(0.0,
 7              self.view.frame.size.height,
 8              adMobView.frame.size.width,
 9              adMobView.frame.size.height);
10     [UIView beginAnimations:@"AdMobBannerMoveOnScreen" context:NULL];
11     adMobAd.frame = CGRectMake(0.0,
12              self.view.frame.size.height - adMobView.frame.size.height,
13              adMobView.frame.size.width,
14              adMobView.frame.size.height);
15     [UIView commitAnimations];
16 }
17 
18 // Admob取得失败
19 - (void)adView:(GADBannerView *)adMobView didFailToReceiveAdWithError:(GADRequestError *)error {
20     NSLog(@"Admob:didFailToReceiveAdWithError:%@", [error localizedDescription]);
21 }
22 
23 // AdMob广告被打开时
24 - (void)adViewWillPresentScreen:(GADBannerView *)adView {
25 }
26 
27 // 从AdMob的广告跳到其他程序时
28 -(void) applicationDidEnterBackground:(UIApplication*)application {
29 }
30 
31 // 从其他被打开的程序返回到AdMob广告显示
32 -(void) applicationWillEnterForeground:(UIApplication*)application {
33 }
34 
35 // AdMob广告显示被关闭时
36 - (void)adViewWillDismissScreen:(GADBannerView *)adView {
37 }


整体的代码如下所示 。这里GADBannerView的delegate实现在父窗口的ViewController中。

 1 #import "FirstViewController.h"
 2 
 3 #define MY_BANNER_UNIT_ID   @"xxxxxxxxxxxxxxx"
 4 #define ANIMATION_DURATION  0.5f
 5 #define TABBAR_HEIGHT       49.0f
 6 
 7 @implementation FirstViewController
 8 
 9 // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
10 - (void)viewDidLoad
11 {
12     [super viewDidLoad];
13 
14     // Create a view of the standard size at the bottom of the screen.
15     bannerView_ = [[GADBannerView alloc]
16                             initWithFrame:CGRectMake(0.0,
17                                             self.view.frame.size.height -
18                                             TABBAR_HEIGHT,
19                                             GAD_SIZE_320x50.width,
20                                             GAD_SIZE_320x50.height)];
21 
22     // delegate の設定
23     bannerView_.delegate = self;
24 
25     // Specify the ad‘s "unit identifier." This is your AdMob Publisher ID.
26     bannerView_.adUnitID = MY_BANNER_UNIT_ID;
27 
28     // Let the runtime know which UIViewController to restore after taking
29     // the user wherever the ad goes and add it to the view hierarchy.
30     bannerView_.rootViewController = self;
31     [self.view addSubview:bannerView_];
32 
33     // For Testing
34     GADRequest *rq = [GADRequest request];
35     rq.testing = YES;
36 
37     // Initiate a generic request to load it with an ad.
38     [bannerView_ loadRequest:rq];
39 }
40 
41 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
42 {
43     // Return YES for supported orientations
44     return (interfaceOrientation == UIInterfaceOrientationPortrait);
45 }
46 
47 - (void)didReceiveMemoryWarning
48 {
49     // Releases the view if it doesn‘t have a superview.
50     [super didReceiveMemoryWarning];
51 
52     // Release any cached data, images, etc. that aren‘t in use.
53 }
54 
55 - (void)viewDidUnload
56 {
57     [super viewDidUnload];
58 
59     // Release any retained subviews of the main view.
60     // e.g. self.myOutlet = nil;
61 }
62 
63 - (void)dealloc
64 {
65     bannerView_.delegate = nil;
66     [bannerView_ release];
67 
68     [super dealloc];
69 }
70 
71 - (void)adViewDidReceiveAd:(GADBannerView *)view {
72     [UIView animateWithDuration:ANIMATION_DURATION
73                      animations:^{
74                          bannerView_.center = CGPointMake(bannerView_.center.x, bannerView_.center.y-TABBAR_HEIGHT);
75                      }];
76 }
77 
78 @end


最终效果如下。
技术分享

iOS开发——高级技术&广告功能的实现

标签:

原文地址:http://www.cnblogs.com/iCocos/p/4598603.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!