123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- //
- // DBRouletteAppDelegate.m
- // DBRoulette
- //
- // Created by Brian Smith on 6/29/10.
- // Copyright Dropbox, Inc. 2010. All rights reserved.
- //
- #import "DBRouletteAppDelegate.h"
- #import <DropboxSDK/DropboxSDK.h>
- #import "PhotoViewController.h"
- #import "RootViewController.h"
- @interface DBRouletteAppDelegate () <DBSessionDelegate, DBNetworkRequestDelegate>
- @end
- @implementation DBRouletteAppDelegate
- @synthesize window;
- @synthesize navigationController;
- @synthesize rootViewController;
- #pragma mark -
- #pragma mark Application lifecycle
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-
- // Set these variables before launching the app
- NSString* appKey = @"APP_KEY";
- NSString* appSecret = @"APP_SECRET";
- NSString *root = nil; // Should be set to either kDBRootAppFolder or kDBRootDropbox
- // You can determine if you have App folder access or Full Dropbox along with your consumer key/secret
- // from https://dropbox.com/developers/apps
-
- // Look below where the DBSession is created to understand how to use DBSession in your app
-
- NSString* errorMsg = nil;
- if ([appKey rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) {
- errorMsg = @"Make sure you set the app key correctly in DBRouletteAppDelegate.m";
- } else if ([appSecret rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) {
- errorMsg = @"Make sure you set the app secret correctly in DBRouletteAppDelegate.m";
- } else if ([root length] == 0) {
- errorMsg = @"Set your root to use either App Folder of full Dropbox";
- } else {
- NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
- NSData *plistData = [NSData dataWithContentsOfFile:plistPath];
- NSDictionary *loadedPlist =
- [NSPropertyListSerialization
- propertyListFromData:plistData mutabilityOption:0 format:NULL errorDescription:NULL];
- NSString *scheme = [[[[loadedPlist objectForKey:@"CFBundleURLTypes"] objectAtIndex:0] objectForKey:@"CFBundleURLSchemes"] objectAtIndex:0];
- if ([scheme isEqual:@"db-APP_KEY"]) {
- errorMsg = @"Set your URL scheme correctly in DBRoulette-Info.plist";
- }
- }
-
- DBSession* session =
- [[DBSession alloc] initWithAppKey:appKey appSecret:appSecret root:root];
- session.delegate = self; // DBSessionDelegate methods allow you to handle re-authenticating
- [DBSession setSharedSession:session];
- [session release];
-
- [DBRequest setNetworkRequestDelegate:self];
- if (errorMsg != nil) {
- [[[[UIAlertView alloc]
- initWithTitle:@"Error Configuring Session" message:errorMsg
- delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]
- autorelease]
- show];
- }
-
- rootViewController.photoViewController = [[PhotoViewController new] autorelease];
- if ([[DBSession sharedSession] isLinked]) {
- navigationController.viewControllers =
- [NSArray arrayWithObjects:rootViewController, rootViewController.photoViewController, nil];
- }
-
- // Add the navigation controller's view to the window and display.
- window.rootViewController = navigationController;
- [window makeKeyAndVisible];
-
- NSURL *launchURL = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
- NSInteger majorVersion =
- [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];
- if (launchURL && majorVersion < 4) {
- // Pre-iOS 4.0 won't call application:handleOpenURL; this code is only needed if you support
- // iOS versions 3.2 or below
- [self application:application handleOpenURL:launchURL];
- return NO;
- }
- return YES;
- }
- - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
- if ([[DBSession sharedSession] handleOpenURL:url]) {
- if ([[DBSession sharedSession] isLinked]) {
- [navigationController pushViewController:rootViewController.photoViewController animated:YES];
- }
- return YES;
- }
-
- return NO;
- }
- - (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.
- */
- }
- - (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.
- */
- }
- - (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.
- */
- }
- - (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.
- See also applicationDidEnterBackground:.
- */
- }
- #pragma mark -
- #pragma mark Memory management
- - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
- /*
- Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
- */
- }
- - (void)dealloc {
- [navigationController release];
- [window release];
- [super dealloc];
- }
- #pragma mark -
- #pragma mark DBSessionDelegate methods
- - (void)sessionDidReceiveAuthorizationFailure:(DBSession*)session userId:(NSString *)userId {
- relinkUserId = [userId retain];
- [[[[UIAlertView alloc]
- initWithTitle:@"Dropbox Session Ended" message:@"Do you want to relink?" delegate:self
- cancelButtonTitle:@"Cancel" otherButtonTitles:@"Relink", nil]
- autorelease]
- show];
- }
- #pragma mark -
- #pragma mark UIAlertViewDelegate methods
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)index {
- if (index != alertView.cancelButtonIndex) {
- [[DBSession sharedSession] linkUserId:relinkUserId fromController:rootViewController];
- }
- [relinkUserId release];
- relinkUserId = nil;
- }
- #pragma mark -
- #pragma mark DBNetworkRequestDelegate methods
- static int outstandingRequests;
- - (void)networkRequestStarted {
- outstandingRequests++;
- if (outstandingRequests == 1) {
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
- }
- }
- - (void)networkRequestStopped {
- outstandingRequests--;
- if (outstandingRequests == 0) {
- [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
- }
- }
- @end
|