给自己的应用添加iAd广告之一

本文详细介绍如何通过代码方式在iOS应用中添加iAd广告框架,并实现不同屏幕方向下的广告展示效果。文中提供了完整的代码示例及关键步骤说明。

在这里,我先给大家介绍以下如何添加iAd广告,当然了,在这一讲,我先给大家介绍的是如何用代码实现添加iAd的效果,接下来,我会接续说说如何真正在我们应用里在发布时能用到。

先给自己的项目添加iAd.framework库

接着在头文件中添加下面代码:

#import <UIKit/UIKit.h>
#import <iAd/iAd.h>

@interface iAdsDemoViewController : UIViewController 
<ADBannerViewDelegate> {
	ADBannerView *adView;
}

@end

在实现里添加如下代码:

//
//  iAdsDemoViewController.m
//  iAdsDemo
//
//  Created by gao wei on 10-6-12.
//  Copyright __MyCompanyName__ 2010. All rights reserved.
//

#import "iAdsDemoViewController.h"



@implementation iAdsDemoViewController

- (void)viewDidLoad {
	//竖屏
	adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
	adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
    //adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;//横屏是用这个
	[self.view addSubview:adView];
	adView.delegate = self;
	adView.hidden = YES;
		
	adView.requiredContentSizeIdentifiers = [NSSet setWithObjects: ADBannerContentSizeIdentifierPortrait, ADBannerContentSizeIdentifierLandscape, nil];
	
    [super viewDidLoad];
}

- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave
{
	NSLog(@"should begin");
	return YES;
}

- (void)bannerViewActionDidFinish:(ADBannerView *)banner
{
	NSLog(@"did finish");
}

- (void)bannerViewDidLoadAd:(ADBannerView *)banner
{
	NSLog(@"%d",adView.bannerLoaded);
	adView.hidden = NO;
	NSLog(@"did load");
}

- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error
{
	NSLog(@"error:%@",error);
}

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
	NSLog(@"rotate");
	//adView.frame = CGRectZero;
    if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
	{
		adView.currentContentSizeIdentifier =
		ADBannerContentSizeIdentifierLandscape;
		adView.frame = CGRectZero;
	}
    else
	{
        adView.currentContentSizeIdentifier =
		ADBannerContentSizeIdentifierPortrait;
		adView.frame = CGRectZero;
	}	
}



// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return YES;
}


- (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 {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}

@end


出现的效果图,如下:

例子下载在这里:http://download.youkuaiyun.com/detail/comeontom/4350668

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值