Thursday, 8 December 2011

adding iAd and in-app promotion alertView in Xcode for Unity project

Hi there.


so today i finally finished 1.0 version of my second app Bubble Mayhem, and i stumbled into making a lite version for it.
the process involves adding iAd banner and an in-app promotion pop-up that comes out each time the app become active again. there wasn't a place i can find a complete answer for this so i think it would be nice to share my result of half a day's research here and maybe people will see and find something useful.


all right less talk more work.


i didnt come up with these by my self and the original pages are at http://answers.unity3d.com/questions/40494/who-can-help-me-how-to-get-the-view-controlleruivi.html
and the next one if you happen to read Chinese:   http://www.cnblogs.com/CallmePandora/archive/2011/11/03/Unity_plugin_for_iOS.html




So this is what i did:


1.in xcode 4.2, add file - choose UIViewController subclass with the xib file, naming RootViewController. Also add iAd framework to the target.


we need to make a root viewController and make the EAGLView a subview of this root voewColtroler if im not mistaken. im semi-newbie i suppose.




2.in RootViewController.h replace all with:


#import <UIKit/UIKit.h>
#import <iAd/iAd.h>
#import "AppController.h"
@interface RootViewController : UIViewController<ADBannerViewDelegate> { IBOutlet ADBannerView *_bannerView; EAGLView *_glView; BOOL _showingBanner; } 
-(void) addEAGLView:(EAGLView *)glView; 
@end

3.in RootViewController.m  replace all with:

#import "RootViewController.h" 


@interface RootViewController()
-(void) moveBannerOnScreen;
-(void) moveBannerOffScreen;
@end
@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 
    
    AppController *myAppController = [[AppController alloc] init];
    [myAppController myPauseUnity];
    
    return YES; 
}



-(void) bannerViewActionDidFinish:(ADBannerView *)banner { 
    NSLog(@"Did finish ad action..."); 
   
    AppController *myAppController = [[AppController alloc] init];
    [myAppController myResumeUnity];
    
    
}


-(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, 430, 320, 50);
        _glView.frame = CGRectMake(0, 0, 320, 480); //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]; }
                
@end
4. in AppController.h  replace all with :

#import <UIKit/UIKit.h> 

@class RootViewController;

@interface AppController : NSObject<UIAccelerometerDelegate, UIApplicationDelegate>
{
    UIWindow*         _window;
}
- (void) startUnity:(UIApplication*)application;
- (void) startRendering:(UIApplication*)application;
- (void) myPauseUnity;
- (void) myResumeUnity;
@end 

@interface EAGLView : UIView {}
@end 

#define NSTIMER_BASED_LOOP 0
#define THREAD_BASED_LOOP 1
#define EVENT_PUMP_BASED_LOOP 2
5. now we go ahead to the AppController.mm file and do some triky things:

first remove these:
@interface EAGLView : UIView {}
@end
since we already doing this in AppController.h which is linked to RootViewController by the line "@class RootViewController" (we already done this) which i dont fully understand at all..

add 
#import "RootViewController.h"
at the top where it should goes

then inside @implementation block of AppController   add these:
-(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]; 
}



- (void) myPauseUnity
{
//this pause unity
    printf_console("myPauseUnity called\n");
    UnityPause(true);
    
 _didResignActive = YES;
    
}

- (void) myResumeUnity
{
//resume unity player
    printf_console("myResumeUnity called\n");
    UnityPause(false);
    
 _didResignActive = NO;
    
    
}
next inside - (void) applicationDidFinishLaunching:(UIApplication*)application   (we are still in the implementation block )  add:
[self performSelector:@selector(registeriAdBanner) withObject:nil afterDelay:0.4];



then inside - (void) applicationDidBecomeActive:(UIApplication*)application add:


/*re-register banner, i added this myself and not sure if necessary told ya im new*/


[self registeriAdBanner];
    
//this is the promotion popup we are doing
    UIAlertView *myAlertView = [[UIAlertView alloc] initWithTitle:@"Like this game?" message:@"Consider buying paid version?" delegate:self cancelButtonTitle:@"No,thanks" otherButtonTitles:@"OK", nil];
    [myAlertView show];
    [myAlertView release];
all right we are almost there . now we add the button functions for the alertView:
after applicationDidBecomeActive function  add:
- (void)alertView:(UIAlertView *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
/*this is the "ok" button. i didnt bother write code for the cancel button since it is already doing its job*/
    if(buttonIndex == 1)
    {
        NSString *str = @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=XXXXX";
/*XXXXX is the Apple Id for the paid version of the app, which in my case is a 9 digit number. thats right you need to register the paid version in itunes connect first and this is the way to link without even having the app on sale. Note that i just implemented this code into my project after read the Chinese guy's blog and i honestly have no idea whether itll work or not. but it looks promising. im new i know bare with me*/
        
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        
    }
}

and we are done. comment if you hate me or whatsoever :P. 
and be sure to check out Bubble Mayhem or Bubble Mayhem Lite if you want to see the iAd and in-app promotion result! i just uploaded today its gonna take a few days. 



Friday, 30 September 2011

My First Game!

Hi there!

My new game Transferral for iPhone, iPod touch, and iPad is currently in review by apple and will soon be available for all!

The game is themed to be puzzle solving. Two modes in the game : cuboid and plane. The little robot is trapped in a cuboid/maze and has to utilize the portals to escape.

festures include first person shoot mode in which an innovative control method is used - control with two joysticks , one for horizontal movement , the other for vertical movenent. Be sure to try out this.

Since it is my first game I am sure there are features that gamers may not enjoy. Please leave comments and/or suggestions and it may be in the next version!