root/apps/iphone/superbook/trunk/Classes/LocateThem_AppDelegate.m
@
864
| Revision 864, 6.7 kB (checked in by henri, 23 months ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // LocateThem_AppDelegate.m |
| 3 | // LocateThem |
| 4 | // |
| 5 | // Created by Henri Asseily on 8/13/08. |
| 6 | /* |
| 7 | Copyright (c) 2008-2009, Telnic Ltd. All rights reserved. |
| 8 | |
| 9 | Redistribution and use in source and binary forms, with or without modification, |
| 10 | are permitted provided that the following conditions are met: |
| 11 | |
| 12 | Redistributions of source code must retain the above copyright notice, this list of conditions |
| 13 | and the following disclaimer. Redistributions in binary form must reproduce the above copyright |
| 14 | notice, this list of conditions and the following disclaimer in the documentation and/or other |
| 15 | materials provided with the distribution. |
| 16 | Neither the name of the Telnic Ltd. nor the names of its contributors may be used to endorse or |
| 17 | promote products derived from this software without specific prior written permission. |
| 18 | THIS DOCUMENTATION IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS |
| 19 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 20 | AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 21 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 24 | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 25 | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #import "LocateThem_AppDelegate.h" |
| 29 | #import "LocateThem_ViewController.h" |
| 30 | #import "TelDomain.h" |
| 31 | #import "NetworkUtility.h" |
| 32 | |
| 33 | @implementation LocateThem_AppDelegate |
| 34 | |
| 35 | @synthesize window; |
| 36 | @synthesize navC; |
| 37 | |
| 38 | -(id)init { |
| 39 | if ((self = [super init])) { |
| 40 | // Get rid of old sqlite data in /Documents directory. Now we store the data in /Library/Caches |
| 41 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
| 42 | NSString *documentsDirectory = [paths objectAtIndex:0]; |
| 43 | NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory]; |
| 44 | NSString *pname; |
| 45 | while ((pname = [direnum nextObject])) { |
| 46 | if ([[pname pathExtension] isEqualToString:@"sqlite"]) { |
| 47 | [[NSFileManager defaultManager] removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:pname] error:nil]; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | // Make sure the domain cache directory exists |
| 52 | NSString *archivesDirectory = [TelDomain archivesDirectory]; |
| 53 | [[NSFileManager defaultManager] createDirectoryAtPath:archivesDirectory withIntermediateDirectories:YES attributes:nil error:NULL]; |
| 54 | |
| 55 | #ifdef DEBUG_REMOTE |
| 56 | // Enable remote debug logging for special debugging |
| 57 | int fd = open ([[documentsDirectory stringByAppendingPathComponent:@"debugLog"] cStringUsingEncoding:NSASCIIStringEncoding], |
| 58 | O_CREAT | O_TRUNC | O_WRONLY, |
| 59 | S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); |
| 60 | if (fd > -1) { |
| 61 | NSLog(@"DEBUG_REMOTE defined: logging debug info to %@", [documentsDirectory stringByAppendingPathComponent:@"debugLog"]); |
| 62 | close (STDERR_FILENO); |
| 63 | dup (fd); |
| 64 | close (fd); |
| 65 | NSLog(@"========== STARTING SESSION: %@ ==========", [UIDevice currentDevice].uniqueIdentifier); |
| 66 | } else { |
| 67 | NSLog(@"ERROR CREATING DEBUG LOGFILE"); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | //Notifications for tile requests. This code allows for a class to know when a tile is requested and retrieved |
| 72 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 73 | selector:@selector(tileRequested:) name:@"RMTileRequested" object:nil ]; |
| 74 | [[NSNotificationCenter defaultCenter] addObserver:self |
| 75 | selector:@selector(tileRetrieved:) name:@"RMTileRetrieved" object:nil ]; |
| 76 | } |
| 77 | |
| 78 | return self; |
| 79 | } |
| 80 | |
| 81 | -(void)tileRequested:(NSNotification *)notification { |
| 82 | [[NetworkUtility sharedInstance] networkwillActivate:YES]; |
| 83 | } |
| 84 | |
| 85 | -(void)tileRetrieved:(NSNotification *)notification { |
| 86 | [[NetworkUtility sharedInstance] networkwillActivate:NO]; |
| 87 | } |
| 88 | |
| 89 | - (void)applicationDidFinishLaunching:(UIApplication *)application { |
| 90 | |
| 91 | // Override point for customization after app launch |
| 92 | |
| 93 | // Start the networking by opening a write stream |
| 94 | NetworkUtility *nUtil = [NetworkUtility sharedInstance]; |
| 95 | if ([nUtil startNetwork] == NotReachable) { |
| 96 | NSDictionary *dict = [NSDictionary dictionaryWithObject:NSLocalizedString(@"Network Error: No Internet Connection", @"Network Error: No Internet Connection") |
| 97 | forKey:NSLocalizedDescriptionKey]; |
| 98 | NSError *error; |
| 99 | error = [NSError errorWithDomain:@"org.telnic.superbook.tel" code:666 userInfo:dict]; |
| 100 | CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:@"" |
| 101 | message:[error localizedDescription] |
| 102 | delegate:nil |
| 103 | shouldQuit:NO |
| 104 | cancelButtonTitle:NSLocalizedString(@"OK", @"OK") |
| 105 | otherButtonTitles:nil]; |
| 106 | [alertView show]; |
| 107 | [alertView release]; |
| 108 | } |
| 109 | |
| 110 | LocateThem_ViewController *lVC = [[[LocateThem_ViewController alloc] initWithNibName:@"MainView" bundle:[NSBundle mainBundle]] autorelease]; |
| 111 | navC = [[UINavigationController alloc] initWithRootViewController:lVC]; |
| 112 | navC.navigationBar.barStyle = UIBarStyleBlackOpaque; |
| 113 | navC.navigationBar.tintColor = [UIColor colorWithRed:0.392 green:0.110 blue:0.498 alpha:1]; |
| 114 | [window addSubview:navC.view]; |
| 115 | [window makeKeyAndVisible]; |
| 116 | |
| 117 | } |
| 118 | |
| 119 | - (void)applicationDidBecomeActive:(UIApplication *)application { |
| 120 | NetworkUtility *nUtil = [NetworkUtility sharedInstance]; |
| 121 | [nUtil startNetwork]; |
| 122 | } |
| 123 | |
| 124 | - (void)applicationDidEnterBackground:(UIApplication *)application { |
| 125 | |
| 126 | #ifdef DEBUG_REMOTE |
| 127 | // Send the debug log to the app admin |
| 128 | NSLog(@"========== CLOSING SESSION: %@ ==========", [UIDevice currentDevice].uniqueIdentifier); |
| 129 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
| 130 | NSString *documentsDirectory = [paths objectAtIndex:0]; |
| 131 | NSString *debugLogPath = [documentsDirectory stringByAppendingPathComponent:@"debugLog"]; |
| 132 | NSString *debugString = [[NSString alloc] initWithContentsOfFile:debugLogPath]; |
| 133 | NSMutableURLRequest *request = [[NSMutableURLRequest alloc] |
| 134 | initWithURL:[NSURL URLWithString:@"http://telnames.appspot.com/debug"] |
| 135 | cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData |
| 136 | timeoutInterval:20 |
| 137 | ]; |
| 138 | [request setHTTPMethod:@"POST"]; |
| 139 | NSString *request_body = [NSString stringWithFormat:@"debugString=%@", |
| 140 | [debugString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] |
| 141 | ]; |
| 142 | [request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]]; |
| 143 | NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:nil startImmediately:YES]; |
| 144 | [theConnection autorelease]; |
| 145 | [request release]; |
| 146 | #endif |
| 147 | |
| 148 | } |
| 149 | |
| 150 | @end |
Note: See TracBrowser
for help on using the browser.








