Who can help me! How to get the view controller(uiViewController) for Admob


Hi. I have follow "ads with AdMob" page to do. http://forum.unity3d.com/threads/16668-ads-with-AdMob/page2?highlight=admob

Pass the compiler, but still have a problem: The AdMob SDK requires a valid view controller to be returned from currentViewController AdMobDelegateProtocol). Cancelling ad request.

I understand why have this error, and I spending lot of time to find solve information, but I'm not good at Object-C. I do not know how to get unity's view controller. Who can help me solve this problem This files can help for debug >>> code&Error.zip

Thanks very much!

A few tips to other friends in need: I use Unity 3.1 / Admob 2010-11-08 / xCode 3.2.5 / iOS4.2 to building 1. Use libAdMobNoThumb.a. 2. Set 'iOS Deployment Target' to 'iOS 3.0'

Actually you can get the unity view controller with this:


just declare it on the class you are going to use it, like this:

UIViewController *UnityGetGLViewController();

and use it as you would for any other view controller, like:

[UnityGetGLViewController() presentModalViewController:myViewController];

hope it helps.

Unity3 doesn't create a view controller. I had a similar issue when trying to get an iAd banner to display.

This is how I've done it for iAd, replace iAd code statements for AdMob:

  • Build your game into an XCode project using Unity.
  • Open the XCode project in XCode.
  • Add the iAd framework to the project.
  • Create a new UIViewController derived class called RootViewController. Note, do not create an UITableViewController derived class.
  • Open AppController.h and replace the contents of the file with the following code -


@class RootViewController;

@interface AppController : NSObject<UIAccelerometerDelegate, UIApplicationDelegate>
     UIWindow* _window;

  • (void) startUnity:(UIApplication*)application;
  • (void) startRendering:(UIApplication*)application;

@interface EAGLView : UIView {}



Note I've MOVED the EAGLView definition from AppController.mm, added a special import for the RootViewController class.

  • Open AppController.mm and add the following piece of code after the 'registerAccelerometer' method -

 #import "RootViewController.h" //This can be placed at the top of the file
-(void) registeriAdBanner
  NSLog(@"-> initialising iAd banner"); 
  UIWindow *window = [UIApplication sharedApplication].keyWindow;
  NSArray *views = window.subviews;
  EAGLView *glView = nil;

for(UIView *v in views) { if ([v isKindOfClass:[EAGLView class]]) { glView = (EAGLView *)v; break; } }

if (glView == nil) return; //This should never happen so raise an error message accordingly

RootViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; [rootController addEAGLView:glView]; window.rootViewController = rootController; [rootController release]; }


The above code finds the main window for the application. It then searches for the EAGLView, Unity renders all content to this EAGLView. Then it creates an instance of RootViewController and adds the EAGLView to it. The RootViewController instance is assigned as the rootViewController for the App and released.

  • In the same file, find the 'applicationDidFinishLaunching' method and add the following call to the 'registeriAdBanner' method -

`[self performSelector:@selector(registeriAdBanner) withObject:nil afterDelay:0.4];

  • Open the RootViewController.h file and replace all the contents with the following -

 #import "AppController.h"

@interface RootViewController : UIViewController { IBOutlet ADBannerView *_bannerView; EAGLView *_glView; BOOL _showingBanner; }

-(void) addEAGLView:(EAGLView *)glView;



  • Open RootViewController.m and replace the contents of the file with the following code -

 #import "RootViewController.h" 

@interface RootViewController()
-(void) moveBannerOnScreen;
-(void) moveBannerOffScreen;

@implementation RootViewController

-(void) addEAGLView:(EAGLView *)glView { _glView = glView; [self.view addSubview:glView]; }

#pragma mark - #pragma mark iAd Banner delegate methods

-(void) bannerViewDidLoadAd:(ADBannerView *)banner
NSLog(@“Did load ad…”);
[self moveBannerOnScreen];

-(BOOL) bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave { //TODO - Pause the game return YES; }

-(void) bannerViewActionDidFinish:(ADBannerView *)banner { NSLog(@"Did finish ad action..."); }

-(void) bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error { NSLog(@"Failed to receive an ad..."); [self moveBannerOffScreen]; }

#pragma mark - #pragma mark Private methods

-(void) moveBannerOnScreen
if (!_showingBanner)
[self.view bringSubviewToFront:_bannerView];
[UIView beginAnimations:@“Show banner” context:NULL];
_bannerView.frame = CGRectMake(0, 0, 320, 50);
_glView.frame = CGRectMake(0, 50, 320, 430); //shrink the Unity view
[UIView commitAnimations];
_showingBanner = YES;

-(void) moveBannerOffScreen { if (_showingBanner) { [UIView beginAnimations:@"Hiding banner" context:NULL]; _bannerView.frame = CGRectMake(0, -50, 320, 50); _glView.frame = CGRectMake(0, 0, 320, 480); //enlarge the Unity view [UIView commitAnimations]; [self.view bringSubviewToFront:_glView]; _showingBanner = NO; } }

#pragma mark - #pragma mark Overrides

-(void) viewDidLoad
_bannerView = [[ADBannerView alloc] initWithFrame:CGRectMake(0, -50, 320, 50)];
_bannerView.delegate = self;
[self.view addSubview:_bannerView];
[_bannerView release];;
//TODO - Portrait, Landscape & iPad
[super viewDidLoad];

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


  • Your done.

Update: 4/17/12
Couple minor changes for anyone building latest iOS of this date:

In AppController.h you should have the following list of functions:

- (void) startUnity:(UIApplication*)application;
- (void) startRendering;
- (void) Repaint;
- (void) RepaintDisplayLink;
- (void) prepareRunLoop;

Notice that the startRendering function doesn’t pass UIApplication anymore.

In AppController.mm place the registeriAdBanner selection in didFinishLaunchingWithOptions:

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
    // The default notification code

    [self performSelector:@selector(registeriAdBanner) withObject:nil afterDelay:0.4];
   	[self startUnity:application];

	return NO;

Hi! I got an error saying “Duplicate definition of EAGLView class” in Iphone_View.h. Please help me soon.

I’ve been working with this code, and found that I had to comment out the code in AppController.h

// its located in iphone_view.h 
//@interface EAGLView : UIView {}