iOS improvements

- Recursively copy disks directory out of bundle to a R/W location in Documents directory
    - Allow background audio to play
This commit is contained in:
Aaron Culliney 2016-07-17 18:38:43 -07:00
parent 635075f34b
commit b4925292e1

View File

@ -8,7 +8,8 @@
* Copyright 2015 Aaron Culliney
*
*/
#import <AVFoundation/AVFoundation.h>
#import "AppDelegate.h"
#import "EAGLView.h"
#import "common.h"
@ -19,7 +20,9 @@
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)options
{
// Override point for customization after application launch.
[self recursivelyCopyBundleResources];
[self activateAudioSession];
[self.window makeKeyAndVisible];
@ -32,10 +35,7 @@
- (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.
[self deactivateAudioSession];
EAGLView *view = (EAGLView *)self.window.rootViewController.view;
[view pauseRendering];
[view pauseEmulation];
@ -43,18 +43,12 @@
- (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, called instead of applicationWillTerminate: when the user quits.
EAGLView *view = (EAGLView *)self.window.rootViewController.view;
[view pauseRendering];
[view pauseEmulation];
// ...
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of transition from the background to the inactive state: here you can undo many of the changes
// made on entering the background.
[self activateAudioSession];
EAGLView *view = (EAGLView *)self.window.rootViewController.view;
[view resumeRendering];
[view resumeEmulation];
@ -62,16 +56,11 @@
- (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.
EAGLView *view = (EAGLView *)self.window.rootViewController.view;
[view resumeRendering];
[view resumeEmulation];
// ...
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. See also applicationDidEnterBackground:
EAGLView *view = (EAGLView *)self.window.rootViewController.view;
self.window.rootViewController.view = [[UIView alloc] init];
@ -89,5 +78,103 @@
LOG("...");
}
#pragma mark - resource management
- (void)activateAudioSession
{
NSError *error = nil;
[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryAmbient error:&error];
if (error)
{
ERRLOG("Error setting AVAudioSessionCategoryAmbient : %s", [[error description] UTF8String]);
error = nil;
}
[[AVAudioSession sharedInstance] setActive:YES error:&error];
if (error)
{
ERRLOG("Error activating AVAudioSession : %s", [[error description] UTF8String]);
error = nil;
}
}
- (void)deactivateAudioSession
{
NSError *error = nil;
[[AVAudioSession sharedInstance] setActive:NO error:&error];
if (error)
{
ERRLOG("Error deactivating AVAudioSession : %s", [[error description] UTF8String]);
}
}
#pragma mark - Export bundle resources to a location where we have R/W access
- (void)recursivelyCopyBundleResources
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, /*expandTilde:*/YES);
NSString *documentsDir = [paths objectAtIndex:0];
// copy disks directory into apple2ix directory
NSString *apple2ix = @"apple2ix";
NSString *disks = @"disks";
NSString *apple2ixDirString = [documentsDir stringByAppendingPathComponent:apple2ix];
data_dir = strdup([apple2ixDirString UTF8String]);
NSString *documentsPath = [apple2ixDirString stringByAppendingPathComponent:disks];
NSString *resourcesPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:disks];
[self copyDirectoryFrom:resourcesPath to:documentsPath];
}
- (void)copyDirectoryFrom:(NSString *)resourcesPath to:(NSString *)documentsPath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
if (![fileManager fileExistsAtPath:documentsPath])
{
[fileManager createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:&error];
}
if (error)
{
ERRLOG("Could not create directory. Error: %s", [[error description] UTF8String]);
return;
}
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:resourcesPath error:&error];
if (error)
{
ERRLOG("Could not list contents of bundle. Error: %s", [[error description] UTF8String]);
return;
}
for (NSString *s in fileList)
{
NSString *documentsFile = [documentsPath stringByAppendingPathComponent:s];
NSString *resourcesFile = [resourcesPath stringByAppendingPathComponent:s];
BOOL isDirectory = false;
if (![fileManager fileExistsAtPath:resourcesFile isDirectory:&isDirectory])
{
assert(NO);
continue;
}
if (isDirectory)
{
[self copyDirectoryFrom:resourcesFile to:documentsFile];
}
else
{
[fileManager copyItemAtPath:resourcesFile toPath:documentsFile error:&error];
if (error)
{
ERRLOG("Could not copy file. Error: %s", [[error description] UTF8String]);
}
}
}
}
@end