didUpdateToLocation不叫(didUpdateToLocation is not c

2019-09-17 18:44发布

我希望收到位置更新。 我添加了一个位置代表对标题:

@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
}

但既不didUpdateToLocation也不didFailWithError被称为(设备上的仿真器和上同样的问题)。 我在想什么?

提前致谢,

Luda

Answer 1:

为您的应用程序的位置服务启用?

PS:这将有助于使你的位置管理的实例变量则其将被ARC的方法后,仍然保留- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions完成。



Answer 2:

我意识到这是一个死灵,但我找遍高和低对这个答案并不能找到我类似情况的解决方案,这似乎是它的最佳地点。

我与,设置一些标志然后运行[个体>的LocationManager startUpdatingLocation],并且在viewDidLoad中调用它作为这样的自定义初始化一个委托对象:

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

无论didUpdateToLocation也不didFailWithError渐渐调用。 好像是ARC忘记了我的getter对象的回调来之前通过。 通过声明吸气剂如在视图控制器实例var和在viewDidLoad中初始化它解决了这个问题。



文章来源: didUpdateToLocation is not called