iOS平台加入Google Admob -1/2(Unity3D开发之七)

Posted blfbuaa

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS平台加入Google Admob -1/2(Unity3D开发之七)相关的知识,希望对你有一定的参考价值。

猴子原创,欢迎转载。转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com。谢谢!

原文地址: http://www.cocos2dev.com/?p=567

技术分享

Unity调用ios还是非常easy的,晚上空暇时间写了unity在IOS平台载入Google Admob。须要的朋友能够看下。

一、首先编写Admob的iOS的代码。
请去https://apps.admob.com下载iOS Admob SDK, 并注冊app id。假设这一步不熟悉。请自行Google,网上应该非常多。

1、IOSAdmob类,主要实现Admob的创建和载入
IOSAdmob.h

//
//  IOSAdmob.h
//  IOSAdmob
//
//  Created by LiuYanghui on 14-8-4.
//  Copyright (c) 2014年 LiuYanghui. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "GADBannerView.h"
#import "GADBannerViewDelegate.h"

@interface IOSAdmob : NSObject<GADBannerViewDelegate>

- (void)startRequestAdmob:(NSString*)admobId AdSize:(GADAdSize)size Position:(CGPoint)pos;
- (void)setAdmobHidden:(BOOL)isHidden;

@end

IOSAdmob.m

//
//  IOSAdmob.m
//  IOSAdmob
//
//  Created by LiuYanghui on 14-8-4.
//  Copyright (c) 2014年 LiuYanghui. All rights reserved.
//

#import "IOSAdmob.h"

@interface IOSAdmob ()
    @property (nonatomic) BOOL isHidden;
    @property (nonatomic) CGRect adRect;
    @property (nonatomic, readonly, strong) UIViewController* rootViewController;
    @property (nonatomic, readonly, strong) GADBannerView* adBannerView;
@end

@implementation IOSAdmob

- (id) init
{
	self = [super init];
	if(self != nil)
	{
        _isHidden = NO;
        _adRect = CGRectZero;
        _rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
        _adBannerView = nil;
	}
	return self;
}

- (void) dealloc
{
    [_rootViewController release];
    [_adBannerView release];
    
	[super dealloc];
}

- (void) startRequestAdmob:(NSString*)admobId AdSize:(GADAdSize)size Position:(CGPoint)pos
{
    if (_adBannerView) { return; }
    
    _adBannerView = [[GADBannerView alloc] initWithAdSize:size];
//    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
//        
//    }else {
//
//    }
    
    _adRect = CGRectMake(pos.x, pos.y, _adBannerView.frame.size.width, _adBannerView.frame.size.height);
    _adBannerView.adUnitID = admobId;
    _adBannerView.delegate = self;
    [_adBannerView setRootViewController:_rootViewController];
    _adBannerView.frame = _adRect;
    [_rootViewController.view addSubview:_adBannerView];
    
    double delayInSeconds = 5.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"Retrying to load request");
        [_adBannerView loadRequest:[self createRequest]];
    });
}

- (void)setAdmobHidden:(BOOL)isHidden
{
    _isHidden = isHidden;
    if (_adBannerView) {
        [_adBannerView setHidden:_isHidden];
    }
    //        [UIView animateWithDuration:0.5 animations:^ {
    //            _adBannerView.frame = _adRect;
    //        }];
}

#pragma mark GADRequest generation

// Here we‘re creating a simple GADRequest and whitelisting the simulator
// and two devices for test ads. You should request test ads during development
// to avoid generating invalid impressions and clicks.
- (GADRequest *)createRequest {
    GADRequest *request = [GADRequest request];
    return request;
}

#pragma mark GADBannerViewDelegate impl

// Since we‘ve received an ad, let‘s go ahead and set the frame to display it.
- (void)adViewDidReceiveAd:(GADBannerView *)adView {
    NSLog(@"Received ad");
    _adBannerView.frame = _adRect;
    [_adBannerView setHidden:_isHidden];
}

- (void)adView:(GADBannerView *)view
didFailToReceiveAdWithError:(GADRequestError *)error {
    NSLog(@"Failed to receive ad with error: %@", [error localizedFailureReason]);
    double delayInSeconds = 5.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayInSeconds * NSEC_PER_SEC));
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        NSLog(@"Retrying to load request");
        [_adBannerView loadRequest:[self createRequest]];
    });
}

@end

二、Admob对外单例类接口
IOSAdmobManager.h

