应用程序崩溃(位置GPS区域监控)

时间:2015-08-12 09:17:36

标签: ios objective-c xcode gps

应用程序在Xcode中启动时崩溃:它不会生成错误报告,而是在一个线程中终止(自我> UIViewController> _locationManager)。

以下是MapViewController.m

的代码

#import "MapViewController.h"

@interface MapViewController ()


@end
@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //Monitoring a Region
    [self.locationManager requestWhenInUseAuthorization];
    [self.locationManager startUpdatingLocation];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)initializeLocationServices
{
    NSLog(@"Started location services");
    
    _locationManager = [[CLLocationManager alloc] init];
    _locationManager.delegate = self;
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    _locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    
    [_locationManager startUpdatingLocation]; // to show authorisation popup
}

-(CLCircularRegion*)createRegion
{
    // Test coordinates
    CLLocationDegrees latitude = 50;
    CLLocationDegrees longitude = -1;
    CLLocationDistance radius = 50; // meters;
    
    // If radius is too large, registration fails automatically, so limit the radius to the maximum value
    if (radius > _locationManager.maximumRegionMonitoringDistance) {
        radius = _locationManager.maximumRegionMonitoringDistance;
    }
    
    CLCircularRegion* region = [[CLCircularRegion alloc] initWithCenter:CLLocationCoordinate2DMake(latitude, longitude) radius:radius identifier:@"Clue1"];
    
    region.notifyOnEntry = YES;
    region.notifyOnExit = YES;
    
    NSLog(@"Created region");
    
    return region;
}

-(void)monitorProximity
{
    CLRegion *region = [self createRegion];
    
    // Check if support is unavailable
    if ( ![CLLocationManager isMonitoringAvailableForClass:[CLRegion class]]) {
        NSLog( @"Failed to initialise region monitoring: support unavailable");
        return;
    }
    
    // Check if authorised
    if ([CLLocationManager authorizationStatus] != kCLAuthorizationStatusAuthorizedAlways) {
        NSLog( @"Failed to initialise region monitoring: app not authorized to use location services");
        return;
    } else {
        NSLog(@"Started monitoring proximity");
    }
    
    
    // Clear out any old regions to prevent buildup.
    if ([_locationManager.monitoredRegions count] > 0) {
        for (id obj in _locationManager.monitoredRegions)
            [_locationManager stopMonitoringForRegion:obj];
    }
    
    [_locationManager startMonitoringForRegion:region];
}

-(void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
    NSLog(@"Started monitoring for region: %@", [region description]);
    //[_locationManager requestStateForRegion:region]; // check if already inside region
}

-(void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{
    NSLog(@"Failed to start monitoring for region: %@", [error localizedDescription]);
}


-(void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
    NSLog(@"didDetermineState");
    
    if (state == CLRegionStateInside) {
        //println(@"Enter");
        NSLog(@"inside");
        return;
        
        
    } else if (state == CLRegionStateOutside) {
        // println(@"Outside");
        NSLog(@"outside");
        
    } else {
        // println("unknown");
        NSLog(@"unknown");
    }
    
}

-(void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
    NSLog(@"didEnterRegion");
    
}

-(void)locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region
{
    NSLog(@"didExitRegion");
    
}

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    NSLog(@"Monitoring authorisation status is now: %@", status == kCLAuthorizationStatusAuthorizedAlways ? @"authorized" : @"not authorized");
    
    if (status == kCLAuthorizationStatusAuthorizedAlways) {
        [self monitorProximity];
    }

}
@end

更新:我在哪里为locationManager调用Initialise调用并且还与MapKit映射关联?当线程失败时,应用程序将无法启动。

0 个答案:

没有答案