iAd的推出无疑给Iphone/IPad的应用程序开发者打开了另一条创收之门,前不久,美国的一位开发者Json
Ting开发的将相机闪光灯转为手电筒的应用,集成iAd后在第一天就给他带来了1400$的广告收入。
我将在这篇文章中讲讲如何把iAd集成到你的应用程序中。另外也会提到集成中可能遇到的一些问题:
- 如何支持横屏跟竖屏。
- 如何保持与os 3.0的后向兼容。
- 与UITableViewController的集成。
1)将Base SDK设为4.0, 将Deployment
target设为3.0.
![](http://www.raywenderlich.com/wp-content/uploads/2010/06/BaseSDK4.png)
![](http://www.raywenderlich.com/wp-content/uploads/2010/06/DeploymentTarget.png)
2) 链接iAd Framework.
右击Frameworks, 选择"Add\Existing Frameworks", 添加"iAd.framework".
但是在没有iAd.framework的机器上,比如3.x版本的,这样会Crash.所以要把这个链接变为weak link.
在"targets"中右击你的工程,然后"Get Info", 在Linking\Other Linker
Flags里添加"-weak_framework iAd". 这样就能够保证程序的后向兼容性。
3) 在XIB中加入你的UI
可以考虑把其它功能性的UI加在一个父亲UIView,后面把iAd跟这个父亲UIView作为同一级,这样iAd显示时不会影响原有UI。
4)与UIViewController的集成
4.1 获取iAd Banner大小的帮助函数(见示例).
4.2 创建iAd
Banner的函数.
- (void)createAdBannerView { Class classAdBannerView = NSClassFromString(@"ADBannerView"); if (classAdBannerView != nil) { ... } }这个地方使用NSClassFromString 能够保证代码的后向兼容性,在os 3.x的系统上这个函数不会成功,iAd不会显示,但是程序仍然能够运行.
4.3 调整功能性UI及iAd
Banner的位置的函数。(见代码示例 fixupAdView)
4.4 在合适时机创建和调整iAd Banner位置。
- (void)viewDidLoad
{
[self createAdBannerView];
}
- (void) viewWillAppear:(BOOL)animated
{
[self refresh];
[self fixupAdView:[UIDevice currentDevice].orientation];
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
duration:(NSTimeInterval)duration
{
[self fixupAdView:toInterfaceOrientation];
}
4.5 实现ADBannerViewDelegate
- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
if (!_adBannerViewIsVisible)
{
_adBannerViewIsVisible = YES;
[self fixupAdView:[UIDevice currentDevice].orientation];
}
}
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
if (_adBannerViewIsVisible)
{
_adBannerViewIsVisible = NO;
[self fixupAdView:[UIDevice currentDevice].orientation];
}
}
5) 与UITableViewController的集成(更新中)
参考:
黄锦
Email: hj@s-tuan.com
关注移动互联网,电子商务