Skip to content

Instantly share code, notes, and snippets.

@manishkungwani
Created December 20, 2012 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manishkungwani/4343470 to your computer and use it in GitHub Desktop.
Save manishkungwani/4343470 to your computer and use it in GitHub Desktop.
AppDelegate.m for OlympusiPhone application
#import "AppDelegate.h"
#import "MainViewController.h"
#import <Cordova/CDVPlugin.h>
#import "VideoDataManager.h"
//#import "TestFlight.h"
#import "UAPush.h"
#import "UAirship.h"
@implementation AppDelegate
@synthesize window, viewController;
@synthesize mPlayer = _mPlayer;
@synthesize nvCtr = _nvCtr;
@synthesize vCtr = _vCtr;
- (id) init
{
/** If you need to do any extra app-specific initialization, you can do it here
* -jm
**/
NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
[cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
self.vCtr.view.frame=CGRectMake(0, 20, self.window.frame.size.width, self.window.frame.size.height-20);
self = [super init];
return self;
}
#pragma UIApplicationDelegate implementation
/**
* This is main kick off after the app inits, the views and Settings are setup here. (preferred - iOS4 and up)
*/
- (BOOL) application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//# Start DDLogger
[VideoDataManager OlympusLog:@"AppD1AppFinLaunch"];
_fileLogger = [[DDFileLogger alloc] init];
_fileLogger.maximumFileSize = (1024 * 64); // 64 KByte
_fileLogger.logFileManager.maximumNumberOfLogFiles = 1;
[_fileLogger rollLogFile];
[DDLog addLogger:_fileLogger];
[DDLog addLogger:[DDTTYLogger sharedInstance]];
//# End DDLogger
[VideoDataManager OlympusLog:@"AppD2AppFinLaunch"];
[VideoDataManager OlympusLog:@"AppD1AirshipLaunchOption"];
//Init Airship launch options
NSMutableDictionary *takeOffOptions = [[[NSMutableDictionary alloc] init] autorelease];
[takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];
// Create Airship singleton that's used to talk to Urban Airship servers.
// Please populate AirshipConfig.plist with your info from http://go.urbanairship.com
[UAirship takeOff:takeOffOptions];
[VideoDataManager OlympusLog:@"AppD2AirshipLaunchOption"];
// Register for notifications
[VideoDataManager OlympusLog:@"AppD1UAPush"];
[[UAPush shared]
registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
[VideoDataManager OlympusLog:@"AppD2UAPush"];
NSURL* url = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
NSString* invokeString = nil;
if (url && [url isKindOfClass:[NSURL class]]) {
invokeString = [url absoluteString];
NSLog(@"Temp_Child_Browser launchOptions = %@", url);
}
CGRect screenBounds = [[UIScreen mainScreen] bounds];
self.window = [[UIWindow alloc] initWithFrame:screenBounds];
self.window.autoresizesSubviews = YES;
self.viewController = [[MainViewController alloc] init];
self.viewController.useSplashScreen = YES;
self.viewController.wwwFolderName = @"www";
self.viewController.startPage = @"EntryPage.html";
self.viewController.invokeString = invokeString;
// NOTE: To control the view's frame size, override [self.viewController viewWillAppear:] in your view controller.
// check whether the current orientation is supported: if it is, keep it, rather than forcing a rotation
BOOL forceStartupRotation = YES;
UIDeviceOrientation curDevOrientation = [[UIDevice currentDevice] orientation];
if (UIDeviceOrientationUnknown == curDevOrientation) {
// UIDevice isn't firing orientation notifications yet… go look at the status bar
curDevOrientation = (UIDeviceOrientation)[[UIApplication sharedApplication] statusBarOrientation];
}
if (UIDeviceOrientationIsValidInterfaceOrientation(curDevOrientation)) {
if ([self.viewController supportsOrientation:curDevOrientation]) {
forceStartupRotation = NO;
}
}
if (forceStartupRotation) {
UIInterfaceOrientation newOrient;
if ([self.viewController supportsOrientation:UIInterfaceOrientationPortrait])
newOrient = UIInterfaceOrientationPortrait;
else if ([self.viewController supportsOrientation:UIInterfaceOrientationLandscapeLeft])
newOrient = UIInterfaceOrientationLandscapeLeft;
else if ([self.viewController supportsOrientation:UIInterfaceOrientationLandscapeRight])
newOrient = UIInterfaceOrientationLandscapeRight;
else
newOrient = UIInterfaceOrientationPortraitUpsideDown;
NSLog(@"AppDelegate forcing status bar to: %d from: %d", newOrient, curDevOrientation);
[[UIApplication sharedApplication] setStatusBarOrientation:newOrient];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
// Start downloads code
/*
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.itmystix.com/wp-content/uploads/2012/12/Logo-IT-Mystix-mini.png"]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"itm.png"];
AFDownloadRequestOperation *operation = [[AFDownloadRequestOperation alloc] initWithRequest:request targetPath:path shouldResume:YES];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Successfully downloaded file to %@", path);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];
[operation setProgressiveDownloadProgressBlock:^(NSInteger bytesRead, long long totalBytesRead, long long totalBytesExpected, long long totalBytesReadForFile, long long totalBytesExpectedToReadForFile) {
NSLog(@"Operation%i: bytesRead: %d", 1, bytesRead);
NSLog(@"Operation%i: totalBytesRead: %lld", 1, totalBytesRead);
NSLog(@"Operation%i: totalBytesExpected: %lld", 1, totalBytesExpected);
NSLog(@"Operation%i: totalBytesReadForFile: %lld", 1, totalBytesReadForFile);
NSLog(@"Operation%i: totalBytesExpectedToReadForFile: %lld", 1, totalBytesExpectedToReadForFile);
}];
//[operations addObject:operation];
*/
// End Downloads code
return YES;
}
// this happens while we are running ( in the background, or from within our own app )
// only valid if Temp_Child_Browser-Info.plist specifies a protocol to handle
- (BOOL) application:(UIApplication*)application handleOpenURL:(NSURL*)url
{
if (!url) {
return NO;
}
[VideoDataManager OlympusLog:@"AppD1handleOpenURL"];
// calls into javascript global function 'handleOpenURL'
NSString* jsString = [NSString stringWithFormat:@"handleOpenURL(\"%@\");", url];
[self.viewController.webView stringByEvaluatingJavaScriptFromString:jsString];
// all plugins will get the notification, and their handlers will be called
[[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:CDVPluginHandleOpenURLNotification object:url]];
[VideoDataManager OlympusLog:@"AppD2handleOpenURL"];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
[UAirship land];
}
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// Updates the device token and registers the token with UA
[VideoDataManager OlympusLog:@"AppD1DeviceToken"];
[[UAPush shared] registerDeviceToken:deviceToken];
[VideoDataManager OlympusLog:@"AppD2DeviceToken"];
}
- (NSUInteger) application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
/*
// IPhone doesn't support upside down by default, while the IPad does. Override to allow all orientations always, and let the root view controller decide whats allowed (the supported orientations mask gets intersected).
NSUInteger supportedInterfaceOrientations = (1 << UIInterfaceOrientationPortrait) | (1 << UIInterfaceOrientationLandscapeLeft) | (1 << UIInterfaceOrientationLandscapeRight) | (1 << UIInterfaceOrientationPortraitUpsideDown);
return supportedInterfaceOrientations;
*/
return YES;
}
- (NSString *)customDeviceIdentifierForUpdateManager:(BITUpdateManager *)updateManager {
#ifndef CONFIGURATION_AppStore
if ([[UIDevice currentDevice] respondsToSelector:@selector(uniqueIdentifier)])
return [[UIDevice currentDevice] performSelector:@selector(uniqueIdentifier)];
#endif
return nil;
}
// get the log content with a maximum byte size
- (NSString *) getLogFilesContentWithMaxSize:(NSInteger)maxSize {
[VideoDataManager OlympusLog:@"AppD1getLogFilesContentWithMaxSize"];
NSMutableString *description = [NSMutableString string];
NSArray *sortedLogFileInfos = [[_fileLogger logFileManager] sortedLogFileInfos];
NSUInteger count = [sortedLogFileInfos count];
// we start from the last one
for (NSUInteger index = count - 1; index >= 0; index--) {
DDLogFileInfo *logFileInfo = [sortedLogFileInfos objectAtIndex:index];
NSData *logData = [[NSFileManager defaultManager] contentsAtPath:[logFileInfo filePath]];
if ([logData length] > 0) {
NSString *result = [[NSString alloc] initWithBytes:[logData bytes]
length:[logData length]
encoding: NSUTF8StringEncoding];
[description appendString:result];
[result release];
}
}
if ([description length] > maxSize) {
description = (NSMutableString *)[description substringWithRange:NSMakeRange([description length]-maxSize-1, maxSize)];
}
[VideoDataManager OlympusLog:@"AppD2getLogFilesContentWithMaxSize"];
return description;
}
#pragma mark - BITCrashManagerDelegate
- (NSString *)applicationLogForCrashManager:(BITCrashManager *)crashManager {
[VideoDataManager OlympusLog:@"AppD1applicationLogForCrashManager"];
NSString *description = [self getLogFilesContentWithMaxSize:5000]; // 5000 bytes should be enough!
if ([description length] == 0) {
return nil;
} else {
return description;
}
[VideoDataManager OlympusLog:@"AppD2applicationLogForCrashManager"];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment