Issue with CLlocationManager in iOS 8 - objective-c

When I configure the CLlocationManager with this code:
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
if([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
// Or [self.locationManager requestWhenInUseAuthorization];
} [self.locationManager startUpdatingLocation];
}
and the set the location delegate
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your 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) {
self.textLongitude.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
self.textLatitude.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
}
DonĀ“t send the alert for authorized, but the Info.plist is complete
when go to settings and change in settings -> privacy -> localize and change the status for "always", and restart the app this status is clean.

The NSLocationAlwaysUsageDescription is misspelled in the plist.

Related

CLLocationManager not getting location

Hi I am having trouble using CLLocationManager to find the current user location. Here is my code:
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
}
- (IBAction)pressed:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation];
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(#"%.8f", currentLocation.coordinate.longitude);
NSLog(#"%.8f", currentLocation.coordinate.latitude);
}
}
When I run this code, nothing happens at all. There is no reaction in the debugger. As I understand, didUpdateToLocation is deprecated in iOS6 but I thought it was still usable. If anyone can tell me what I am doing wrong or suggest an alternative please let me know. Thanks!
How'd you define stuff in your .h file? I assume like:
#import <UIKit/UIKit.h>
#import <CoreLocation/CLLocationManager.h>
#import <CoreLocation/CLLocationManagerDelegate.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
And the main .m file I ran:
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[self.locationManager startUpdatingLocation];
return YES;
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Error" message:#"Failed to Get Your Location" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[self.locationManager stopUpdatingLocation];
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
NSLog(#"%.8f", currentLocation.coordinate.longitude);
NSLog(#"%.8f", currentLocation.coordinate.latitude);
}
}
Here's the output on the simulated iOS:
2014-08-12 13:36:28.933 tester[5577:60b] Application windows are expected to have a root view controller at the end of application launch
2014-08-12 13:36:35.237 tester[5577:60b] didUpdateToLocation: <+37.78******,-122.40******> +/- 5.00m (speed -1.00 mps / course -1.00) # 8/12/14, 1:36:35 PM Central Daylight Time
2014-08-12 13:36:35.237 tester[5577:60b] -122.40******
2014-08-12 13:36:35.238 tester[5577:60b] 37.78******

Region monitoring for current location iOS 7

I want to make an app that gives me an Enter & Exit alert message for current position with 10 meters radius. Here is my code:
-(void)startLocationServices
{
if (self.locationManager == nil)
{
self.locationManager = [CLLocationManager new];
}
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
[self.locationManager setDistanceFilter:kCLDistanceFilterNone];
[self.locationManager startUpdatingLocation];
}
-(void)stopLocationServices
{
[self.locationManager stopUpdatingLocation];
[self.locationManager setDelegate:nil];
//self.locationUpdateTextView.text = #"Location Service stop";
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self startLocationServices];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
self.locationUpdateTextView.text = [#"New Location: \n\n" stringByAppendingFormat:#"%# \n \nCurrent Position\nLatitude: %f \nLongitude: %f", [locations lastObject], self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude);
CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center radius:10.0 identifier:#"Test"];
[self.locationManager startMonitoringForRegion:region];
[self stopLocationServices];
}
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Status" message:#"You Enter the region" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alertView show];
}
- (void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Status" message:#"You Exit the region" delegate:self cancelButtonTitle:#"ok" otherButtonTitles:nil, nil];
[alertView show];
}
- (void) locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
self.availabilityTextView.text = [#"\nError:" stringByAppendingFormat:#"%#", error];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
I call [self stopLocationServices]; in didUpdateLocations method so that it grabs the user's current position, set the radius and then stop. When I run the app and move 10 meters it is not giving any alert message though I cross the 10 meters radius. Rather, I get it randomly especially when I restart the app after crossing the 10 meters radius. I know the cell tower issue in this case, but I am not sure my thinking is accurately workable in this scenario. If anyone understands my problem and knows any solution please share that with me.

Circle overlay follow user location

How can i make my custom circle overlay follow as a subview the current user location in real time? I have managed to display my circle overlay and get to the center of userlocation but when user moves (gps) the overlay stays still and does not followuser. How do i fix that? Thank you in advance..
- (void)viewDidLoad {
[super viewDidLoad];
[self.mapView setRegion:MKCoordinateRegionMake(self.location.coordinate, MKCoordinateSpanMake(0.02, 0.02))];
[self configureOverlay];
[[self locationManager] startUpdatingLocation];
}
in configureOverlay:
CircleOverlay *overlay = [[CircleOverlay alloc] initWithCoordinate:self.location.coordinate radius:self.radius];
[self.mapView addOverlay:overlay];
GeoQueryAnnotation *annotation = [[GeoQueryAnnotation alloc] initWithCoordinate:self.location.coordinate radius:self.radius];
[self.mapView addAnnotation:annotation];
I also tried using:
CLLocation *location = _locationManager.location;
CLLocationCoordinate2D coordinate = [location coordinate];
and replacing coordinate with self.location.coordinate
any advise would be appreciated.Thank you
Tell your class you are going to implement CLLocationManagerDelegate
#interface YourClass () <CLLocationManagerDelegate>
Then add the following methods:
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//You can change the overlay coordinates here. Even with an animation if you want.
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ? #"Access Denied" : #"Unknown Error";
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error getting Location"message:errorType delegate:nil cancelButtonTitle:#"Okay" otherButtonTitles:nil];
[alert show];
}
The locationManager:didUpdateToLocation... method will be triggered each time the location manager detects a change in the user's position.
Cheers.
EDIT
I forgot about something very important. In your viewDidLoad method add your class as the delegate:
self.locationmanager.delegate = self;

