Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
250 lines (204 sloc) 9.55 KB
/* Copyright (c) 2018 BlackBerry 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.
*
*/
#import "AppDelegate.h"
@import GD.SecureStore.CoreData;
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[GDiOS sharedInstance] getWindow];
[[GDiOS sharedInstance] authorize: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 invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
- (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 active state; here you can undo many of the changes made on entering the background.
}
- (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.
}
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
// Saves changes in the application's managed object context before the application terminates.
[self saveContext];
}
#pragma mark - Core Data stack
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory
{
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
- (NSManagedObjectModel *)managedObjectModel {
// The managed object model for the application. It is a fatal error for the application not to be able to find and load its model.
if (_managedObjectModel != nil) {
return _managedObjectModel;
}
NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"Secure_Storage_Sample_Obj_C" withExtension:@"momd"];
_managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return _managedObjectModel;
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
// The persistent store coordinator for the application. This implementation creates and return a coordinator, having added the store for the application to it.
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
// Create the coordinator and store
_persistentStoreCoordinator = [[GDPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Secure_Storage_Sample_Obj_C.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data.";
if (![_persistentStoreCoordinator addPersistentStoreWithType:GDEncryptedIncrementalStoreType configuration:nil URL:storeURL options:nil error:&error]) {
// Report any error we got.
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code:9999 userInfo:dict];
// Replace this with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
- (NSManagedObjectContext *)managedObjectContext {
// Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.)
if (_managedObjectContext != nil) {
return _managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (!coordinator) {
return nil;
}
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
return _managedObjectContext;
}
#pragma mark - Core Data Saving support
- (void)saveContext {
NSManagedObjectContext *managedObjectContext = self.managedObjectContext;
if (managedObjectContext != nil) {
NSError *error = nil;
if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) {
// Replace this implementation with code to handle the error appropriately.
// abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
}
#pragma mark - Good Dynamics Delegate Methods
- (void)handleEvent:(GDAppEvent *)anEvent {
/* Called from _good when events occur, such as system startup. */
switch (anEvent.type)
{
case GDAppEventAuthorized:
{
[self onAuthorized:anEvent];
break;
}
case GDAppEventNotAuthorized:
{
[self onNotAuthorized:anEvent];
break;
}
case GDAppEventRemoteSettingsUpdate:
{
//A change to application-related configuration or policy settings.
break;
}
case GDAppEventServicesUpdate:
{
//A change to services-related configuration.
break;
}
case GDAppEventPolicyUpdate:
{
//A change to one or more application-specific policy settings has been received.
break;
}
case GDAppEventEntitlementsUpdate:
{
//A change to the entitlements data has been received.
break;
}
default:
{
NSLog(@"Unhandled Event");
break;
}
}
}
- (void)onNotAuthorized:(GDAppEvent *)anEvent {
/* Handle the Good Libraries not authorized event. */
switch (anEvent.code) {
case GDErrorActivationFailed:
case GDErrorProvisioningFailed:
case GDErrorPushConnectionTimeout:
case GDErrorSecurityError:
case GDErrorAppDenied:
case GDErrorAppVersionNotEntitled:
case GDErrorBlocked:
case GDErrorWiped:
case GDErrorRemoteLockout:
case GDErrorPasswordChangeRequired: {
// an condition has occured denying authorization, an application may wish to log these events
NSLog(@"onNotAuthorized %@", anEvent.message);
break;
}
case GDErrorIdleLockout: {
// idle lockout is benign & informational
break;
}
default:
NSAssert(false, @"Unhandled not authorized event");
break;
}
}
- (void)onAuthorized:(GDAppEvent *)anEvent {
/* Handle the Good Libraries authorized event. */
switch (anEvent.code) {
case GDErrorNone: {
if (!self.hasAuthorized) {
// launch application UI here
if (!self.hasAuthorized) {
// launch application UI here
NSString *storyboardName = nil;
storyboardName = @"Main";
UIStoryboard *uiStoryboard = [UIStoryboard storyboardWithName:storyboardName bundle:nil];
UIViewController *uiViewController = [uiStoryboard instantiateInitialViewController];
self.window.rootViewController = uiViewController;
self.hasAuthorized = YES;
//[self didAuthorize];
}
break;
}
default:
NSAssert(false, @"Authorized startup with an error");
break;
}
}
}
@end