iOS platform joins Google ADMOB-1/2 (Unity3d Development VII)

Source: Internet
Author: User
Tags joins uikit admob

Monkey Original, welcome reprint. Reproduced please specify: reproduced from the cocos2d Development network –cocos2dev.com. Thank you!

Original address: http://www.cocos2dev.com/?p=567


Unity calls iOS is still very easy, spare time in the evening to write unity on the iOS platform loaded into Google Admob. A friend who needs to be able to see.

First, write the code for the AdMob iOS.
Go to https://apps.admob.com to download the iOS AdMob SDK and register your app ID. Suppose this step is unfamiliar. Please Google yourself, the Internet should be very much.

1, Iosadmob class, mainly for the creation and onboarding of 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) {retur N } _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 both devices for test ads. You should request test ads during development//to avoid generating invalid impressions and clicks.-(Gadrequest *) creat    erequest {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.-(VO    ID) Adviewdidreceivead: (Gadbannerview *) AdView {NSLog (@ "Received ad");    _adbannerview.frame = _adrect; [_adbannerview Sethidden:_ishidden];} -(void) AdView: (Gadbannerview *) Viewdidfailtoreceiveadwitherror: (Gadrequesterror *) error {NSLog (@ "Failed to receive a    D 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

Second, AdMob external single-instance class interface
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{stat    IC 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

third, for the second step of the single interface register C function interface. provided to unity for use
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, it's over. AdMob created and the Unity interface was written.

Attention:
1. Unity-exported Iosproject does not use arc by default, so none of the above code uses arc.
Xcode->targets->build settings->objective-c Automatic Reference Counting set No, that is, turn off arc.

2. What type of Xcodeproject is created for the appellate interface?
You can create random xcodeproject. Write the code above.

You can also create Ios->framework & Library->cocos Touch Static library static libraries project.

3. How to add the above code to unity?
The first method: Copy the above code files directly into the Unity->assets/plugins/ios directory. The file is a plug-in file specified by unity.
The second approach: assume that you are creating a static library project. Xcode->product->archive compile the. A static library file and copy the. A file to the Unity->assets/plugins/ios directory.



iOS platform joins Google ADMOB-1/2 (Unity3d Development VII)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.