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








