这是一个 CoreLocation 封装
(单例) 以允许在整个应用程序中进行内部使用。
在您的 AppDelegate 文件或其他位置调用 [JFELocationManager sharedInstance];
以初始化单例并自动开始更新位置和航向
JFELocationManager 现在已准备好支持 iOS 8。要启用此库,您必须在您的 Info.plist
文件中添加一个新的条目。
NSLocationAlwaysUsageDescription = "The message to be shown";
设置控制器为
@interface JFEViewController : UIViewController<JFELocationManagerDelegate>
然后您可以根据需要实现以下(或其中之一)委托方法
-(void) didUpdateHeading:(CLHeading *)newHeading;
-(void) didUpdateLocation:(CLLocation*) location;
-(void) didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
要允许后台更新,您必须在您的 appdelegate
文件中定义这些方法
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// ... Do something else ...
// Force to disable the location update only if there is no "important delegate"
if ([JFELocationManager sharedInstance].importantDelegate == nil) {
NSLog(@"AppDelegate -> I disable updating location");
[[JFELocationManager sharedInstance] stop];
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// ... Do something else ...
if ([JFELocationManager sharedInstance].importantDelegate == nil) {
[[JFELocationManager sharedInstance] start];
NSLog(@"AppDelegate -> I enable updating location");
}
}
iOS 6.0 及以上。
将 JFELocationManager.h/m
添加到您的项目中,或使用 CocoaPods 添加 pod 'JFELocationManager'
。
Jerome Freyre, [email protected]
JFELocationManager 在 MIT 许可证下可用。有关更多信息,请参阅 LICENSE 文件。