//
//  IOSAdmobManager.h
//  IOSAdmob
//
//  Created by LiuYanghui on 14-8-4.
//  Copyright (c) 2014年 LiuYanghui. All rights reserved.
//

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

@interface IOSAdmobManager : NSObject

+ (IOSAdmobManager *)getInstance;

- (void)startRequestAdmob:(NSString*)admobId AdSizeId:(NSInteger)adSizeId Position:(CGPoint)pos;
- (void)setAdmobHidden:(BOOL)isHidden;

@end

IOSAdmobManager.m

//
//  IOSAdmobManager.m
//  IOSAdmob
//
//  Created by LiuYanghui on 14-8-4.
//  Copyright (c) 2014年 LiuYanghui. All rights reserved.
//

#import "IOSAdmobManager.h"
#import "IOSAdmob.h"

@interface IOSAdmobManager ()
@property (nonatomic, readonly, strong) IOSAdmob* admob;
@end


@implementation IOSAdmobManager

+ (IOSAdmobManager *)getInstance
{
    static IOSAdmobManager *mgr = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        mgr = [[self alloc] init];
    });
    return mgr;
}

- (id)init
{
    self = [super init];
    if (self) {
        _admob = [[IOSAdmob alloc] init];
        return self;
    }
    return nil;
}

- (void)startRequestAdmob:(NSString*)admobId AdSizeId:(NSInteger)adSizeId Position:(CGPoint)pos
{
    switch (adSizeId) {
        case 1:
            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeBanner Position:pos];
            break;
            
        case 2:
            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeLargeBanner Position:pos];
            break;
            
        case 3:
            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeMediumRectangle Position:pos];
            break;
            
        case 4:
            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeFullBanner Position:pos];
            break;
            
        case 5:
            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeLeaderboard Position:pos];
            break;
            
        case 6:
            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeSkyscraper Position:pos];
            break;
            
        case 7:
            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeSmartBannerPortrait Position:pos];
            break;
            
        case 8:
            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeSmartBannerLandscape Position:pos];
            break;
            
        default:
            [_admob startRequestAdmob:admobId AdSize:kGADAdSizeBanner Position:pos];
            break;
    }
}

- (void)setAdmobHidden:(BOOL)isHidden
{
    [_admob setAdmobHidden:isHidden];
}

@end

三、为第二步中的单例接口注冊C函数接口。提供给Unity使用
LHAdmob.mm

//
//  LHAdmob.mm
//  IOSAdmob
//
//  Created by LiuYanghui on 14-8-4.
//  Copyright (c) 2014年 LiuYanghui. All rights reserved.
//

#import "IOSAdmobManager.h"

extern "C" {
    void startRequestAdmob(const char* admobId, int adSizeId, float pixelX, float pixelY)
    {
        [[IOSAdmobManager getInstance] startRequestAdmob:[NSString stringWithUTF8String:admobId] AdSizeId:adSizeId Position:CGPointMake(pixelX, pixelY)];
    }
    
    void setAdmobHidden(bool isHidden)
    {
        [[IOSAdmobManager getInstance] setAdmobHidden:isHidden];
    }
}

OK,到这里已经完毕了Admob的创建和Unity接口编写。

注意:
1、Unity导出的iOSproject默认没有使用arc,所以以上代码所有未使用arc。
Xcode->Targets->Build Settings->Objective-C Automatic Reference Counting 设置NO,即关闭arc。

2、针对上诉接口创建什么类型的xCodeproject ?
你能够创建随意的Xcodeproject。编写以上代码。

也能够创建iOS->Framework & Library->Cocos Touch Static Library 静态库project。

3、以上代码怎样加到Unity?
第一个方法:直接将以上代码文件拷贝到Unity->Assets/Plugins/iOS 目录中。该文件是Unity指定的插件文件。
第二个方法:假设你创建的是静态库project。Xcode->Product->Archive 编译出.a静态库文件,将.a文件拷贝到Unity->Assets/Plugins/iOS 目录中。
























以上是关于iOS平台加入Google Admob -1/2(Unity3D开发之七)的主要内容,如果未能解决你的问题,请参考以下文章

来自 AdMob 的 iOS 应用错误 = com.google.DummyAdapter?

iOS 7 Google AdMob 未出现

如何使用适用于 iOS 的 Admob (google) 设置 MoPub 中介?

iOS - pod 'Google-Mobile-Ads-SDK' 和 pod 'Firebase/AdMob' 有啥区别

iOS:google admob 横幅不适合 320x50 GADBannerView

iOS - Google AdMob v6.12.0 - “idfa 类丢失,不会收集 idfa”