Add Google Admob-1/2 to iOS platform (7 from Unity3D Development)

Source: Internet
Author: User
Tags admob

Add Google Admob-1/2 to iOS platform (7 from Unity3D Development)

 

 

The call to iOS by Unity is still very simple. In the free time of the night, unity loads Google Admob on the IOS platform. You can check it out.

I. First write the iOS code of Admob.
Please go to https://apps.admob.com to download iOS Admob SDK and register app id. If you are not familiar with this step, please Google yourself. There should be a lot of Internet.

1. IOSAdmob class, mainly used to create and load Admob
IOSAdmob. h

/// IOSAdmob. h // IOSAdmob /// Created by LiuYanghui on 14-8-4. // Copyright (c) 2014 LiuYanghui. all rights reserved. // # import
 
  
# Import GADBannerView. h # import GADBannerViewDelegate. h @ interface IOSAdmob: NSObject
  
   
-(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; interval popTime = dispatch_time (interval, (int64_t) (delayInSeconds * interval); dispatch_after (popTime, interval (), ^ (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 re creating a simple GADRequest and whitelisting the simulator // and two devices for test ads. you shoshould 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 ve got ed 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 *) viewdidFailToReceiveAdWithError :( GADRequestError *) error {NSLog (@ Failed to receive ad with error :%@, [error Failed]); 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

Ii. Admob's external Singleton class Interface
IOSAdmobManager. h

 

 

/// IOSAdmobManager. h // IOSAdmob /// Created by LiuYanghui on 14-8-4. // Copyright (c) 2014 LiuYanghui. all rights reserved. // # import
 
  
# Import
  
   
@ 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: Position: pos]; break; case 2: [_ admob startRequestAdmob: admobId AdSize: Position: pos]; break; case 3: [_ admob startRequestAdmob: admobId AdSize: Position: pos]; break; case 4: [_ admob startRequestAdmob: admobId AdSize: Position: pos]; break; case 5: [_ admob startRequestAdmob: admobId AdSize: kGADAdSizeLeaderboard Position: pos]; break; case 6: [_ admob startRequestAdmob: admobId AdSize: Increment Position: pos]; break; case 7: [_ admob startRequestAdmob: admobId AdSize: Increment Position: pos]; break; case 8: [_ admob startRequestAdmob: admobId AdSize: Position: pos]; break; default: [_ admob startRequestAdmob: admobId AdSize: kGADAdSizeBanner Position: pos]; break ;}} -(void) setAdmobHidden :( BOOL) isHidden {[_ admob setAdmobHidden: isHidden];} @ end

3. register the C function interface for the singleton interface in step 2 and provide it to Unity.
LHAdmob. mm

 

 

//// LHAdmob. mm // IOSAdmob /// Created by LiuYanghui on 14-8-4. // Copyright (c) 2014 LiuYanghui. all rights reserved. // # import IOSAdmobManager. hextern C {void expect (const char * admobId, int adSizeId, float pixelX, float pixelY) {[[IOSAdmobManager getInstance] handle: [NSString handle: admobId] AdSizeId: adSizeId Position: CGPointMake (pixelX, pixelY)];} void setAdmobHidden (bool isHidden) {[[IOSAdmobManager getInstance] setAdmobHidden: isHidden];}

 

OK. Now the creation of Admob and the compilation of the Unity interface have been completed.

Note:
1. By default, arc is not used in the iOS project exported by Unity, so none of the above Code uses arc.
Xcode-> Targets-> Build Settings-> Objective-C Automatic Reference Counting: set NO to disable arc.

2. What types of xCode projects are created for the appeal interface?
You can create any Xcode project and write the above Code. You can also create a Static Library project for iOS-> Framework & Library-> Cocos Touch Static Library.

3. How can I add the above Code to Unity?
Method 1: copy the above Code file to the Unity-> Assets/Plugins/iOS folder. The file is the specified plug-in file of Unity.
Method 2: If you create a static library project, Xcode> Product> Archive compiles the project. a static library file. copy file a to the Unity-> Assets/Plugins/iOS folder.


 

Related Article

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.