iOS开发-植入广告(iAd, Admob实例)

Posted liguangsunls

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS开发-植入广告(iAd, Admob实例)相关的知识,希望对你有一定的参考价值。

应用中植入广告是一种非常好的盈利手段。

以下介绍主流的两种方法。iAd, Admob


先mark一个非常具体的pdf。   http://pan.baidu.com/share/link?shareid=1656439633&uk=1394536315&fid=406566606116897

一。iAd

1.须要增加iAd.framework

2.   .h文件增加例如以下代码

#import <UIKit/UIKit.h>  
#import <iAd/iAd.h>  
@interface ViewController : UIViewController<ADBannerViewDelegate> 

3.   .m文件增加例如以下代码


#import "ViewController.h"  
  
@interface ViewController ()  
@property (nonatomic,strong)ADBannerView *adView;  
@end  
  
@implementation ViewController  
  
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    self.adView = [[ADBannerView alloc]initWithFrame:CGRectMake(0, 64, 320, 50)];  
    self.adView.requiredContentSizeIdentifiers = [NSSet setWithObject:ADBannerContentSizeIdentifierPortrait];  
    self.adView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;  
      
    self.adView.delegate = self;  
    [self.view addSubview:self.adView];  
}  
  
- (void)bannerViewWillLoadAd:(ADBannerView *)banner{  
    NSLog(@"bannerViewWillLoadAd");  
}  
  
- (void)bannerViewDidLoadAd:(ADBannerView *)banner  
{  
    NSLog(@"bannerViewDidLoadAd");  
}  
  
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error  
{  
    NSLog(@"didFailToReceiveAdWithError");  
}  

效果图:

技术分享






二。admob 

1.须要增加第三方文件,以及例如以下framework

技术分享技术分享

2.

#define ADID @"xxxxxxx"  
//设置一个自己的全局id

3.   .h文件增加例如以下代码
#import <UIKit/UIKit.h>  
#import "GADBannerView.h"  
@interface AdmobDefaultViewController : UIViewController  
{  
    GADBannerView *ADView;  
}

3.   .m文件增加例如以下代码
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    // Do any additional setup after loading the view, typically from a nib.  
      
    // Create a view of the standard size at the bottom of the screen.  
    ADView = [[GADBannerView alloc]  
                   initWithFrame:CGRectMake(0.0,self.view.frame.size.height - GAD_SIZE_320x50.height,GAD_SIZE_320x50.width,GAD_SIZE_320x50.height)];  
      
    ADView.adUnitID = ADID;//调用id  
      
    ADView.rootViewController = self;  
    ADView.backgroundColor = [UIColor yellowColor];  
    [self.view addSubview:ADView];  
      
    [ADView loadRequest:[GADRequest request]];  
}

技术分享


3。

ADMOB插屏广告


.h文件代码
#import <UIKit/UIKit.h>  
#import "GADInterstitial.h"  
#import "GADInterstitialDelegate.h"  
  
@interface InterAdmobViewController : UIViewController<GADInterstitialDelegate>  
  
@property(nonatomic, retain) GADInterstitial *interstitial;  
  
@end

.m文件代码
- (void)viewDidLoad  
{  
    [super viewDidLoad];  
      
    self.interstitial = [[GADInterstitial alloc] init];  
    self.interstitial.delegate = self;  
    self.interstitial.adUnitID = ADID;  
      
    [self.interstitial loadRequest: [self createRequest]];  
      
}  
  
- (GADRequest *)createRequest {  
    GADRequest *request = [GADRequest request];  
      
    // Make the request for a test ad. Put in an identifier for the simulator as  
    // well as any devices you want to receive test ads.  
    request.testDevices =  
    [NSArray arrayWithObjects:  
     // TODO: Add your device/simulator test identifiers here. They are  
     // printed to the console when the app is launched.  
     nil nil];  
    return request;  
}  
  
- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {  
    [interstitial presentFromRootViewController:self];  
}


技术分享























以上是关于iOS开发-植入广告(iAd, Admob实例)的主要内容,如果未能解决你的问题,请参考以下文章

iPhone -iAd、Admob 和本地广告

AdMob 中介:iAd 在 iOS 6 上无法正确显示

AdMob 中介:iAd 在 iOS 6 上无法正确显示

AdWhirl (AdMob, iAd) 广告未在已发布的 iPhone 应用程序中显示

AdWhirl (AdMob, iAd) 广告未在已发布的 iPhone 应用程序中显示

iAd 和 AdMob 广告未在已发布的 iPhone 应用程序中显示