- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
works only if the UIView corresponding to class that implement CLLocationManagerDelegate is active.
If I changed the view it wont trigger didEnterRegion. Anyone can help me?
My code look like this
- (void)enableRegionMonitoring {
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
CLLocationCoordinate2D myMonLocation = CLLocationCoordinate2DMake(10.766699790955, 76.650101525879);
CLRegion *myRegion = [[CLRegion alloc]
initCircularRegionWithCenter:myMonLocation
radius:100
identifier:#"MyLoc"];
//NSLog(#"reg=%#",myRegion);
// Start monitoring for our CLRegion using best accuracy
[locationManager startMonitoringForRegion:myRegion
desiredAccuracy:kCLLocationAccuracyBest];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
NSLog(#"Entered Region");
NSDate *nowx=[NSDate date];
UILocalNotification *localNotification=[[UILocalNotification alloc]init];
if (!localNotification)
return;
NSDictionary *data = [NSDictionary dictionaryWithObject:#"qw" forKey:#"mykey"];
[localNotification setUserInfo:data];
[localNotification setFireDate:nowx];
[localNotification setTimeZone:[NSTimeZone defaultTimeZone]];
NSMutableString *message=[[NSMutableString alloc]init];
message = #"Local Not Triggered By didEnterRegion";
[localNotification setAlertBody:[nowx description]];
[localNotification setAlertAction:#"Open App"];
[localNotification setHasAction:YES];
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}
From looking at your code I guess you are using ARC, depending on your controller/view hierarchy your view and controller might get deallocated when you switch to a different view, when this happens the locationManager will be deallocated too.
Just to move the whole CLLocationManager code to your AppDelegate and let the AppDelegate be the CLLocationManager delegate. Where you are now calling "enableRegionMonitoring" you would call it on your AppDelegate instead. This will stay active even if the ViewController isn't visible anymore.
Related
How can I do the following please?
I have a button in my iPhone app that the user clicks to start GPS navigation. All I do when the button is clicked is get their location, and pass it to Google maps to do the rest.
However, as soon as I land on the screen where the button is, the application ask me the standard "This app would like to use your current location". But I dont want this to be asked as soon as I land on the screen. Instead, it should be asked when the button is clicked.
Here is how I handle the scenario in my views .m file:
- (void)viewDidLoad
{
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
And then when the button is clicked. Also tried to put the code from viewDidLoad here as can be seen from the commented out section. This works in that I dont get asked the question when the view loads, but then the GPS does not work once button is clicked. I get an error message on the app screen saying "The app was unable to establish a route to the nearest road". And the "This app would like to access your current location" only flashes up on the screen briefly.
- (IBAction)gpsNavigation:(id)sender
{
/*
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
*/
NSString *destAddress = #"52.269444, -9.708674";
NSString *url = [NSString stringWithFormat: #"http://maps.apple.com/maps?saddr=%f%f&daddr=%#",
locationManager.location.coordinate.latitude,
locationManager.location.coordinate.longitude,
[destAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
}
Please use commented code in gpsNavigation: method.
Your problem is that you get locationManager.location as soon as you call [locationManager startUpdatingLocation].
So your code can be as following.
- (IBAction)gpsNavigation:(id)sender
{
self.locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSString *destAddress = #"52.269444, -9.708674";
NSString *url = [NSString stringWithFormat: #"http://maps.apple.com/maps?saddr=%f%f&daddr=%#",
locationManager.location.coordinate.latitude,
locationManager.location.coordinate.longitude,
[destAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url]];
[locationManager stopUpdatingLocation];
}
As soon as you call [locationManager startUpdatingLocation] your app request locations and will ask the user for permission. So delay this call untill you need it (button pressed)
I have created a simple alarm notification App through which I can get real time, set alarm on or off, and play a single tone audio. But I need to play a sound which should start with a class VOID.
Below is the code:
To get and start alarm notification:
- (void)viewDidLoad {
[super viewDidLoad];
dateTimerPicker.date = [NSDate date];
}
- (void)presentMessage:(NSString *)message {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Hello!"
message:message
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
[alert show];
}
- (void)scheduleLocalNotificationWithDate:(NSDate *)fireDate {
UILocalNotification *notification = [[UILocalNotification alloc]init];
notification.fireDate = fireDate;
notification.alertBody = #"Time to wake up!!";
notification.soundName = #"PhoneOld.mp3";
[self playPause];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
- (IBAction)alarmSetOn:(id)sender{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone defaultTimeZone];
dateFormatter.timeStyle = NSDateFormatterShortStyle;
dateFormatter.dateStyle = NSDateFormatterShortStyle;
NSString *dateTimeString = [dateFormatter stringFromDate:dateTimerPicker.date];
NSLog(#"Alarm Set: %#", dateTimeString);
[self scheduleLocalNotificationWithDate:dateTimerPicker.date];
[self presentMessage:#"Alarm ON!"];
}
- (IBAction)alarmSetOff:(id)sender {
NSLog(#"Alarm Off");
[[UIApplication sharedApplication] cancelAllLocalNotifications];
[self presentMessage:#"Alarm OFF!"];
}
This is my VOID:
- (void)playPause {
RADAppDelegate *appDelegate = (RADAppDelegate *)[[UIApplication sharedApplication] delegate];
if (appDelegate.radiosound == 0){
[appDelegate.radiosound play];
} else {
[appDelegate.radiosound pause];
}
}
How can I set the alarm to start playing the radiosound if is rated 0, like a:
notification.soundName = [self playPause];
But I know this is a NSString.
You don't need to assign a sound name to scheduled notification, just invoke the playPause method and get the name of sound file from notification, as shown below and just assign it to NSString and set property to it in appDelegate and access it to play that file.
AppDelegate.h
#property (nonatomic,strong) NSString *nsStr_soundFile;
AppDelegate.m
#synthesize nsStr_soundFile;
- (void)application:(UIApplication *)application
didReceiveLocalNotification:(UILocalNotification *)notification {
//Give call to play sound method.
self.nsStr_soundFile=notification.soundName;
VOID *obj=[VOID alloc]init];
[obj playPause];
}
You can make a trick with opting out of iOS multitasking by setting in your app .plist file this key UIApplicationExitsOnSuspend to YES as written here
When an app opts out, it cycles between the not-running, inactive, and
active states and never enters the background or suspended states.
An app that runs in the pre-multitasking compatibility mode keeps
running when the user locks the device while the app is in the
foreground. All the app has to do is wait for the alarm time and
execute its custom code.
I am building a rails-backed iphone app that uses AFNetworking to create posts at specific locations. So the post model has lat/lng parameters that should be filled in with the client's current location.
At this point, the posts can be made, but the lat/lng comes up as null.
In my (save:) method I pass a conditional to see if a location was found- this is what is failing i.e. "No Location" is logged.
- (void)save:(id)sender {
[self getLocation];
NSArray *locations;
CLLocation *location = [locations objectAtIndex:0];
Post *post = [[Post alloc] init];
post.content = self.contentTextView.text;
post.photoData = UIImagePNGRepresentation(self.imageView.image);
[self.view endEditing:YES];
ProgressView *progressView = [ProgressView presentInWindow:self.view.window];
if (location) {
[post savePostAtLocation:location withBlock:^(CGFloat progress) {
[progressView setProgress:progress];
} completion:^(BOOL success, NSError *error) {
[progressView dismiss];
if (success) {
[self.navigationController popViewControllerAnimated:YES];
} else {
NSLog(#"ERROR: %#", error);
}
}];
} else {
NSLog(#"No Location");
}
}
I have also attempted to implement a locationManager like so
-(void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
[self getLocation];
}
-(CLLocation *) getLocation{
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.locationManager.distanceFilter = 80.0f;
[locationManager startUpdatingLocation];
CLLocation * location = [locationManager location];
return location;
}
I think ideally I would implement the savePostAtlocation in the CLLocationManagerDelegate where I could pass in the locations array like this:
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
{
CLLocation *location = [locations objectAtIndex:0 /* firstObject */];
if (location) {
[Post createPostAtLocation:location...
But I want to have the post created onSave so I am trying to identify the location but running into some problems..
How do I properly get the current location and pass it into the dictionary?
Any advice on this would be greatly appreciated. Thanks!
Looking at your code, I think you have a slight misunderstanding about how CLLocationManager is designed to work. It looks like you are trying to call [self getLocation] from inside locationManager:didUpdateLocations. This is not correct. Try something like this, inside your save method that is called when you press your button (I would remove the code that's currently in there while testing):
CLLocationManager * locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
self.locationManager.distanceFilter = 80.0f;
[locationManager startUpdatingLocation];
Then it will start generating location data. When that happens, the phone will automatically call locationManager:didUpdateLocations very rapidly. Then, in locationManager:didUpdateLocations you could use:
CLLocation * location = [manager location];
NSLog(#"%#", location);
To see your location data in the console.
What I have written here should get the phone generating location data for you. What you say about createPostAtLocation: in locationManager:didUpdateLocations is probably the correct way to go. When you get the location data, call [manager stopUpdatingLocation] to make the phone stop, then post the location data you got back to your server.
My application gets the user's location in the appDelegate and calls that location in the viewDidAppear method of some viewControllers. My problem is that the first time the viewController's load, there hasn't been enough time to get the user's location.
Here is my AppDelegate:
- (NSString *)getUserCoordinates
{
NSString *userCoordinates = [NSString stringWithFormat:#"latitude: %f longitude: %f",
locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
return userCoordinates;
}
- (NSString *)getUserLatitude
{
NSString *userLatitude = [NSString stringWithFormat:#"%f",
locationManager.location.coordinate.latitude];
return userLatitude;
}
- (NSString *)getUserLongitude
{
NSString *userLongitude = [NSString stringWithFormat:#"%f",
locationManager.location.coordinate.longitude];
return userLongitude;
}
Here is what I am using to call the location from the viewControllers:
- (void) viewDidAppear:(BOOL)animated
{
NSString *userLatitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLatitude];
NSString *userLongitude =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate
getUserLongitude];
}
Anyone have any ideas on how to fix? Thanks so much!
Try to use global variables here. It will be updated as your location updates and you will have updated location cordinates all time.
-(void)didUpdateLocation // location update method of CLLocation Manager class
{
// assign current ordinates values to global variables.
}
Not sure why you posted the same question two times... but see my answer at your other post,
Pass Coordinates from locationManager in appDelegate to viewController
In short, you need to implement the delegate methods of CoreLocation, as those methods are called when a new location is found. In those delegate methods, fire off an NSNotification (which ViewControllers) subscribe to in order to get the new user's location.
i'm trying to get a location and compare it with a location online all that happen in background
the method i create is working fine using background location service but after a minute or so the location icon in the status bar is getting disappear and the method is not getting called any longer
here is the code
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
double lat = newLocation.coordinate.latitude;
double lon = newLocation.coordinate.longitude;
NSURL * locationURL = [NSURL URLWithString:[NSString stringWithFormat:#"http://somedomainname.com/iphoneLocation?lat=%f&lon=%f",lat,lon]];
NSData * responseData = [NSData dataWithContentsOfURL:locationURL];
NSString* aStr;
aStr = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];
if ([aStr isEqualToString:#"out Of Any Knowen Range"] ){
UILocalNotification *notify =[[UILocalNotification alloc] init];
notify.alertAction = [[NSString alloc] initWithString: #"View"];
notify.fireDate=nil;
notify.alertBody = [NSString stringWithFormat:#"New Data Occured"];
notify.soundName = UILocalNotificationDefaultSoundName;
NSLog(#"Local notification should display");
[[UIApplication sharedApplication] presentLocalNotificationNow:notify];
}
}
and in the viewDid load i'm using something like this
locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
CLLocation *userLocation = [locationManager location];
so what is the wrong with it
You need to modify your AppName-Info.plist file by adding a key Required background modes with an item with value App registers for location updates. Another thing I think you should do as you connect online and this may not happen very fast, so the operation that you connect online, post the location and wait for response should start in another thread and meanwhile if you have received another location from CLLocationManager and your previous request has not finished yet to skip posting the new location...
I wonder if the Location Manager is getting released somewhere and therefore not sending any more updates.
Have you tried setting the location manager to a retained property in your view controller?
#property (nonatomic, strong) CLLocationManager *locationManager