Developer Area

root/apps/iphone/superbook/trunk/Classes/MyCLController.m @ 902

Revision 885, 7.4 kB (checked in by henri, 3 years ago)

Added (unfinished) TelPages? support. Refactored some main view controller code.

  • Property svn:executable set to *
Line 
1/*
2 
3 File: MyCLController.m
4 Abstract: Singleton class used to talk to CoreLocation and send results back to
5 the app's view controllers.
6 
7 */
8
9#import "MyCLController.h"
10#import "FriendsData.h"
11#import "SBContact.h"
12
13// This is a singleton class, see below
14static MyCLController *sharedCLDelegate = nil;
15
16@implementation MyCLController
17
18@synthesize delegate, locationManager, myCurrentLoc;
19
20- (id) init {
21        self = [super init];
22        if (self != nil) {
23                self.locationManager = [[[CLLocationManager alloc] init] autorelease];
24                self.locationManager.delegate = self; // Tells the location manager to send updates to this object
25                self.myCurrentLoc = [[[CLLocation alloc] initWithLatitude:0 longitude:0] autorelease];
26                if (! resolver) {
27                        resolver = [[[DnsResolver alloc] init] retain];
28                }
29        }
30        return self;
31}
32
33- (void)dealloc {
34        [super dealloc];
35        [resolver release];
36}
37
38// Called when the location is updated
39- (void)locationManager:(CLLocationManager *)manager
40    didUpdateToLocation:(CLLocation *)newLocation
41           fromLocation:(CLLocation *)oldLocation
42{       
43
44       
45        // Horizontal coordinates
46        if (signbit(newLocation.horizontalAccuracy)) {
47                // Negative accuracy means an invalid or unavailable measurement
48                [self.delegate gpsUpdate:nil];
49                return;
50        }
51   
52//      // Altitude (we don't care about it)
53//      if (signbit(newLocation.verticalAccuracy)) {
54//              // Negative accuracy means an invalid or unavailable measurement
55//      }
56   
57        self.myCurrentLoc = [[newLocation copy] autorelease];
58
59        // send the timestamp
60        [self.delegate gpsUpdate:self.myCurrentLoc];
61        return;
62}
63
64// Called when there is an error getting the location
65// TODO: Update this function to return the proper info in the proper UI fields
66- (void)locationManager:(CLLocationManager *)manager
67       didFailWithError:(NSError *)error
68{
69
70        NSMutableString *errorString = [[[NSMutableString alloc] init] autorelease];
71        BOOL shouldQuit;
72   
73        if ([error domain] == kCLErrorDomain) {
74       
75                // We handle CoreLocation-related errors here
76       
77                switch ([error code]) {
78                // This error code is usually returned whenever user taps "Don't Allow" in response to
79                // being told your app wants to access the current location. Once this happens, you cannot
80                // attempt to get the location again until the app has quit and relaunched.
81                //
82                // "Don't Allow" on two successive app launches is the same as saying "never allow". The user
83                // can reset this for all apps by going to Settings > General > Reset > Reset Location Warnings.
84                //
85                        case kCLErrorDenied:
86                                [errorString appendFormat:@"%@\n", NSLocalizedString(@"LocationDenied", @"Access to use your location was denied")];
87                                //[errorString appendFormat:@"%@\n", NSLocalizedString(@"AppWillQuit", nil)];
88                                shouldQuit = NO;
89                                break;
90               
91                // This error code is usually returned whenever the device has no data or WiFi connectivity,
92                // or when the location cannot be determined for some other reason.
93                //
94                // CoreLocation will keep trying, so you can keep waiting, or prompt the user.
95                                // We keep waiting so we don't nag the user every time
96                //
97                        case kCLErrorLocationUnknown:
98                                return;         // remove this if you want to alert the user
99                                [errorString appendFormat:@"%@\n", NSLocalizedString(@"LocationUnknown", @"Your location could not be determined")];
100                                //[errorString appendFormat:@"%@\n", NSLocalizedString(@"AppWillQuit", nil)];
101                                shouldQuit = NO;
102                                break;
103               
104                // We shouldn't ever get an unknown error code, but just in case...
105                //
106                        default:
107                                [errorString appendFormat:@"%@ %d\n", NSLocalizedString(@"GenericLocationError", @"Unknown location error code:"), [error code]];
108                                shouldQuit = NO;
109                                break;
110                }
111        } else {
112                // We handle all non-CoreLocation errors here
113                // (we depend on localizedDescription for localization)
114                [errorString appendFormat:@"Error domain: \"%@\"  Error code: %d\n", [error domain], [error code]];
115                [errorString appendFormat:@"Description: \"%@\"\n", [error localizedDescription]];
116                shouldQuit = NO;
117        }
118   
119        // Send the delegate the alert
120        [delegate newError:errorString shouldQuit:shouldQuit];
121}
122
123// Find all your friends' .tel records
124- (NSInteger) findFriendsTel {
125    // Iterate through the people with .tels and grab the locs if at all possible
126    // and store them in the shared instance of FriendsData
127
128        FriendsData *friends = [FriendsData sharedInstance];
129        NSNumber *personWithTel;
130        //NSInteger telStatus = FriendsDataSectionStatusUnknown;
131               
132        NSArray *peopleWithTel = [friends.peopleCurrentList allKeys];
133        int totalPeople = [friends.peopleCurrentList count];
134        int nP = 0;
135        NSNumber *progress = nil;
136        if (totalPeople == 0) {
137                // say we're done
138                [self.delegate updateProgressView:[NSNumber numberWithInt:1]];
139        } else {
140                for (personWithTel in peopleWithTel) {
141                        nP++;
142                        progress = [NSNumber numberWithFloat:(float)nP/(float)totalPeople];
143                        [self.delegate updateProgressView:progress];
144            SBContact *origContact = [friends.peopleCurrentList objectForKey:personWithTel];
145                        NSString *personName = [NSString stringWithString:origContact.name];
146                        NSString *telUrl = [NSString stringWithString:origContact.telDomain];
147                        NSMutableArray *locArray = [NSMutableArray arrayWithCapacity:1];
148                        NSError *error;
149                        NSUInteger locCount = [resolver getLOCForTel:telUrl inArray:locArray withError:&error];
150                        CLLocation *theLoc;
151                        CLLocationDistance theDist = 0.0;
152                        if (locCount == -1) { // Error
153                                //telStatus = FriendsDataSectionStatusUnknown;
154                                theLoc = nil;
155#ifdef DEBUG
156                                NSLog(@"Error finding location from domain: %@, error:%@\n", telUrl, [error localizedDescription]);
157#endif
158                        } else if (locCount == 0) {
159                                //telStatus = FriendsDataSectionWithoutLoc;
160                                theLoc = nil;
161#ifdef DEBUG
162                                NSLog(@"No LOC record in domain: %@\n", telUrl);
163#endif
164                        } else {
165                                if (locCount > 1) {
166#ifdef DEBUG
167                                        NSLog(@"Warning: %d LOC records in domain %@", locCount, telUrl);
168#endif
169                                }               
170                                //telStatus = FriendsDataSectionWithLoc;
171                                theLoc = (CLLocation *)[locArray objectAtIndex:0];
172                                theDist = [theLoc distanceFromLocation:self.myCurrentLoc];
173#ifdef DEBUG
174                                NSLog(@"Found LOC record in domain: %@\n", telUrl);
175#endif
176                        }
177                       
178            SBContact *personInfoRow = [SBContact new];
179            personInfoRow.name = personName;
180            personInfoRow.loc = theLoc;
181            personInfoRow.locTimestamp = [NSDate date];
182            personInfoRow.telDomain = telUrl;
183            personInfoRow.distanceFromDevice = theDist;                 
184                        [(NSMutableDictionary *)friends.peopleNewList setObject:personInfoRow forKey:personWithTel];
185            [personInfoRow release];
186                }
187        }
188       
189        // Now set lastFriendsUpdate to trigger a reload of the friends list data source
190        friends.lastFriendsUpdate = [NSDate date];
191    return EXIT_SUCCESS;
192}
193
194#pragma mark ---- singleton object methods ----
195
196// See "Creating a Singleton Instance" in the Cocoa Fundamentals Guide for more info
197
198+ (MyCLController *)sharedInstance {
199    if (sharedCLDelegate == nil) {
200        sharedCLDelegate = [[super allocWithZone:NULL] init];
201    }
202    return sharedCLDelegate;
203}
204
205+ (id)allocWithZone:(NSZone *)zone {
206    return [[self sharedInstance] retain];
207}
208
209- (id)copyWithZone:(NSZone *)zone
210{
211    return self;
212}
213
214- (id)retain {
215    return self;
216}
217
218- (unsigned)retainCount {
219    return NSUIntegerMax;  // denotes an object that cannot be released
220}
221
222- (oneway void)release {
223    //do nothing
224}
225
226- (id)autorelease {
227    return self;
228}
229
230@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