I've got a class method that wants to use CLLocationManager and some of its delegate methods.
What is the best way to access the delegate methods from the class method, since I don't have a true instance level "self"? I could instantiate a self and use as the delegate, which would let the delegate methods run, but doesn't show how to get the data out. What's the best approach?
// desired end function, which runs a block when location is found
[SFGeoPoint geoPointForCurrentLocationInBackground:^(SFGeoPoint *geoPoint, NSError *error) {
if (!error) {
// do something with the new geoPoint
NSLog(@"GeoPoint: %@", geoPoint);
}
}];
// SFGeoPoint class, key points
static CLLocationManager *_locationManager = nil;
// get geo point for current location and call block with it
+ (void) geoPointForCurrentLocationInBackground:( void ( ^ )( SFGeoPoint*, NSError* ) ) locationFound {
SFGeoPoint *point = [[SFGeoPoint alloc] init];
_locationManager = [[CLLocationManager alloc] init];
// ?????????
_locationManager.delegate = self; // this gives a warning about incompatible pointer type assigning Delegate from Class;
_locationManager.delegate = point; // could work, but how to get feedback?
_locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[_locationManager startUpdatingLocation];
[_locationManager startUpdatingLocation];
locationFound(point, nil);
}
/////////// Core Location Delegate
+ (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
[_locationManager stopUpdatingLocation];
if (_locationBlock) {
_locationBlock(newLocation);
}
}
id:= (id)self. Conceptually the two are not that much different, just that in this case you are using the class object itself as a delegate instead of some instance, saving you the need to create another object.