iOS开发——高级技术&广告功能的实现
广告功能的实现
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中实现为例。
- (void)viewDidLoad {
……【省略】……
// 初始化ADBannerView
ADBannerView *adView = [[[ADBannerView alloc] initWithFrame:CGRectZero] autorelease];
adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
// 登陆ADBannerView的delegate,这里我们设定其父窗口自己
adView.delegate = self;
// 在父窗口下方表示
adView.frame = CGRectOffset(adView.frame, , self.view.frame.size.height - self.adView.frame.size.height);
// 添加到父窗口中
[self.view addSubview:adView];
}
3:接下来,我们来实现ADBannerView的delegate。这里可以实现在父窗口的UIViewController子类中,也可以单独写一个ViewController。这里面实现了ADBannerView广告的读取,错误处理,全画面表示等delegate的处理设定。下面实现在父窗口的ViewController中。
#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
@interface XXXViewController : UIViewController<UITextFieldDelegate, ADBannerViewDelegate> {
……【省略】……
}
4:如上所示,这里增加了「ADBannerViewDelegate」protocol的实现。接下来看看都有哪些delegate接口。
// 广告读取过程中出现错误
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError * )error{
// 切换ADBannerView表示状态,显示→隐藏
// adView.frame = CGRectOffset(adView.frame, 0, self.view.frame.size.height);
}
// 成功读取广告
- (void)bannerViewDidLoadAd:(ADBannerView *)banner{
// 切换ADBannerView表示状态,隐藏→显示
// adView.frame = CGRectOffset(adView.frame, 0, self.view.frame.size.height - adView.frame.size.height);
}
// 用户点击广告是响应,返回值BOOL指定广告是否打开
// 参数willLeaveApplication是指是否用其他的程序打开该广告
// 一般在该函数内让当前View停止,以及准备全画面表示广告
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave {
NSLog(@"bannerViewActionShouldBegin:willLeaveApplication: is called.");
}
// 全画面的广告表示完了后,调用该接口
// 该接口被调用之后,当前程序一般会作为后台程序运行
// 该接口中需要回复之前被中断的处理(如果有的话)
- (void)bannerViewActionDidFinish:(ADBannerView *)banner {
NSLog(@"bannerViewActionDidFinish: is called.");
}
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的设定例程中有下面这样的定义
(BannerExampleViewController.m)。 #if !defined(MY_BANNER_UNIT_ID) #error "You must define MY_BANNER_UNIT_ID as your AdMob Publisher ID" #endif
里就需要上面介绍的注册时得到的程序专有的Publisher ID。没有它,编译的时候将报错。类似下面的样子,我们设定一下。
#define MY_BANNER_UNIT_ID @"xxxxxxxxxxxxxxx" #if !defined(MY_BANNER_UNIT_ID) #error "You must define MY_BANNER_UNIT_ID as your AdMob Publisher ID" #endif
另外,还有一个测试时用的属性测试的时候将 GADRequest::testing 属性置为 YES。如下For Testing的设置。
// Let the runtime know which UIViewController to restore after taking // the user wherever the ad goes and add it to the view hierarchy. bannerView_.rootViewController = self; [self.view addSubview:bannerView_]; // For Testing GADRequest *rq = [GADRequest request]; rq.testing = YES; // Initiate a generic request to load it with an ad. [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,用来显示广告。其创建过程如下。
// Create a view of the standard size at the bottom of the screen.
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
self.view.frame.size.height -
TABBAR_HEIGHT,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
// delegate の設定
bannerView_.delegate = self;
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// For Testing
GADRequest *rq = [GADRequest request];
rq.testing = YES;
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:rq];
同样,也有一个叫做GADBannerViewDelegate的delegate。可以实现在父窗口的UIViewController子类中,也可以单独写一个ViewController。
#import <UIKit/UIKit.h>
#import "GADBannerView.h"
@interface FirstViewController : UIViewController <GADBannerViewDelegate> {
GADBannerView *bannerView_;
}
@end
或者
#import "GADBannerView.h"
#import "GADBannerViewDelegate.h"
@interface MyBannerView : GADBannerView <GADBannerViewDelegate> {
}
@end
这里的delegate接口与ADBannerViewDelegate其实很类似。
// Admob成功取得
- (void)adViewDidReceiveAd:(GADBannerView *)adMobView {
NSLog(@"Admob:adViewDidReceiveAd");
// 动画表示
adMobAd.frame = CGRectMake(0.0,
self.view.frame.size.height,
adMobView.frame.size.width,
adMobView.frame.size.height);
[UIView beginAnimations:@"AdMobBannerMoveOnScreen" context:NULL];
adMobAd.frame = CGRectMake(0.0,
self.view.frame.size.height - adMobView.frame.size.height,
adMobView.frame.size.width,
adMobView.frame.size.height);
[UIView commitAnimations];
}
// Admob取得失败
- (void)adView:(GADBannerView *)adMobView didFailToReceiveAdWithError:(GADRequestError *)error {
NSLog(@"Admob:didFailToReceiveAdWithError:%@", [error localizedDescription]);
}
// AdMob广告被打开时
- (void)adViewWillPresentScreen:(GADBannerView *)adView {
}
// 从AdMob的广告跳到其他程序时
-(void) applicationDidEnterBackground:(UIApplication*)application {
}
// 从其他被打开的程序返回到AdMob广告显示
-(void) applicationWillEnterForeground:(UIApplication*)application {
}
// AdMob广告显示被关闭时
- (void)adViewWillDismissScreen:(GADBannerView *)adView {
}
整体的代码如下所示 。这里GADBannerView的delegate实现在父窗口的ViewController中。
#import "FirstViewController.h"
#define MY_BANNER_UNIT_ID @"xxxxxxxxxxxxxxx"
#define ANIMATION_DURATION 0.5f
#define TABBAR_HEIGHT 49.0f
@implementation FirstViewController
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad];
// Create a view of the standard size at the bottom of the screen.
bannerView_ = [[GADBannerView alloc]
initWithFrame:CGRectMake(0.0,
self.view.frame.size.height -
TABBAR_HEIGHT,
GAD_SIZE_320x50.width,
GAD_SIZE_320x50.height)];
// delegate の設定
bannerView_.delegate = self;
// Specify the ad's "unit identifier." This is your AdMob Publisher ID.
bannerView_.adUnitID = MY_BANNER_UNIT_ID;
// Let the runtime know which UIViewController to restore after taking
// the user wherever the ad goes and add it to the view hierarchy.
bannerView_.rootViewController = self;
[self.view addSubview:bannerView_];
// For Testing
GADRequest *rq = [GADRequest request];
rq.testing = YES;
// Initiate a generic request to load it with an ad.
[bannerView_ loadRequest:rq];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc
{
bannerView_.delegate = nil;
[bannerView_ release];
[super dealloc];
}
- (void)adViewDidReceiveAd:(GADBannerView *)view {
[UIView animateWithDuration:ANIMATION_DURATION
animations:^{
bannerView_.center = CGPointMake(bannerView_.center.x, bannerView_.center.y-TABBAR_HEIGHT);
}];
}
@end
最终效果如下。
iOS开发——高级技术&广告功能的实现的更多相关文章
- iOS开发——高级技术&通讯录功能的实现
通讯录功能的实现 iOS 提供了对通讯录操作的组建,其中一个是直接操作通讯录,另一个是调用通讯录的 UI 组建.实现方法如下: 添加AddressBook.framework到工程中. 代码实现: 1 ...
- iOS开发——高级技术&密码锁功能的实现
密码锁功能的实现 一个ios手势密码功能实现 ipad/iphone 都可以用 没有使用图片,里面可以通过view自己添加 keychain做的数据持久化,利用苹果官方KeychainItemWrap ...
- iOS开发——高级技术&支付宝功能的实现
支付宝功能的实现 现在不少app内都集成了支付宝功能 使用支付宝进行一个完整的支付功能,大致有以下步骤: 1>先与支付宝签约,获得商户ID(partner)和账号ID(seller) (这个 ...
- iOS开发——高级技术&摇一摇功能的实现
摇一摇功能的实现 在AppStore中多样化功能越来越多的被使用了,所以今天就开始介绍一些iOS开发的比较实用,但是我们接触的比较少的功能,我们先从摇一摇功能开始 在 UIResponder中存在这么 ...
- iOS开发——高级技术OC篇&运行时(Runtime)机制
运行时(Runtime)机制 本文将会以笔者个人的小小研究为例总结一下关于iOS开发中运行时的使用和常用方法的介绍,关于跟多运行时相关技术请查看笔者之前写的运行时高级用法及相关语法或者查看响应官方文档 ...
- iOS开发——高级技术精选OC篇&Runtime之字典转模型实战
Runtime之字典转模型实战 如果您还不知道什么是runtime,那么请先看看这几篇文章: http://www.cnblogs.com/iCocos/p/4734687.html http://w ...
- iOS开发——高级技术&广告服务
广告服务 上 面也提到做iOS开发另一收益来源就是广告,在iOS上有很多广告服务可以集成,使用比较多的就是苹果的iAd.谷歌的Admob,下面简单演示一下如何 使用iAd来集成广告.使用iAd集成广告 ...
- iOS开发——高级技术&内购服务
内购服务 大家都知道做iOS开发本身的收入有三种来源:出售应用.内购和广告.国内用户通常很少直接 购买应用,因此对于开发者而言(特别是个人开发者),内购和广告收入就成了主要的收入来源.内购营销模式,通 ...
- iOS开发——高级技术&签名机制
签名机制 最近看了objc.io上第17期中的文章 <Inside Code Signing> 对应的中文翻译版 <代码签名探析> ,受益颇深,对iOS代码签名机制有了进一步的 ...
随机推荐
- Java [leetcode 15] 3Sum
问题描述: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find ...
- Java [leetcode 10] Regular Expression Matching
问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single cha ...
- Entity Framework4.0 (七) EF4的存储过程
前面了解了EF4的CRUD的操作,你会发现EF4使用起来比较简单的.呵呵,之前我们使用数据库的时候,有时会使用存储过程代替在代码中直接使用SQL语句. 使用存储过程的好处: 提高效率:因为存储过程是经 ...
- MySQL join的实现原理及优化思路
Join 的实现原理 在MySQL 中,只有一种Join 算法,也就是Nested Loop Join,没有其他很多数据库所提供的Hash Join,也没有Sort Merge Join.顾名思义,N ...
- 根据IP地址查询所在地
using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Tex ...
- How to cancel parallel loops in .NET C# z
Cancellation token Parallel options CancellationTokenSource cancellationTokenSource = new Cancellati ...
- ChineseCounter.cs 统计中文文本中常用字占比
http://www.tuicool.com/articles/qmMba2 1 using System; using System.IO; using System.Collections.Gen ...
- POJ 2486-Apple Tree(树状背包)
题意 n个节点的树,每个节点都有价值,求在树上最多走k步获得的最大价值(从1开始走,节点的价值只能获取一次) 分析: 开始以为是最基础的树形背包,但后来发现,不能只走子树,有可能还回到根节点,dp[i ...
- 备份 VPS 上得内容到国内
起源: 最近毕设快开题了,校园网进入了收费测试的阶段,得把车辆的数据库 down 下来.发现国内 down 的速度真心慢呢.于是乎使用了在美国的 VPS 来 down,果不其然,30M 左右的下载速度 ...
- <转>MySql 与Oracle区别
http://blog.sina.com.cn/s/blog_61e034d50100k6xn.html 近期突击学习了mysql,应杨毅的邀请,简单比较一下mysql和oracle的差别,不当之处欢 ...