我想接收位置更新。我在标题中添加了一个位置委托:
@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
(模拟器和设备上的问题相同)。我错过了什么?
提前感谢,
卢达
是否为应用程序启用了定位服务?
PS:这将使您的位置管理器成为实例变量会有所帮助,以便在方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
完成后由 ARC 保留它。
我意识到这是一个死灵,但我上下搜索了这个问题的答案,但找不到适合我类似情况的解决方案,这似乎是最好的地方。
我用自定义初始值设定项创建了一个委托对象,该初始值设定项设置了一些标志,然后运行[self->locationManager startUpdatingLocation],并在viewDidLoad中调用它:
myLocationGetter* __unused getter = [[myLocationGetter alloc]initAndUpdateOnce;
UpdateToLocation和FailWithError都没有被调用。似乎 ARC 在回调通过之前忘记了我的 getter 对象。通过在视图控制器中将 getter 声明为实例 var 并在 viewDidLoad 中对其进行初始化来解决此问题。