Created
November 18, 2012 00:27
-
-
Save reefwing/4101921 to your computer and use it in GitHub Desktop.
Tutorial 23 - iAds for Codea
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// aGameCenter_Codea.h | |
// | |
// Created by Juan Belón on 28/05/12 | |
// Games -> http://www.xixgames.com | |
// LGPL - @juaxix - Codea connection! | |
// | |
// Modified by Reefwing Software on 14/10/12 | |
// http://www.reefwing.com.au | |
// @reefwing on the Codea Forums | |
// | |
// Version: 1.5 | |
// - Multiple Leaderboard (Easy, Medium & Hard) access added | |
// - Achievements added. | |
// Version: 1.6 | |
// - Modified for universal apps (http://codeatuts.blogspot.com.au/2012/10/tutorial-22-building-universal-app-in.html) | |
// - New showLeaderBoard: (int)ident method | |
// Version: 1.7 | |
// - iAds support added | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// | |
#import <Foundation/Foundation.h> | |
#import <GameKit/GameKit.h> | |
#import <AVFoundation/AVAudioPlayer.h> | |
@interface aGameCenter_Codea : NSObject <GKLeaderboardViewControllerDelegate, AVAudioPlayerDelegate, GKAchievementViewControllerDelegate> | |
{ | |
bool hasGameCenter; | |
bool playing; | |
AVAudioPlayer *player; | |
} | |
// Game Center Methods | |
- (void) start; | |
- (void) authenticateLocalPlayer; | |
- (void) registerForAuthenticationNotification; | |
- (void) authenticationChanged; | |
- (BOOL) isGameCenterAvailable; | |
- (void) leaderboardViewControllerDidFinish: (GKLeaderboardViewController *)viewController; | |
- (void) showAchievements; | |
- (void) achievementViewControllerDidFinish:(GKAchievementViewController *)viewController; | |
- (void) resetAchievements; | |
- (void) reportAchievementIdentifier: (int) ident; | |
- (void) showLeaderboard: (int)ident; | |
- (void) showEasyLeaderboard; | |
- (void) showMediumLeaderboard; | |
- (void) showHardLeaderboard; | |
- (void) reportEasyScore:(int) score; | |
- (void) reportMediumScore:(int) score; | |
- (void) reportHardScore:(int) score; | |
// Music Player Methods | |
- (void) playMusic:(int) songNumber; | |
- (void) stopMusic; | |
// iAd Methods | |
- (void) showBannerAd: (bool)display; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// aGameCenter_Codea.m | |
// | |
// Created by Juan Belón on 28/05/12 | |
// Games -> http://www.xixgames.com | |
// LGPL - @juaxix - Codea connection! | |
// | |
// Modified by Reefwing Software on 14/10/12 | |
// http://www.reefwing.com.au | |
// @reefwing on the Codea Forums | |
// | |
// Version: 1.5 | |
// - Multiple Leaderboard (Easy, Medium & Hard) access added | |
// - Achievements added. | |
// Version: 1.6 | |
// - Modified for universal apps (http://codeatuts.blogspot.com.au/2012/10/tutorial-22-building-universal-app-in.html) | |
// - New showLeaderBoard: (int)ident method | |
// Version: 1.7 | |
// - iAds support added | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// | |
#import "aGameCenter_Codea.h" | |
#import "CodifyAppDelegate.h" | |
#import "SharedRenderer.h" | |
@implementation aGameCenter_Codea | |
- (id) init | |
{ | |
self = [super init]; | |
player = [[AVAudioPlayer alloc] init]; | |
playing = false; | |
return self; | |
} | |
- (void) dealloc | |
{ | |
[player release]; | |
[super dealloc]; | |
} | |
- (void) showBannerAd: (bool)display | |
{ | |
[CodifyAppDelegate delegate].displayingAds = display ? YES : NO; | |
[[CodifyAppDelegate delegate] iAdDisplayStatusChanged]; | |
} | |
- (void) start | |
{ | |
//Game Center | |
hasGameCenter = false; | |
[self authenticateLocalPlayer]; | |
} | |
- (BOOL) isGameCenterAvailable | |
{ | |
// Check for presence of GKLocalPlayer API. | |
Class gcClass = (NSClassFromString(@"GKLocalPlayer")); | |
// The device must be running running iOS 4.1 or later. | |
NSString *reqSysVer = @"4.1"; | |
NSString *currSysVer = [[UIDevice currentDevice] systemVersion]; | |
BOOL osVersionSupported = ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending); | |
if (gcClass && osVersionSupported) | |
NSLog(@"Game Center is Available"); | |
else | |
NSLog(@"Game Center is not Available"); | |
return (gcClass && osVersionSupported); | |
} | |
- (void)authenticateLocalPlayer | |
{ | |
if(![self isGameCenterAvailable]) | |
{ | |
hasGameCenter = false; | |
return; | |
} | |
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) | |
{ | |
if (error == nil) | |
{ | |
[self registerForAuthenticationNotification]; | |
hasGameCenter = true; | |
NSLog(@"Game Center - local player authenticated."); | |
}else | |
{ | |
hasGameCenter = false; | |
} | |
}]; | |
} | |
- (void)registerForAuthenticationNotification | |
{ | |
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; | |
[nc addObserver: self selector:@selector(authenticationChanged) name:GKPlayerAuthenticationDidChangeNotificationName object:nil]; | |
} | |
- (void)authenticationChanged | |
{ | |
if([self isGameCenterAvailable ]) | |
{ | |
return; | |
} | |
if ([GKLocalPlayer localPlayer].isAuthenticated) | |
{ | |
hasGameCenter = true; | |
}else | |
{ | |
hasGameCenter = false; | |
} | |
} | |
// Achievement Methods | |
- (void) reportAchievementIdentifier: (int) ident | |
{ | |
NSString *identifier; | |
// INSERT YOUR ACHIEVEMENT IDENTIFIERS BELOW | |
switch (ident) | |
{ | |
case 1: | |
identifier = @"easyWinner"; | |
break; | |
case 2: | |
identifier = @"mediumWinner"; | |
break; | |
case 3: | |
identifier = @"hardWinner"; | |
break; | |
case 4: | |
identifier = @"boom"; | |
break; | |
case 5: | |
identifier = @"badLuck"; | |
break; | |
case 6: | |
identifier = @"decathlon"; | |
break; | |
case 7: | |
identifier = @"centurion"; | |
break; | |
default: | |
NSLog(@"Warning Unknown Achievement"); | |
break; | |
} | |
NSLog(@"Achievement complete: %@", identifier); | |
GKAchievement *achievement = [[GKAchievement alloc] initWithIdentifier: identifier]; | |
if (achievement) | |
{ | |
achievement.showsCompletionBanner = YES; | |
achievement.percentComplete = 100.0; | |
[achievement reportAchievementWithCompletionHandler:^(NSError *error) | |
{ | |
if (error != nil) | |
{ | |
NSLog(@"Error in reporting achievements: %@", error); | |
} | |
}]; | |
} | |
} | |
- (void)showAchievements | |
{ | |
GKAchievementViewController *achievements = [[GKAchievementViewController alloc] init]; | |
if (achievements != nil) | |
{ | |
achievements.achievementDelegate = self; | |
[[SharedRenderer renderer] presentViewController: achievements animated: YES completion:nil]; | |
} | |
[achievements release]; | |
} | |
- (void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController | |
{ | |
[[SharedRenderer renderer] dismissViewControllerAnimated:YES completion:nil]; | |
[[SharedRenderer renderer] startAnimation]; | |
} | |
- (void) resetAchievements | |
{ | |
// You may want to allow the player to reset their progress on achievements in your game. | |
// First, this method clears any locally cached achievement objects created by you. | |
// Then, it calls Game Kit to reset the player’s progress stored on Game Center. | |
// | |
// Clear all locally saved achievement objects - Enter your code to clear locally saved achievements below. | |
// YOUR CODE GOES HERE | |
// Clear all progress saved on Game Center. | |
[GKAchievement resetAchievementsWithCompletionHandler:^(NSError *error) | |
{ | |
if (error != nil) | |
// handle the error. | |
NSLog(@"Game Center: Error Resetting Achievements - %@", [error localizedDescription]); | |
else | |
NSLog(@"Game Center - Achievements Reset."); | |
}]; | |
} | |
// Leaderboard Methods | |
- (void)showLeaderboard: (int)ident | |
{ | |
NSString *identifier; | |
// INSERT YOUR LEADER BOARD IDENTIFIERS BELOW | |
switch (ident) | |
{ | |
case 1: | |
identifier = @"easyDifficulty"; | |
break; | |
case 2: | |
identifier = @"mediumDifficulty"; | |
break; | |
case 3: | |
identifier = @"hardDifficulty"; | |
break; | |
default: | |
NSLog(@"Warning Unknown Leader Board"); | |
break; | |
} | |
GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init]; | |
if (leaderBoardCont) | |
{ | |
// INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW | |
leaderBoardCont.category=identifier; | |
leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday; | |
leaderBoardCont.leaderboardDelegate=self; | |
[[SharedRenderer renderer] presentModalViewController:leaderBoardCont animated:YES]; | |
} | |
} | |
- (void)showEasyLeaderboard | |
{ | |
GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init]; | |
if (leaderBoardCont) | |
{ | |
// INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW | |
leaderBoardCont.category=@"easyDifficulty"; | |
leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday; | |
leaderBoardCont.leaderboardDelegate=self; | |
[[SharedRenderer renderer] presentModalViewController:leaderBoardCont animated:YES]; | |
} | |
} | |
- (void)showMediumLeaderboard | |
{ | |
GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init]; | |
if (leaderBoardCont) | |
{ | |
// INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW | |
leaderBoardCont.category=@"mediumDifficulty"; | |
leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday; | |
leaderBoardCont.leaderboardDelegate=self; | |
[[SharedRenderer renderer] presentModalViewController:leaderBoardCont animated:YES]; | |
} | |
} | |
- (void)showHardLeaderboard | |
{ | |
GKLeaderboardViewController *leaderBoardCont = [[GKLeaderboardViewController alloc] init]; | |
if (leaderBoardCont) | |
{ | |
// INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW | |
leaderBoardCont.category=@"hardDifficulty"; | |
leaderBoardCont.timeScope=GKLeaderboardTimeScopeToday; | |
leaderBoardCont.leaderboardDelegate=self; | |
[[SharedRenderer renderer] presentModalViewController:leaderBoardCont animated:YES]; | |
} | |
} | |
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController | |
{ | |
[[SharedRenderer renderer] dismissModalViewControllerAnimated:YES]; | |
[[SharedRenderer renderer] startAnimation]; | |
} | |
- (void) reportEasyScore:(int) score | |
{ | |
// INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW | |
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"easyDifficulty"] autorelease]; | |
if(scoreReporter) | |
{ | |
scoreReporter.value = score; | |
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) | |
{ | |
if (error != nil) | |
{ | |
// handle the reporting error | |
NSLog(@"Game Center: Error Reporting Easy Score - %@", [error localizedDescription]); | |
} | |
}]; | |
} | |
} | |
- (void)reportMediumScore:(int) score | |
{ | |
// INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW | |
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"mediumDifficulty"] autorelease]; | |
if(scoreReporter) | |
{ | |
scoreReporter.value = score; | |
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) | |
{ | |
if (error != nil) | |
{ | |
// handle the reporting error | |
NSLog(@"Game Center: Error Reporting Medium Score - %@", [error localizedDescription]); | |
} | |
}]; | |
} | |
} | |
- (void)reportHardScore:(int) score | |
{ | |
// INSERT YOUR LEADERBOARD IDENTIFIER IN THE LINE BELOW | |
GKScore *scoreReporter = [[[GKScore alloc] initWithCategory: @"hardDifficulty"] autorelease]; | |
if(scoreReporter) | |
{ | |
scoreReporter.value = score; | |
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) | |
{ | |
if (error != nil) | |
{ | |
// handle the reporting error | |
NSLog(@"Game Center: Error Reporting Hard Score - %@", [error localizedDescription]); | |
} | |
}]; | |
} | |
} | |
// Music Play and Stop Methods | |
- (void)playMusic:(int) songNumber | |
{ | |
NSString* resourcePath = [[NSBundle mainBundle] resourcePath]; | |
NSString* song = [[NSString alloc] initWithString:@""]; | |
NSError* err; | |
switch (songNumber) | |
{ | |
case 1: //menu song | |
song = @"/menu.MP3"; | |
break; | |
case 2: | |
song = @"/game.MP3"; | |
break; | |
case 3: | |
song = @"/winner.MP3"; | |
default: | |
break; | |
} | |
if ([song isEqualToString:@""]) | |
{ | |
return ; | |
} | |
//NSLog(@"Playing song %d: %@",songNumber,song); | |
resourcePath = [resourcePath stringByAppendingString:song]; | |
//NSLog(@"Path to play: %@", resourcePath); | |
//Initialize our player pointing to the path to our resource | |
if (playing && player) | |
{ | |
[player stop]; | |
playing = false; | |
} | |
player = [[AVAudioPlayer alloc] initWithContentsOfURL: | |
[NSURL fileURLWithPath:resourcePath] error:&err]; | |
if( err ) | |
{ | |
//bail! | |
NSLog(@"Failed with reason: %@", [err localizedDescription]); | |
} | |
else | |
{ | |
//set our delegate and begin playback | |
player.delegate = self; | |
[player prepareToPlay]; | |
player.numberOfLoops = -1; //One Infinite Loop (music!) ;) | |
[player play]; | |
playing = true; | |
} | |
} | |
- (void)stopMusic | |
{ | |
if (playing) | |
{ | |
[player stop]; | |
playing = false; | |
} | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CodifyAppDelegate.h | |
// Codify | |
// | |
// Created by Simeon Nasilowski on 14/05/11. | |
// | |
// Copyright 2012 Two Lives Left Pty. Ltd. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// | |
// | |
// MineSweeper | |
// v1.7 | |
// | |
// Modifications by Reefwing Software | |
// Copyright Kintarla Pty Ltd, 2012. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
#import <GameKit/GameKit.h> | |
#import <iAd/iAd.h> | |
@class LuaState; | |
@class Project; | |
@interface CodifyAppDelegate : UIResponder<UIApplicationDelegate, ADBannerViewDelegate> | |
{ | |
BOOL displayingAds; | |
UIViewController* viewController; | |
Project* currentProject; | |
} | |
@property BOOL displayingAds; | |
@property (nonatomic, retain) IBOutlet UIWindow *window; | |
@property (nonatomic, retain) IBOutlet UIViewController* viewController; | |
@property (nonatomic, readonly) LuaState *scriptState; | |
+(Project*) currentProject; | |
+(CodifyAppDelegate*) delegate; | |
- (void) showRenderView:(BOOL)show animated:(BOOL)animated; | |
- (void) iAdDisplayStatusChanged; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CodifyAppDelegate.m | |
// Codify | |
// | |
// Created by Simeon Nasilowski on 14/05/11. | |
// | |
// Copyright 2012 Two Lives Left Pty. Ltd. | |
// | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
// | |
// | |
// MineSweeper | |
// v1.7 | |
// | |
// Modifications by Reefwing Software | |
// Copyright Kintarla Pty Ltd, 2012. All rights reserved. | |
// | |
#import "CodifyAppDelegate.h" | |
#import "CodifyScriptExecute.h" | |
#import "LuaState.h" | |
#import "Project.h" | |
#import "SharedRenderer.h" | |
#import "OALSimpleAudio.h" | |
@interface CodifyAppDelegate() | |
- (BOOL) migrateProjectAtPath:(NSString*)path toPath:(NSString*)destPath; | |
@property (nonatomic, retain) Project* currentProject; | |
@property (nonatomic, strong) UIView *contentView; | |
@end | |
@implementation CodifyAppDelegate | |
@synthesize scriptState; | |
@synthesize window=_window; | |
@synthesize viewController; | |
@synthesize currentProject; | |
@synthesize displayingAds; | |
BOOL deviceIsIpad = YES; | |
ADBannerView *_bannerView; | |
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions | |
{ | |
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; | |
// Create the Lua scripting state | |
scriptState = [[LuaState alloc] init]; | |
self.viewController = [SharedRenderer renderer]; //[[[UIViewController alloc] init] autorelease]; | |
self.window.rootViewController = self.viewController; | |
[self.window makeKeyAndVisible]; | |
NSURL *url = (NSURL *)[launchOptions valueForKey:UIApplicationLaunchOptionsURLKey]; | |
if ( [url isFileURL] ) | |
{ | |
NSLog(@"Codify opened with file: %@", url); | |
//[self openNewProject:url]; | |
} | |
[OALSimpleAudio sharedInstance]; | |
NSString* path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Project.codea"]; | |
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
NSString *destPath = [documentsDirectory stringByAppendingPathComponent:@"Project.codea"]; | |
NSError* error = nil; | |
//If there was an error copying it means we are upgrading the project rather than just installing it, | |
//So we need to ask the user what to do. | |
if(![[NSFileManager defaultManager] copyItemAtPath:path toPath:destPath error:&error]) | |
{ | |
if(![self migrateProjectAtPath:path toPath:destPath]) | |
{ | |
NSLog(@"Error migrating project"); | |
} | |
} | |
self.currentProject = [[[Project alloc] initWithPath:destPath validFileTypes:[NSArray arrayWithObjects:@"lua", @"plist", nil]] autorelease]; | |
[self showRenderView:YES animated:NO]; | |
// Check if device is an iPhone or iPad | |
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) | |
{ | |
deviceIsIpad = YES; | |
NSLog(@"App Delegate - iPad device detected."); | |
} | |
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) | |
{ | |
deviceIsIpad = NO; | |
CGSize result = [[UIScreen mainScreen] bounds].size; | |
if (result.height == 480) | |
{ | |
NSLog(@"App Delegate - iPhone/iPod Touch detected (< v5)."); | |
} | |
if (result.height == 568) | |
{ | |
NSLog(@"App Delegate - iPhone/iPod Touch detected (> v5)."); | |
} | |
} | |
// Initialise Banner Ads | |
_bannerView = [[ADBannerView alloc] init]; | |
_bannerView.delegate = self; | |
return YES; | |
} | |
- (void)applicationWillResignActive:(UIApplication *)application | |
{ | |
/* | |
Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. | |
Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. | |
*/ | |
if( [SharedRenderer renderer].animating ) | |
{ | |
[[SharedRenderer renderer] stopAnimation]; | |
} | |
} | |
- (void)applicationDidEnterBackground:(UIApplication *)application | |
{ | |
/* | |
Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. | |
If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. | |
*/ | |
} | |
- (void)applicationWillEnterForeground:(UIApplication *)application | |
{ | |
/* | |
Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. | |
*/ | |
[OALSimpleAudio sharedInstance]; | |
} | |
- (void)applicationDidBecomeActive:(UIApplication *)application | |
{ | |
/* | |
Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. | |
*/ | |
DBLog(@"Application did become active"); | |
[[SharedRenderer renderer] startAnimation]; | |
} | |
- (void)applicationWillTerminate:(UIApplication *)application | |
{ | |
/* | |
Called when the application is about to terminate. | |
Save data if appropriate. | |
See also applicationDidEnterBackground:. | |
*/ | |
if( [SharedRenderer renderer].animating ) | |
{ | |
[[SharedRenderer renderer] stopAnimation]; | |
} | |
} | |
#pragma mark - Show the renderer | |
- (void)showRenderView:(BOOL)show animated:(BOOL)animated | |
{ | |
if (show) | |
{ | |
[SharedRenderer renderer].project = currentProject; | |
[[SharedRenderer renderer] prepareViewForDisplay]; | |
//[self.viewController presentModalViewController:renderController animated:animated]; //renderController is viewController | |
[[SharedRenderer renderer] startAnimation]; | |
} | |
else | |
{ | |
//[self.viewController dismissModalViewControllerAnimated:animated]; //renderController is viewController | |
[[SharedRenderer renderer] stopAnimation]; | |
} | |
} | |
#pragma mark - Helper functions | |
+ (CodifyAppDelegate*) delegate | |
{ | |
return (CodifyAppDelegate*)[UIApplication sharedApplication].delegate; | |
} | |
+(Project*) currentProject | |
{ | |
return [self delegate].currentProject; | |
} | |
- (NSString*) versionForProjectAtPath:(NSString*)path | |
{ | |
NSString* infoPlistFile = [path stringByAppendingPathComponent:@"Info.plist"]; | |
NSDictionary* infoPlist = [NSDictionary dictionaryWithContentsOfFile:infoPlistFile]; | |
NSString* version = [[infoPlist objectForKey:@"Version"] description]; | |
if (!version) version = @"0"; | |
return version; | |
} | |
- (BOOL) migrateProjectAtPath:(NSString*)path toPath:(NSString*)destPath | |
{ | |
//NSString* oldVersion = [self versionForProjectAtPath:destPath]; | |
//NSString* newVersion = [self versionForProjectAtPath:path]; | |
//if ([oldVersion isEqualToString:newVersion]) | |
//{ | |
// return YES; | |
//} | |
NSArray* contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; | |
for(NSString* content in contents) | |
{ | |
NSArray* pathComponents = [content pathComponents]; | |
NSString* filename = [pathComponents lastObject]; | |
// if (![filename isEqualToString:@"Data.plist"]) //Just copy all the files across for now, | |
// { | |
//TODO: call into lua to check if this file should be updated from the previous version to the current one | |
NSString* destPathContent = [destPath stringByAppendingPathComponent:filename]; | |
NSString* pathContent = [path stringByAppendingPathComponent:filename]; | |
NSLog(@"Updating project file: %@",destPathContent); | |
[[NSFileManager defaultManager] removeItemAtPath:destPathContent error:nil]; | |
if(![[NSFileManager defaultManager] copyItemAtPath:pathContent toPath:destPathContent error:nil]) | |
{ | |
return NO; | |
} | |
// } | |
} | |
return YES; | |
} | |
#pragma mark - iAd Delegate Methods | |
- (void)layoutAnimated:(BOOL)animated | |
{ | |
// As of iOS 6.0, the banner will automatically resize itself based on its width. | |
// To support iOS 5.0 however, we continue to set the currentContentSizeIdentifier appropriately. | |
CGRect contentFrame = viewController.view.bounds; | |
BOOL landscape; | |
if (contentFrame.size.width < contentFrame.size.height) | |
{ | |
landscape = NO; | |
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait; | |
} | |
else | |
{ | |
landscape = YES; | |
_bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape; | |
} | |
CGRect bannerFrame = _bannerView.frame; | |
if (_bannerView.bannerLoaded) | |
{ | |
if (landscape || deviceIsIpad) | |
bannerFrame.origin.y = 0; | |
else | |
bannerFrame.origin.y = contentFrame.size.height - _bannerView.frame.size.height; | |
} | |
else | |
{ | |
bannerFrame.origin.y = contentFrame.size.height; | |
} | |
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{ | |
//_contentView.frame = contentFrame; | |
//[_contentView layoutIfNeeded]; | |
_bannerView.frame = bannerFrame; | |
}]; | |
} | |
- (void) iAdDisplayStatusChanged | |
{ | |
if (displayingAds) | |
{ | |
NSLog(@"Banner View Ad Shown."); | |
[_bannerView removeFromSuperview]; | |
[viewController.view addSubview:_bannerView]; | |
[viewController.view bringSubviewToFront:_bannerView]; | |
} | |
else | |
{ | |
NSLog(@"Banner View Ad Removed."); | |
[_bannerView removeFromSuperview]; | |
} | |
[self layoutAnimated: YES]; | |
} | |
- (void)bannerViewDidLoadAd:(ADBannerView *)banner | |
{ | |
[self layoutAnimated: YES]; | |
} | |
- (void)bannerView:(ADBannerView *)banner didFailToReceiveAdWithError:(NSError *)error | |
{ | |
[self layoutAnimated: YES]; | |
} | |
- (BOOL)bannerViewActionShouldBegin:(ADBannerView *)banner willLeaveApplication:(BOOL)willLeave | |
{ | |
[[SharedRenderer renderer] stopAnimation]; | |
return YES; | |
} | |
- (void)bannerViewActionDidFinish:(ADBannerView *)banner | |
{ | |
[[SharedRenderer renderer] startAnimation]; | |
} | |
#pragma mark - Memory Management | |
- (void)dealloc | |
{ | |
[scriptState release]; | |
[_window release]; | |
[_bannerView release]; | |
[super dealloc]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment