iOS 7 Simulator on Mac doesn't work with custom location (Also doesn't ask permission)

后端 未结 1 1373
醉梦人生
醉梦人生 2021-01-27 22:49

I\'m trying to make an iOS7 app that uses the current location of the device. I\'m using the iPhone simulator on my Mac, but I\'m having some problems. Every time my view that t

1条回答
  •  醉梦人生
    2021-01-27 23:14

    You need to get the newLocation from the delegate method didUpdateLocationToLocation:fromLocation:. Also implement didFailWithError delegate method. It takes some time before you start getting updated locations, hence the delegate call.

    The last location is usually cached, so it maybe wise to check location's timestamp and filter the old location out.

    Edit:

    This is the cleanest example I can provide. Start new project in Xcode, pick Single View application template, iPhone. Don't touch storyboard, just replace content of your ViewController.m with this and run in Simulator or device. If on Simulator, go to Debug and set some location and you will get coordinates in the console. I am also starting and stopping location updates when the view goes on or off screen.

    #import "ViewController.h"
    #import 
    
    @interface ViewController () 
    
    @property (strong, nonatomic) CLLocationManager *locationManager;
    
    @end
    
    @implementation ViewController
    
    #pragma mark - Location Manager delegate methods
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    
        if ([newLocation.timestamp timeIntervalSinceNow] >= -300.0) {
    
            NSLog(@"updated location with latitude %f longitude %f", newLocation.coordinate.longitude, newLocation.coordinate.latitude);
        }
    }
    
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
    
        [self.locationManager startUpdatingLocation];
    }
    
    - (void)viewWillDisappear:(BOOL)animated
    {
        [super viewWillDisappear:animated];
    
        [self.locationManager stopUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error     {
        if(error.code == kCLErrorDenied) {
    
            // alert user
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Access to location services is disabled"
                                                            message:@"You can turn Location Services on in Settings -> Privacy -> Location Services"
                                                           delegate:nil
                                                  cancelButtonTitle:@"OK"
                                                  otherButtonTitles:nil];
            [alertView show];
    
        } else if(error.code == kCLErrorLocationUnknown) {
            NSLog(@"Error: location unknown");
        } else {
            NSLog(@"Error retrieving location");
        }
    }
    
    #pragma mark - Location Manager getter
    
    - (CLLocationManager *)locationManager
    {
        if (!_locationManager) {
            _locationManager = [[CLLocationManager alloc] init];
            _locationManager.delegate = self;
            _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
            _locationManager.distanceFilter = 60.0;
        }
        return _locationManager;
    }
    
    @end
    

    0 讨论(0)
提交回复
热议问题