未调用didUpdateToLocation

时间:2012-06-05 08:21:42

标签: iphone objective-c xcode cocoa-touch cllocationmanager

我想收到位置更新。 我在标题中添加了一个位置委托:

@interface AppDelegate : UIResponder <UIApplicationDelegate, CLLocationManagerDelegate>

以及以下功能:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //initialize location manager
    CLLocationManager* locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    // [locationManager startMonitoringSignificantLocationChanges];
    [locationManager startUpdatingLocation];

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.viewController = [[ViewController alloc] initWithNibName:@"SomeViewController"     bundle:nil];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    return YES;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    //Some problem
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation     *)newLocation fromLocation:(CLLocation *)oldLocation{
    //Some action
}

但是didUpdateToLocationdidFailWithError都没有被调用(模拟器和设备上的问题相同)。我错过了什么?

提前致谢,

旅大

2 个答案:

答案 0 :(得分:4)

是否启用了应用程序的位置服务?

P.S:有助于使您的位置管理器成为一个实例变量,以便在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions完成后由ARC保留。

答案 1 :(得分:0)

我意识到这是一个死神,但是我在这个问题上搜索得很高,并且无法找到解决我类似情况的解决方案,这似乎是最好的选择。

我创建了一个带有自定义初始化程序的Delegate对象,该初始化程序设置了一些标志,然后运行[self-&gt; locationManager startUpdatingLocation],并在viewDidLoad中调用它:

myLocationGetter* __unused getter = [[myLocationGetter alloc]initAndUpdateOnce;

无法调用didUpdateToLocation和didFailWithError。似乎ARC在回调发生之前忘记了我的getter对象。通过在视图控制器中将getter声明为实例var并在viewDidLoad中初始化它来解决此问题。