DBRouletteAppDelegate.m 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // DBRouletteAppDelegate.m
  3. // DBRoulette
  4. //
  5. // Created by Brian Smith on 6/29/10.
  6. // Copyright Dropbox, Inc. 2010. All rights reserved.
  7. //
  8. #import "DBRouletteAppDelegate.h"
  9. #import <DropboxSDK/DropboxSDK.h>
  10. #import "PhotoViewController.h"
  11. #import "RootViewController.h"
  12. @interface DBRouletteAppDelegate () <DBSessionDelegate, DBNetworkRequestDelegate>
  13. @end
  14. @implementation DBRouletteAppDelegate
  15. @synthesize window;
  16. @synthesize navigationController;
  17. @synthesize rootViewController;
  18. #pragma mark -
  19. #pragma mark Application lifecycle
  20. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  21. // Set these variables before launching the app
  22. NSString* appKey = @"APP_KEY";
  23. NSString* appSecret = @"APP_SECRET";
  24. NSString *root = nil; // Should be set to either kDBRootAppFolder or kDBRootDropbox
  25. // You can determine if you have App folder access or Full Dropbox along with your consumer key/secret
  26. // from https://dropbox.com/developers/apps
  27. // Look below where the DBSession is created to understand how to use DBSession in your app
  28. NSString* errorMsg = nil;
  29. if ([appKey rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) {
  30. errorMsg = @"Make sure you set the app key correctly in DBRouletteAppDelegate.m";
  31. } else if ([appSecret rangeOfCharacterFromSet:[[NSCharacterSet alphanumericCharacterSet] invertedSet]].location != NSNotFound) {
  32. errorMsg = @"Make sure you set the app secret correctly in DBRouletteAppDelegate.m";
  33. } else if ([root length] == 0) {
  34. errorMsg = @"Set your root to use either App Folder of full Dropbox";
  35. } else {
  36. NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"];
  37. NSData *plistData = [NSData dataWithContentsOfFile:plistPath];
  38. NSDictionary *loadedPlist =
  39. [NSPropertyListSerialization
  40. propertyListFromData:plistData mutabilityOption:0 format:NULL errorDescription:NULL];
  41. NSString *scheme = [[[[loadedPlist objectForKey:@"CFBundleURLTypes"] objectAtIndex:0] objectForKey:@"CFBundleURLSchemes"] objectAtIndex:0];
  42. if ([scheme isEqual:@"db-APP_KEY"]) {
  43. errorMsg = @"Set your URL scheme correctly in DBRoulette-Info.plist";
  44. }
  45. }
  46. DBSession* session =
  47. [[DBSession alloc] initWithAppKey:appKey appSecret:appSecret root:root];
  48. session.delegate = self; // DBSessionDelegate methods allow you to handle re-authenticating
  49. [DBSession setSharedSession:session];
  50. [session release];
  51. [DBRequest setNetworkRequestDelegate:self];
  52. if (errorMsg != nil) {
  53. [[[[UIAlertView alloc]
  54. initWithTitle:@"Error Configuring Session" message:errorMsg
  55. delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]
  56. autorelease]
  57. show];
  58. }
  59. rootViewController.photoViewController = [[PhotoViewController new] autorelease];
  60. if ([[DBSession sharedSession] isLinked]) {
  61. navigationController.viewControllers =
  62. [NSArray arrayWithObjects:rootViewController, rootViewController.photoViewController, nil];
  63. }
  64. // Add the navigation controller's view to the window and display.
  65. window.rootViewController = navigationController;
  66. [window makeKeyAndVisible];
  67. NSURL *launchURL = [launchOptions objectForKey:UIApplicationLaunchOptionsURLKey];
  68. NSInteger majorVersion =
  69. [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];
  70. if (launchURL && majorVersion < 4) {
  71. // Pre-iOS 4.0 won't call application:handleOpenURL; this code is only needed if you support
  72. // iOS versions 3.2 or below
  73. [self application:application handleOpenURL:launchURL];
  74. return NO;
  75. }
  76. return YES;
  77. }
  78. - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
  79. if ([[DBSession sharedSession] handleOpenURL:url]) {
  80. if ([[DBSession sharedSession] isLinked]) {
  81. [navigationController pushViewController:rootViewController.photoViewController animated:YES];
  82. }
  83. return YES;
  84. }
  85. return NO;
  86. }
  87. - (void)applicationWillResignActive:(UIApplication *)application {
  88. /*
  89. 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.
  90. 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.
  91. */
  92. }
  93. - (void)applicationDidEnterBackground:(UIApplication *)application {
  94. /*
  95. 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.
  96. If your application supports background execution, called instead of applicationWillTerminate: when the user quits.
  97. */
  98. }
  99. - (void)applicationWillEnterForeground:(UIApplication *)application {
  100. /*
  101. 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.
  102. */
  103. }
  104. - (void)applicationDidBecomeActive:(UIApplication *)application {
  105. /*
  106. 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.
  107. */
  108. }
  109. - (void)applicationWillTerminate:(UIApplication *)application {
  110. /*
  111. Called when the application is about to terminate.
  112. See also applicationDidEnterBackground:.
  113. */
  114. }
  115. #pragma mark -
  116. #pragma mark Memory management
  117. - (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
  118. /*
  119. Free up as much memory as possible by purging cached data objects that can be recreated (or reloaded from disk) later.
  120. */
  121. }
  122. - (void)dealloc {
  123. [navigationController release];
  124. [window release];
  125. [super dealloc];
  126. }
  127. #pragma mark -
  128. #pragma mark DBSessionDelegate methods
  129. - (void)sessionDidReceiveAuthorizationFailure:(DBSession*)session userId:(NSString *)userId {
  130. relinkUserId = [userId retain];
  131. [[[[UIAlertView alloc]
  132. initWithTitle:@"Dropbox Session Ended" message:@"Do you want to relink?" delegate:self
  133. cancelButtonTitle:@"Cancel" otherButtonTitles:@"Relink", nil]
  134. autorelease]
  135. show];
  136. }
  137. #pragma mark -
  138. #pragma mark UIAlertViewDelegate methods
  139. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)index {
  140. if (index != alertView.cancelButtonIndex) {
  141. [[DBSession sharedSession] linkUserId:relinkUserId fromController:rootViewController];
  142. }
  143. [relinkUserId release];
  144. relinkUserId = nil;
  145. }
  146. #pragma mark -
  147. #pragma mark DBNetworkRequestDelegate methods
  148. static int outstandingRequests;
  149. - (void)networkRequestStarted {
  150. outstandingRequests++;
  151. if (outstandingRequests == 1) {
  152. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  153. }
  154. }
  155. - (void)networkRequestStopped {
  156. outstandingRequests--;
  157. if (outstandingRequests == 0) {
  158. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  159. }
  160. }
  161. @end