2025-05-24

項目需要加廣告,所以看瞭一下相關代碼,很簡單.

1.iAD 需要加入iad.framework

//.h



#import 
#import 
@interface ViewController : UIViewController

@end

//.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, 80, 320, 100)];
    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");
}

vcD4KPHA+Mi5hZG1vYiA8L3A+CjxwPtDo0qq808jrtdrI/be9zsS8/izS1LywyOfPwmZyYW1ld29yazwvcD4KPHA+PGltZyBzcmM9″https://www.aiwalls.com/uploadfile/Collfiles/20140218/20140218092600171.png” alt=”\”>

#define ADID @"xxxxxxx"
//設置一個自己的全局id

//.H
#import 
#import "GADBannerView.h"
@interface AdmobDefaultViewController : UIViewController
{
    GADBannerView *ADView;
}
@end

- (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插屏廣告


#import 
#import "GADInterstitial.h"
#import "GADInterstitialDelegate.h"

@interface InterAdmobViewController : UIViewController

@property(nonatomic, retain) GADInterstitial *interstitial;

@end


- (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];
    return request;
}

- (void)interstitialDidReceiveAd:(GADInterstitial *)interstitial {
    [interstitial presentFromRootViewController:self];
}

n?膻歛囤l鱚介hリ靷?曠(淁╪)^i?vO蕁?i侉o鰸僵躽豦?崐?…?https://www.aiwalls.com/ym/源碼:https://download.csdn.net/detail/woshiwls/6931237

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *