I am trying to create a plugin for region monitoring. Region Monitoring is starting fine but the function didfinishlaunching and didrecievelocalnotification are not being evoked. I am not sure why this is happening.
regionMonitoring.h
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#interface RegionMonitoringPlugin : NSObject <UIApplicationDelegate,CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
-(void)enterRegionNotify;
-(void)leaveRegionNotify;
-(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)raduis;
#end
regionMonitoring.mm
#import "RegionMonitoringPlugin.h"
#implementation RegionMonitoringPlugin
- (id) init
{
//if ( init == [super init]){
if (locationManager==nil){
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager setDistanceFilter:kCLDistanceFilterNone];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
return self;
}
-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
[self enterRegionNotify];
}
-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
[self leaveRegionNotify];
}
- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)regionwithError:(NSError *)error
{
NSLog(#"Location error %#, %#", error, #"Fill in the reason here");
}
-(void)leaveRegionNotify
{
NSLog(#"Starting region monitoring - check point 3");
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody= #"Region Left"; // ToAsk: What should be displayed
note.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:note];
[note release];
}
-(void)enterRegionNotify
{
UILocalNotification *note = [[UILocalNotification alloc] init];
note.alertBody= #"Region Left"; //ToAsk: what should be displayed ?
note.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:note];
[note release];
}
-(void)startMonitor:(float)latitude longitude:(float)longitude radius:(float)radius
{
NSLog(#"Starting region monitoring - check point 2");
[self leaveRegionNotify];
CLLocationCoordinate2D home;
home.latitude = latitude;
home.longitude = longitude;
CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:home radius:radius identifier:#"region"];
[locationManager startMonitoringForRegion:region desiredAccuracy:kCLLocationAccuracyBest];
[region release];
}
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
NSLog(#"Starting region monitoring - checkpoint 4");
if ([UIApplication sharedApplication].applicationState == UIApplicationStateActive) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Region Monitor Notification" message:notification.alertBody delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
[alertView release];
}
}
- (BOOL)application:(UIApplication *) application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"Test");
return TRUE;
}
#end
extern "C" {
static RegionMonitoringPlugin *regionMonitor;
// Unity callable function to start region monitoring
BOOL _startRegionMonitoring(float m_latitude,float m_longitude, float m_radius)
{
NSLog(#"Starting region monitoring");
if (![CLLocationManager regionMonitoringAvailable] || ![CLLocationManager regionMonitoringEnabled] )
return NO;
if (regionMonitor == nil){
regionMonitor = [[RegionMonitoringPlugin alloc] init] ;
}
[regionMonitor startMonitor:m_latitude longitude:m_longitude radius:m_radius];
return YES;
}
}
Unity Code for plugin : RegionMonitorMediater.h
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class RegionMonitoringMediator {
/*Interface to native implementation */
[DllImport ("__Internal")]
private static extern bool _startRegionMonitoring (float m_latitude,float m_longitude, float m_radius);
public static bool startRegionMonitoring (float latitude,float longitude, float radius)
{
/*Call plugin only when running on real device*/
if (Application.platform != RuntimePlatform.OSXEditor)
return _startRegionMonitoring ( latitude , longitude , radius);
else return false;
}
}
Calling region monitor
OnPress event I do
bool startedRM = RegionMonitoringMediator.startRegionMonitoring(77.0f,28.0f,10.0f);
There is only one UIApplicationDelegate allowed per app. When Unity3D builds your application for iPhone player a class AppController is generated which acts as interface.
This class is the place for inserting your code to call RegionMonitoringPlugin.
init should include a call to super at its start:
- (id) init
{
if (self = [super init])
{
// initialize everything else
}
return self;
}
note that we used assignment (=) operator, and not comparison (==) operator.
Related
I all I wonder if anybody can help me once again? I have moved my location coding from my View controller into an NSObject. I have then called this from the App Delegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//Updating Location
[Location sharedLocation];
//Timer for reloading the XML
recheckTimer = [NSTimer scheduledTimerWithTimeInterval:30 target:self selector:#selector(recheckLocation) userInfo:nil repeats:YES];
return YES:
}
I have set a timer for when I want this process ran again
-(void)recheckLocation
{
//Updating Location
[Location sharedLocation];
NSLog(#"Timer Triggered");
}
The only thing is when the timer triggers the sharedlocation does not get updated again?? Please can anybody help in advance? Many thanks, Jon.
#import "Location.h"
#implementation Location
#synthesize locationManager;
- (id)init {
self = [super init];
if(self) {
self.locationManager = [CLLocationManager new];
[self.locationManager setDelegate:self];
[self.locationManager setDistanceFilter:500];//Metres
[self.locationManager setHeadingFilter:90];
[self.locationManager startMonitoringSignificantLocationChanges];
}
return self;
}
+ (Location*)sharedLocation {
static Location* sharedLocation;
if(!sharedLocation) {
#synchronized(sharedLocation) {
sharedLocation = [Location new];
}
}
return sharedLocation;
}
//LOCATION CODING
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError' %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:#"Error" message:#"Failed to Get Your Current Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
//Resolve Web Address
webAddressResolved = [NSString stringWithFormat:#"XZYYFASDFA%f,%f.xml", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
NSLog(#"Address Resolved %#", webAddressResolved);
}
//Stop Location Manager
[locationManager stopMonitoringSignificantLocationChanges];
}
Well what yo defined here is a singleton of Location and the purpose of shared location does not seem to update the location as it says in the code comments
what the sharedInstance does is return a reference that is once initialized and is used the same everywhere in the whole code.It gives you the location instance in return, which you are not retrieving anywhere and using it.You just call it but not use it.
Define a method to update location and call it after getting the memory from the location using
Location *sharedLoc=[Location sharedLocation];
and call the method to update the location as
[sharedLoc updateLocation];
Location.h
-(void)updateLocation;
in Location .m
-(void)updateLocation
{
//Code for updation purpose
}
I am implementing accelerometer data in background using coremotion. I am getting the data in the background but value of x,y,z coordinates are not correct.
Here is my code.
- (void)applicationWillResignActive:(UIApplication *)application
{
[locationManager stopMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
}
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
BOOL isInBackground = NO;
if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
{
isInBackground = YES;
}
if (isInBackground)
{
[self sendBackgroundLocationToServer:newLocation];
}
}
-(void) sendBackgroundLocationToServer:(CLLocation *)location
{
UIApplication *application = [UIApplication sharedApplication];//Get the shared application instance
__block UIBackgroundTaskIdentifier background_task; //Create a task object
background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
NSAssert(background_task == UIBackgroundTaskInvalid, nil);
[application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
}];
NSOperationQueue *theQueue = [[NSOperationQueue alloc] init];
CMAccelerometerData *_returnedData = [[CMAccelerometerData alloc] init];
CMMotionManager *_motionManager = [[CMMotionManager alloc] init];
[_motionManager startAccelerometerUpdatesToQueue:theQueue withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {
int x = _motionManager.accelerometerData.acceleration.x;
int y = _motionManager.accelerometerData.acceleration.y;
int z = _motionManager.accelerometerData.acceleration.z;
NSLog(#"X: %i, Y: %i, z: %i", x, y,z);
//[self changeFilter:[HighpassFilter class]];
//[filter addAcceleration:acceleration];
const float violence = 1.20;
float magnitudeOfAcceleration = sqrt (x*x + y*y + z*z);
//float magnitudeOfAcceleration = sqrt (filter.x*filter.x + filter.y * filter.y + filter.z * filter.z);
BOOL shake = magnitudeOfAcceleration > violence;
if (shake)
{
step++;
NSLog(#"---------------");
}
NSUserDefaults *defalut = [NSUserDefaults standardUserDefaults];
NSLog(#"steps in background: %i",step );
if([defalut objectForKey:#"Stepscounting"]){
int f = [[defalut objectForKey:#"Stepscounting"] intValue];
step+=f;
}
[defalut setObject:[NSString stringWithFormat:#"%i",step] forKey:#"Stepscounting"];
}];
// AFTER ALL THE UPDATES, close the task
if (background_task != UIBackgroundTaskInvalid)
{
[[UIApplication sharedApplication] endBackgroundTask:background_task];
background_task = UIBackgroundTaskInvalid;
}
}
Using this i am getting the co-ordinates but values are not correct. Can any one tell me what is the reason behind this. I am not able to understand why is this happing.
Thanks in advance.
perhaps you should use float instead of int for your x,y,z.
2012-05-08 21:33:34.279 Passwordapp[384:fb03] Setting up a Fetched Results Controller for the Entity named Role
2012-05-08 21:33:34.292 Passwordapp[384:fb03] There's stuff in the database so skipping the import of default data
2012-05-08 21:33:34.293 Passwordapp[384:fb03] -[LoginViewController viewControllers]: unrecognized selector sent to instance 0x8e7d750
2012-05-08 21:33:34.301 Passwordapp[384:fb03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[LoginViewController viewControllers]: unrecognized selector sent to instance 0x8e7d750'
*** First throw call stack:
(0x1359022 0x1742cd6 0x135acbd 0x12bfed0 0x12bfcb2 0x2db6 0x390386 0x391274 0x3a0183 0x3a0c38 0x394634 0x1f93ef5 0x132d195 0x1291ff2 0x12908da 0x128fd84 0x128fc9b 0x390c65 0x392626 0x25cd 0x2535)
terminate called throwing an exception(lldb)
#import <UIKit/UIKit.h>
#import "passconst.h"
#interface LoginViewController : UIViewController <UITextFieldDelegate>
#property (nonatomic) BOOL pinValidated;
#end
#import "LoginViewController.h"
#import "KeychainWrapper.h"
#implementation LoginViewController
#synthesize pinValidated;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
// Helper method to congregate the Name and PIN fields for validation.
- (BOOL)credentialsValidated
{
NSString *name = [[NSUserDefaults standardUserDefaults] stringForKey:USERNAME];
BOOL pin = [[NSUserDefaults standardUserDefaults] boolForKey:PIN_SAVED];
if (name && pin) {
return YES;
} else {
return NO;
}
}
- (void)presentAlertViewForPassword
{
// 1
BOOL hasPin = [[NSUserDefaults standardUserDefaults] boolForKey:PIN_SAVED];
// 2
if (hasPin) {
// 3
NSString *user = [[NSUserDefaults standardUserDefaults] stringForKey:USERNAME];
NSString *message = [NSString stringWithFormat:#"What is %#'s password?", user];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Enter Password"
message:message
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Done", nil];
// 4
[alert setAlertViewStyle:UIAlertViewStyleSecureTextInput]; // Gives us the password field
alert.tag = kAlertTypePIN;
// 5
UITextField *pinField = [alert textFieldAtIndex:0];
pinField.delegate = self;
pinField.autocapitalizationType = UITextAutocapitalizationTypeWords;
pinField.tag = kTextFieldPIN;
[alert show];
} else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Setup Credentials"
message:#"Secure your Password list!"
delegate:self
cancelButtonTitle:#"Cancel"
otherButtonTitles:#"Done", nil];
// 6
[alert setAlertViewStyle:UIAlertViewStyleLoginAndPasswordInput];
alert.tag = kAlertTypeSetup;
UITextField *nameField = [alert textFieldAtIndex:0];
nameField.autocapitalizationType = UITextAutocapitalizationTypeWords;
nameField.placeholder = #"Name"; // Replace the standard placeholder text with something more applicable
nameField.delegate = self;
nameField.tag = kTextFieldName;
UITextField *passwordField = [alert textFieldAtIndex:1]; // Capture the Password text field since there are 2 fields
passwordField.delegate = self;
passwordField.tag = kTextFieldPassword;
[alert show];
}
}
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (alertView.tag == kAlertTypePIN) {
if (buttonIndex == 1 && self.pinValidated) { // User selected "Done"
[self performSegueWithIdentifier:#"Loginsegue" sender:self];
self.pinValidated = NO;
} else { // User selected "Cancel"
[self presentAlertViewForPassword];
}
} else if (alertView.tag == kAlertTypeSetup) {
if (buttonIndex == 1 && [self credentialsValidated]) { // User selected "Done"
[self performSegueWithIdentifier:#"Loginsegue" sender:self];
} else { // User selected "Cancel"
[self presentAlertViewForPassword];
}
}
}
#pragma mark - Text Field + Alert View Methods
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// 1
switch (textField.tag) {
case kTextFieldPIN: // We go here if this is the 2nd+ time used (we've already set a PIN at Setup).
NSLog(#"User entered PIN to validate");
if ([textField.text length] > 0) {
// 2
NSUInteger fieldHash = [textField.text hash]; // Get the hash of the entered PIN, minimize contact with the real password
// 3
if ([KeychainWrapper compareKeychainValueForMatchingPIN:fieldHash]) { // Compare them
NSLog(#"** User Authenticated!!");
self.pinValidated = YES;
} else {
NSLog(#"** Wrong Password :(");
self.pinValidated = NO;
}
}
break;
case kTextFieldName: // 1st part of the Setup flow.
NSLog(#"User entered name");
if ([textField.text length] > 0) {
[[NSUserDefaults standardUserDefaults] setValue:textField.text forKey:USERNAME];
[[NSUserDefaults standardUserDefaults] synchronize];
}
break;
case kTextFieldPassword: // 2nd half of the Setup flow.
NSLog(#"User entered PIN");
if ([textField.text length] > 0) {
NSUInteger fieldHash = [textField.text hash];
// 4
NSString *fieldString = [KeychainWrapper securedSHA256DigestHashForPIN:fieldHash];
NSLog(#"** Password Hash - %#", fieldString);
// Save PIN hash to the keychain (NEVER store the direct PIN)
if ([KeychainWrapper createKeychainValue:fieldString forIdentifier:PIN_SAVED]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:PIN_SAVED];
[[NSUserDefaults standardUserDefaults] synchronize];
NSLog(#"** Key saved successfully to Keychain!!");
}
}
break;
default:
break;
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.pinValidated = NO;
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self presentAlertViewForPassword];
[super viewDidAppear:animated];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"LoginViewController"];
[vc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentModalViewController:vc animated:YES];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
in your project you are trying to call [loginViewControllerObject viewController]; which is a navigation controller method
I am new to iphone development.I am creating a map application.I have toolbar below the mapview with a button on it.On clicking the button it displays as an alert to load the current location.In my button click even i hava given code to find current location
-(IBAction) gosearch : (id) sender{
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
For me it is not displaying the alert.What should i do?Please help me out. Thanks.
You need to have your controller class implement protocol CLLocationManagerDeligate. This will mean that it gets notified of errors or when the location is posted (for example – locationManager:didUpdateToLocation:fromLocation:)
Then you can pass on the long/lat and radius of view required to the MapView
Make sure for the alert view it looks like this-
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Current Location" message:#"Show Current Location?" delegate:nil cancelButtonTitle:#"Cancel" otherButtonTitles:#"OK"];
[alert show];
and also
- (void)alertView:(UIAlertView *)alertView willDismissWithButtonIndex:(NSInteger)buttonIndex
{
if (buttonIndex != [alertView cancelButtonIndex])
{
map.showsuserlocation = YES;
}
}
I'm not sure I fully understand your questions but here's what I have done to use the location manager.
First, I declare a class that implements CLLocationManagerDelegate
#interface GPSComponent <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
CLLocation *currentLocation;
}
Then, in the class I have:
- (id) init {
locationManager = [[CLLocationManager alloc] init];
// Provide the best possible accuracy (this is the default; just want to write some code).
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
// Must move at least 100 meters to get a new location update (default is get all notifications).
locationManager.distanceFilter = 100;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
#pragma mark -
#pragma mark CLLocationManagerDelegate methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// If you are about 400 miles south off the coast of Ghana, we might be ignoring your location information. We apologize.
// This is a lazy way to check for failure (in which case the struct is zeroed out).
if((fabs(newLocation.coordinate.latitude) > 0.001) || (fabs(newLocation.coordinate.longitude) > 0.001)) {
NSLog(#"Got location %f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
if (currentLocation != nil) {
[currentLocation release];
}
currentLocation = newLocation;
[currentLocation retain];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"Location Manager error");
}
Then, to display a map with the user's location:
// Pull out the longitude and latitude and invoke google maps
- (IBAction)mapItButtonPressed {
NSString *url = [NSString stringWithFormat: #"http://maps.google.com/maps?q=%f,%f", (float)currentLocation.coordinate.latitude, (float)currentLocation.coordinate.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
I already know how to use the CLLocationManager, so I could do it the hard way, with delegates and all that.
But I'd like to have a convenience method that just gets the current location, once, and blocks until it gets the result.
What I do is implement a singleton class to manage updates from core location. To access my current location, I do a CLLocation *myLocation = [[LocationManager sharedInstance] currentLocation]; If you wanted to block the main thread you could do something like this:
while ([[LocationManager sharedInstance] locationKnown] == NO){
//blocking here
//do stuff here, dont forget to have some kind of timeout to get out of this blocked //state
}
However, as it has been already pointed out, blocking the main thread is probably not a good idea, but this can be a good jumping off point as you are building something. You will also notice that the class I wrote checks the timestamp on location updates and ignores any that are old, to prevent the problem of getting stale data from core location.
This is the singleton class I wrote. Please note that it is a little rough around the edges:
#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>
#interface LocationController : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
CLLocation *currentLocation;
}
+ (LocationController *)sharedInstance;
-(void) start;
-(void) stop;
-(BOOL) locationKnown;
#property (nonatomic, retain) CLLocation *currentLocation;
#end
#implementation LocationController
#synthesize currentLocation;
static LocationController *sharedInstance;
+ (LocationController *)sharedInstance {
#synchronized(self) {
if (!sharedInstance)
sharedInstance=[[LocationController alloc] init];
}
return sharedInstance;
}
+(id)alloc {
#synchronized(self) {
NSAssert(sharedInstance == nil, #"Attempted to allocate a second instance of a singleton LocationController.");
sharedInstance = [super alloc];
}
return sharedInstance;
}
-(id) init {
if (self = [super init]) {
self.currentLocation = [[CLLocation alloc] init];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[self start];
}
return self;
}
-(void) start {
[locationManager startUpdatingLocation];
}
-(void) stop {
[locationManager stopUpdatingLocation];
}
-(BOOL) locationKnown {
if (round(currentLocation.speed) == -1) return NO; else return YES;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//if the time interval returned from core location is more than two minutes we ignore it because it might be from an old session
if ( abs([newLocation.timestamp timeIntervalSinceDate: [NSDate date]]) < 120) {
self.currentLocation = newLocation;
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertView *alert;
alert = [[UIAlertView alloc] initWithTitle:#"Error" message:[error description] delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alert show];
[alert release];
}
-(void) dealloc {
[locationManager release];
[currentLocation release];
[super dealloc];
}
#end
There is no such convenience and you shouldn't create your own. "Blocks until it gets the result" is extremely bad programming practice on a device like the iPhone. It can take seconds to retrieve a location; you should never make your users wait like that, and delegates ensure they don't.
There are no "convenience methods" unless you code them yourself, but you'd still need to implement the delegate methods in whatever custom code you use to make things "convenient."
The delegate pattern is there for a reason, and as delegates are a big part of Objective-C, I recommend you get comfortable with them.
I appreciated the answer by Brad Smith. Implementing it I discovered that one of the methods he employs is deprecated as of iOS6. To write code that will work with both iOS5 and iOS6, use the following:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
if (abs([[locations lastObject] timeIntervalSinceDate:[NSDate date]]) < 120) {
[self setCurrentLocation:[locations lastObject]];
}
}
// Backward compatibility with iOS5.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSArray *locations = [NSArray arrayWithObjects:oldLocation, newLocation, nil];
[self locationManager:manager didUpdateLocations:locations];
}
I simplified and combined multiple answers to where the location is only updated if it's valid.
It also works under OSX as well as iOS.
This assumes the use-case where the current location is suddenly desired by the user. If it takes more than 100 ms in this example, it's considered an error. (Assumes the GPS IC &| Wifi (Apple's Skyhook clone) is already fired up and has a good fix already.)
#import "LocationManager.h"
// wait up to 100 ms
CLLocation *location = [LocationManager currentLocationByWaitingUpToMilliseconds:100];
if (!location) {
NSLog(#"no location :(");
return;
}
// location is good, yay
https://gist.github.com/6972228