0

我是 Objective-C 世界的新手。我尝试在模拟器上的 iOS 6.1 上开发简单的定位应用程序。但是当我尝试模拟位置时,它根本不起作用。我没有收到任何事件,但delegate对象已正确注册并且PositionProviderstartUpdate.

怎么了?

我有以下代码:

位置提供者

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "LocationListener.h"

@interface PositionProvider : NSObject
{
    @public
    LocationListener<CLLocationManagerDelegate> *listener;
    @protected
    CLLocationManager *manager;
}

#pragma mark -
#pragma mark on/off

-(void) startUpdate;

-(void) stopUpdate;

@end

执行

    @implementation PositionProvider
{
@private
    CLLocation *lastLocation;
}

-(void) startUpdate{
    manager = [[CLLocationManager alloc] init];

    LocationListener *listener2 = [[LocationListener alloc]init];

    manager.delegate = listener2;
    manager.desiredAccuracy = kCLLocationAccuracyBest;

    if(manager == nil)
        NSLog(@"Manager is NULL");

    if(listener == nil || manager.delegate == nil)
        NSLog(@"Listener is NULL");

    NSLog(@"startUpdate");

    if ([CLLocationManager locationServicesEnabled]) {
        NSLog(@"Location Services Enabled");
        switch ([CLLocationManager authorizationStatus]) {
            case kCLAuthorizationStatusAuthorized:
                NSLog(@"We have access to location services");
                break;
            case kCLAuthorizationStatusDenied:
                NSLog(@"Location services denied by user");
                break;
            case kCLAuthorizationStatusRestricted:
                NSLog(@"Parental controls restrict location services");
                break;
            case kCLAuthorizationStatusNotDetermined:
                NSLog(@"Unable to determine, possibly not available");
                break;
            default:
                break;
        }
    }
    else {
        NSLog(@"Location Services Are Disabled");
    }

    [manager startUpdatingHeading];
    [manager startUpdatingLocation];

}

-(void) stopUpdate{



}

@end

位置监听器

@interface LocationListener : NSObject<CLLocationManagerDelegate>

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;



    - (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;

@end

执行

@implementation LocationListener 

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{

//    newLocation.coordinate;

    NSLog(@"Logging Location");
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    NSLog(@"Logging Location");
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus) status{


    NSLog(@"Status");
}

@end
4

1 回答 1

0

你有没有尝试过

[manager setDelegate:listener2];

或者,您可能遇到内存管理问题。尝试使用调试器断点添加 dealloc 方法,看看它是否在您预期的之前被调用:

- (void)dealloc {
    NSLog(@"Dealloc called");

    self.locationManager = nil;
}
于 2013-11-21T05:26:38.823 回答