How to check if geolocation is enabled on iPhone?

How to check if localization is enable on iPhone?
I need to check if geolocation is enable in viewDidLoad method.
This is my viewDidLoad method:
- (void)viewDidLoad {
[super viewDidLoad];
// 1. Check connection
[self performSelector:#selector(checkConnection)];
// 2. Loader (activity indicator) ...
progressViewController = [[ProgressViewController alloc] initWithNibName:#"ProgressViewController" bundle:nil];
[self.view addSubview:progressViewController.view];
// 3. Active geolocation
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[self startGps];
// 4. Data tableView
NSMutableArray *arrEvents = [[NSMutableArray alloc] init];
[[ListaEventiSingleton sharedListaEventi] setArrEvents:arrEvents];
[arrEvents release];
// 5. remove loader (activity indicator)
[progressViewController.view removeFromSuperview];
}
//delegate localization
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (newLocation.horizontalAccuracy < 0) {
locality = #"Non riesco a localizzarti..\n Refresha!";
}
else {
CLLocationCoordinate2D here = newLocation.coordinate;
latitudine = [NSString stringWithFormat:#"%f", here.latitude];
longitudine = [NSString stringWithFormat:#"%f", here.longitude];
[self getAddressFromGmaps];
[self stopGps];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:#selector(stopUpdatingLocalizzazione) object:nil];
}
labelLocality.text = locality;
// 4. Load data for the table view
[self performSelectorOnMainThread:#selector(loadEvents) withObject:nil waitUntilDone:YES];
[tableViewEvents reloadData];
// 5. remove activity indicator
[progressViewController.view removeFromSuperview];
}
I think you want [CLLocationManager locationServicesEnabled].

wait for CLLocationManager to finish before tweeting

I want to wait for latitude.text and longtitude.text to be filled in before sending a tweet, this code works fine, but I would rather not put the tweeting part in locationManager because I also want to sometimes update the current location without sending a tweet. How can I make sure the txt gets filled in before sending the tweet without doing this?
- (IBAction)update {
latitude.text =#"";
longitude.text =#"";
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D location = [newLocation coordinate];
latitude.text = [NSString stringWithFormat: #"%f", location.latitude];
longitude.text = [NSString stringWithFormat: #"%f", location.longitude];
TwitterRequest * t = [[TwitterRequest alloc] init];
t.username = #"****";
t.password = #"****";
[twitterMessageText resignFirstResponder];
loadingActionSheet = [[UIActionSheet alloc] initWithTitle:#"Posting To Twitter..." delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[loadingActionSheet showInView:self.view];
[t statuses_update:twitterMessageText.text andLat:latitude.text andLong:longitude.text delegate:self requestSelector:#selector(status_updateCallback:)];
twitterMessageText.text=#"";
}
You could register listeners in your class that implements the CLLocationManagerDelegate. For example,
#interface GPS : NSObject <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
NSMutableArray *listeners;
}
- (void) addListener:(id<GPSListener>)listener;
#end
#implementation GPS
- (IBAction)update {
latitude.text =#"";
longitude.text =#"";
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocationCoordinate2D location = [newLocation coordinate];
latitude.text = [NSString stringWithFormat: #"%f", location.latitude];
longitude.text = [NSString stringWithFormat: #"%f", location.longitude];
// Inform the listeners.
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
for ( id<GPSListener> listener in listeners )
{
#try
{
[listener onLocationChangeForLatitude:latitude.text longitude:longitude];
}
#catch (NSException *e)
{
NSLog(#"Unhandled exception in onLocationChange");
}
}
[pool release];
}
- (void) addListener:(id<GPSListener>)listener
{
[listeners addObject:listener];
}
#end
The you can have a set of listeners that register themselves with your GPS object.
#protocol GPSListener {
- (void) onLocationChangeForLatitude:(NSString *)latitude longitude:(NSString *)longitude;
}
So now you can have a TweetGPSListener
#interface TweetGPSListener : NSObject <GPSListener> {
}
#end
#implementation TweetGPSListener
- (void) onLocationChangeForLatitude:(NSString *)latitude longitude:(NSString *)longitude {
TwitterRequest * t = [[TwitterRequest alloc] init];
t.username = #"****";
t.password = #"****";
[twitterMessageText resignFirstResponder];
loadingActionSheet = [[UIActionSheet alloc] initWithTitle:#"Posting To Twitter..." delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[loadingActionSheet showInView:self.view];
[t statuses_update:twitterMessageText.text andLat:latitude andLong:longitude delegate:self requestSelector:#selector(status_updateCallback:)];
twitterMessageText.text=#"";
}
#end
So for cases that you do not want to tweet either do not register the listener, remove the listener or add some logic to the TweetListener to know when it is appropriate to tweet.