Developer Area

root/apps/iphone/superbook/trunk/Classes/LocateThem_AppDelegate.m @ 738

Revision 474, 6.4 kB (checked in by henri, 5 years ago)

Added optional remote debugging feature

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 "NetworkUtility.h"
31
32@implementation LocateThem_AppDelegate
33
34@synthesize window;
35@synthesize navC;
36
37-(id)init {
38        if (self = [super init]) {
39                // Get rid of old sqlite data in /Documents directory. Now we store the data in /Library/Caches
40                NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
41                NSString *documentsDirectory = [paths objectAtIndex:0];
42                NSDirectoryEnumerator *direnum = [[NSFileManager defaultManager] enumeratorAtPath:documentsDirectory];
43                NSString *pname;
44                while (pname = [direnum nextObject]) {
45                        if ([[pname pathExtension] isEqualToString:@"sqlite"]) {
46                                [[NSFileManager defaultManager] removeItemAtPath:[documentsDirectory stringByAppendingPathComponent:pname] error:nil];
47                        }
48                }
49               
50#ifdef DEBUG_REMOTE
51                // Enable remote debug logging for special debugging
52                int fd = open ([[documentsDirectory stringByAppendingPathComponent:@"debugLog"] cStringUsingEncoding:NSASCIIStringEncoding],
53                                           O_CREAT | O_TRUNC | O_WRONLY,
54                                           S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
55                if (fd > -1) {
56                        NSLog(@"DEBUG_REMOTE defined: logging debug info to %@", [documentsDirectory stringByAppendingPathComponent:@"debugLog"]);
57                        close (STDERR_FILENO);
58                        dup (fd);
59                        close (fd);
60                        NSLog(@"========== STARTING SESSION: %@ ==========", [UIDevice currentDevice].uniqueIdentifier);
61                } else {
62                        NSLog(@"ERROR CREATING DEBUG LOGFILE");
63                }
64#endif         
65                                               
66                //Notifications for tile requests.  This code allows for a class to know when a tile is requested and retrieved
67                [[NSNotificationCenter defaultCenter] addObserver:self
68                                                                                                 selector:@selector(tileRequested:) name:@"RMTileRequested" object:nil ];
69                [[NSNotificationCenter defaultCenter] addObserver:self
70                                                                                                 selector:@selector(tileRetrieved:) name:@"RMTileRetrieved" object:nil ];
71        }
72       
73        return self;
74}
75
76-(void)tileRequested:(NSNotification *)notification {
77        [[NetworkUtility sharedInstance] networkwillActivate:YES];
78}
79
80-(void)tileRetrieved:(NSNotification *)notification {
81        [[NetworkUtility sharedInstance] networkwillActivate:NO];
82}
83
84- (void)applicationDidFinishLaunching:(UIApplication *)application {   
85       
86        // Override point for customization after app launch   
87       
88        // Start the networking by opening a write stream
89        NetworkUtility *nUtil = [NetworkUtility sharedInstance];
90        if ([nUtil startNetwork] == NotReachable) {
91                NSDictionary *dict = [NSDictionary dictionaryWithObject:NSLocalizedString(@"Network Error: No Internet Connection", @"Network Error: No Internet Connection")
92                                                                                                                 forKey:NSLocalizedDescriptionKey];
93                NSError *error;
94                error = [NSError errorWithDomain:@"org.telnic.superbook.tel" code:666 userInfo:dict];
95                CustomAlertView *alertView = [[CustomAlertView alloc] initWithTitle:@""
96                                                                                                                                        message:[error localizedDescription]
97                                                                                                                                   delegate:nil
98                                                                                                                                 shouldQuit:NO
99                                                                                                                  cancelButtonTitle:NSLocalizedString(@"OK", @"OK")
100                                                                                                                  otherButtonTitles:nil];
101                [alertView show];
102                [alertView release];
103        }
104       
105        LocateThem_ViewController *lVC = [[[LocateThem_ViewController alloc] initWithNibName:@"MainView" bundle:[NSBundle mainBundle]] autorelease];
106        navC = [[UINavigationController alloc] initWithRootViewController:lVC];
107        navC.navigationBar.barStyle = UIBarStyleBlackOpaque;
108        navC.navigationBar.tintColor = [UIColor colorWithRed:0 green:0.15 blue:0.3 alpha:1];
109        [window addSubview:navC.view];
110        [window makeKeyAndVisible];
111
112}
113
114- (void)applicationDidBecomeActive:(UIApplication *)application {
115        NetworkUtility *nUtil = [NetworkUtility sharedInstance];
116        [nUtil startNetwork];
117}
118
119- (void)applicationWillTerminate:(UIApplication *)application {
120
121#ifdef DEBUG_REMOTE
122        // Send the debug log to the app admin
123        NSLog(@"========== CLOSING SESSION: %@ ==========", [UIDevice currentDevice].uniqueIdentifier);
124        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
125        NSString *documentsDirectory = [paths objectAtIndex:0];
126        NSString *debugLogPath = [documentsDirectory stringByAppendingPathComponent:@"debugLog"];
127        NSString *debugString = [[NSString alloc] initWithContentsOfFile:debugLogPath];
128        NSMutableURLRequest *request = [[NSMutableURLRequest alloc]
129                                                                        initWithURL:[NSURL URLWithString:@"http://telnames.appspot.com/debug"]
130                                                                        cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
131                                                                        timeoutInterval:20
132        ];
133        [request setHTTPMethod:@"POST"];
134        NSString *request_body = [NSString stringWithFormat:@"debugString=%@",
135                                                          [debugString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
136                                                          ];
137        [request setHTTPBody:[request_body dataUsingEncoding:NSUTF8StringEncoding]];
138        NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:nil startImmediately:YES];
139        [theConnection autorelease];
140        [request release];
141#endif         
142       
143}
144
145@end
Note: See TracBrowser for help on using the browser.
Telnic
Search This Site
Partners
Neustar
ICANN
Main site | WHOIS | Sell .tel | FAQ | Archived Site | About Telnic | Contact Us