|
| 1 | +/* |
| 2 | + File: AppDelegate.m |
| 3 | +
|
| 4 | + Contains: Main app controller. |
| 5 | +
|
| 6 | + Written by: DTS |
| 7 | +
|
| 8 | + Copyright: Copyright (c) 2009-2012 Apple Inc. All Rights Reserved. |
| 9 | +
|
| 10 | + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. |
| 11 | + ("Apple") in consideration of your agreement to the following |
| 12 | + terms, and your use, installation, modification or |
| 13 | + redistribution of this Apple software constitutes acceptance of |
| 14 | + these terms. If you do not agree with these terms, please do |
| 15 | + not use, install, modify or redistribute this Apple software. |
| 16 | +
|
| 17 | + In consideration of your agreement to abide by the following |
| 18 | + terms, and subject to these terms, Apple grants you a personal, |
| 19 | + non-exclusive license, under Apple's copyrights in this |
| 20 | + original Apple software (the "Apple Software"), to use, |
| 21 | + reproduce, modify and redistribute the Apple Software, with or |
| 22 | + without modifications, in source and/or binary forms; provided |
| 23 | + that if you redistribute the Apple Software in its entirety and |
| 24 | + without modifications, you must retain this notice and the |
| 25 | + following text and disclaimers in all such redistributions of |
| 26 | + the Apple Software. Neither the name, trademarks, service marks |
| 27 | + or logos of Apple Inc. may be used to endorse or promote |
| 28 | + products derived from the Apple Software without specific prior |
| 29 | + written permission from Apple. Except as expressly stated in |
| 30 | + this notice, no other rights or licenses, express or implied, |
| 31 | + are granted by Apple herein, including but not limited to any |
| 32 | + patent rights that may be infringed by your derivative works or |
| 33 | + by other works in which the Apple Software may be incorporated. |
| 34 | +
|
| 35 | + The Apple Software is provided by Apple on an "AS IS" basis. |
| 36 | + APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING |
| 37 | + WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, |
| 38 | + MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING |
| 39 | + THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN |
| 40 | + COMBINATION WITH YOUR PRODUCTS. |
| 41 | +
|
| 42 | + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, |
| 43 | + INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 44 | + TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 45 | + DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY |
| 46 | + OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION |
| 47 | + OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY |
| 48 | + OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR |
| 49 | + OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF |
| 50 | + SUCH DAMAGE. |
| 51 | +
|
| 52 | +*/ |
| 53 | + |
| 54 | +#import "AppDelegate.h" |
| 55 | + |
| 56 | +#import "NetworkManager.h" |
| 57 | + |
| 58 | +@interface AppDelegate () |
| 59 | + |
| 60 | +// things for IB |
| 61 | + |
| 62 | +@property (nonatomic, strong, readwrite) IBOutlet UIWindow * window; |
| 63 | +@property (nonatomic, strong, readwrite) IBOutlet UITabBarController * tabs; |
| 64 | + |
| 65 | +@end |
| 66 | + |
| 67 | +@implementation AppDelegate |
| 68 | +{ |
| 69 | + char _networkOperationCountDummy; |
| 70 | +} |
| 71 | + |
| 72 | +@synthesize window = _window; |
| 73 | +@synthesize tabs = _tabs; |
| 74 | + |
| 75 | ++ (void)initialize |
| 76 | + // Set up our default preferences. We can't do this in -applicationDidFinishLaunching: |
| 77 | + // because it's too late; the view controller's -viewDidLoad method has already run |
| 78 | + // by the time applicationDidFinishLaunching: is called. |
| 79 | +{ |
| 80 | + if ([self class] == [AppDelegate class]) { |
| 81 | + NSString * initialDefaultsPath; |
| 82 | + NSDictionary * initialDefaults; |
| 83 | + |
| 84 | + initialDefaultsPath = [[NSBundle mainBundle] pathForResource:@"InitialDefaults" ofType:@"plist"]; |
| 85 | + assert(initialDefaultsPath != nil); |
| 86 | + |
| 87 | + initialDefaults = [NSDictionary dictionaryWithContentsOfFile:initialDefaultsPath]; |
| 88 | + assert(initialDefaults != nil); |
| 89 | + |
| 90 | + // If we're running on the device certain defaults don't make any sense |
| 91 | + // (specifically, the upload defaults, which reference localhost), so |
| 92 | + // we nix them. |
| 93 | + |
| 94 | + #if ! TARGET_IPHONE_SIMULATOR |
| 95 | + { |
| 96 | + NSMutableDictionary * initialDefaultsChanged; |
| 97 | + |
| 98 | + initialDefaultsChanged = [initialDefaults mutableCopy]; |
| 99 | + assert(initialDefaultsChanged != nil); |
| 100 | + |
| 101 | + [initialDefaultsChanged setObject:@"" forKey:@"CreateDirURLText"]; |
| 102 | + [initialDefaultsChanged setObject:@"" forKey:@"PutURLText"]; |
| 103 | + |
| 104 | + initialDefaults = initialDefaultsChanged; |
| 105 | + } |
| 106 | + #endif |
| 107 | + |
| 108 | + [[NSUserDefaults standardUserDefaults] registerDefaults:initialDefaults]; |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | ++ (AppDelegate *)sharedAppDelegate |
| 113 | +{ |
| 114 | + return (AppDelegate *) [UIApplication sharedApplication].delegate; |
| 115 | +} |
| 116 | + |
| 117 | +- (void)applicationDidFinishLaunching:(UIApplication *)application |
| 118 | +{ |
| 119 | + #pragma unused(application) |
| 120 | + assert(self.window != nil); |
| 121 | + assert(self.tabs != nil); |
| 122 | + |
| 123 | + [[NetworkManager sharedInstance] addObserver:self forKeyPath:@"networkOperationCount" options:NSKeyValueObservingOptionInitial context:&self->_networkOperationCountDummy]; |
| 124 | + |
| 125 | + self.tabs.selectedIndex = [[NSUserDefaults standardUserDefaults] integerForKey:@"currentTab"]; |
| 126 | +} |
| 127 | + |
| 128 | +- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController |
| 129 | +{ |
| 130 | + assert(tabBarController == self.tabs); |
| 131 | + #pragma unused(tabBarController) |
| 132 | + #pragma unused(viewController) |
| 133 | + [[NSUserDefaults standardUserDefaults] setInteger:(NSInteger) self.tabs.selectedIndex forKey:@"currentTab"]; |
| 134 | +} |
| 135 | + |
| 136 | +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context |
| 137 | +{ |
| 138 | + if (context == &self->_networkOperationCountDummy) { |
| 139 | + [UIApplication sharedApplication].networkActivityIndicatorVisible = ([NetworkManager sharedInstance].networkOperationCount != 0); |
| 140 | + } else { |
| 141 | + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +@end |
0 commit comments