是否可以直接从 appDelegate 获取 GPS 位置 ios8
Is it possible to fetch GPS location ios8 directly from appDelegate
我正在 ios8+ 开发一个应用程序,我必须在其中获取用户的位置。当我在屏幕中使用 CLLocationManager 时,我能够获取位置 (viewcontroller)。但是当我尝试直接从 AppDelegate 获取位置时,似乎出现了一些问题,我没有获得任何位置(流程没有进入委托方法)。
谁能帮我解决这个问题。
这是我使用的代码:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager requestAlwaysAuthorization];
}
[locationManager startUpdatingLocation];
你能帮我解决这个问题吗?谢谢
还知道 CoreLocation 权限已更改 iOS 8. 如果您不请求新的权限授权,CoreLocation 不会执行任何操作。它悄悄地失败了,因为永远不会调用委托方法。
虽然文章非常详细和冗长,但实际的代码修复可能像这样:
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
并且您必须向 info.plist 添加一个值,这是要在权限警报中显示的消息。请参阅屏幕截图。
键:NSLocationWhenInUseUsageDescription
值:需要位置才能确定您所在的位置(您可以将其更改为任何您想要的)
- 将
CLLocationManagerDelegate
添加到 AppDelegate.h
@interface AppDelegate : NSObject < UIApplicationDelegate, CLLocationManagerDelegate> {
- 将
CLLocationManager
对象添加为 属性 到 AppDelegate.m
您必须将 CLLocationManager
对象实现为 属性。
@interface AppDelegate ()
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
- 在AppDelegate.m
中将locationManager
替换为self.locationManager
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
Keeping a strong reference to the location manager object is required until all tasks involving that object are complete. Because most location manager tasks run asynchronously, storing your location manager in a local variable is insufficient.
我正在 ios8+ 开发一个应用程序,我必须在其中获取用户的位置。当我在屏幕中使用 CLLocationManager 时,我能够获取位置 (viewcontroller)。但是当我尝试直接从 AppDelegate 获取位置时,似乎出现了一些问题,我没有获得任何位置(流程没有进入委托方法)。 谁能帮我解决这个问题。
这是我使用的代码:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
if ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[locationManager requestAlwaysAuthorization];
}
[locationManager startUpdatingLocation];
你能帮我解决这个问题吗?谢谢
还知道 CoreLocation 权限已更改 iOS 8. 如果您不请求新的权限授权,CoreLocation 不会执行任何操作。它悄悄地失败了,因为永远不会调用委托方法。
虽然文章非常详细和冗长,但实际的代码修复可能像这样:
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
并且您必须向 info.plist 添加一个值,这是要在权限警报中显示的消息。请参阅屏幕截图。
键:NSLocationWhenInUseUsageDescription
值:需要位置才能确定您所在的位置(您可以将其更改为任何您想要的)
- 将
CLLocationManagerDelegate
添加到 AppDelegate.h
@interface AppDelegate : NSObject < UIApplicationDelegate, CLLocationManagerDelegate> {
- 将
CLLocationManager
对象添加为 属性 到 AppDelegate.m
您必须将 CLLocationManager
对象实现为 属性。
@interface AppDelegate ()
@property (nonatomic, strong) CLLocationManager *locationManager;
@end
- 在AppDelegate.m 中将
locationManager
替换为self.locationManager
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
Keeping a strong reference to the location manager object is required until all tasks involving that object are complete. Because most location manager tasks run asynchronously, storing your location manager in a local variable is insufficient.