我有一个使用CLLocationManager来请求位置更新的应用程序.当应用程序收到外部蓝牙设备的通知时,将启动监控.当应用程序位于前台和后台时,应用程序将侦听来自设备的通知,并且在这两种情况下都会成功启动位置更新.
如果在应用程序位于前台时启动监控,则会根据我在位置管理器上配置的距离过滤器获取位置更新.
但是,如果在应用程序处于后台时启动监控,我仍然会获得位置更新,但很少.有谁知道这是否是预期的行为,或者我是否有可能错误地配置了某些东西?
代码中的设置如下:
fileprivate lazy var locationManager = CLLocationManager()
func initLocationManager(distanceFilter: CLLocationDistance = 270,
desiredAccuracy: CLLocationAccuracy = kCLLocationAccuracyNearestTenMeters,
activityType: CLActivityType = .automotiveNavigation) {
locationManager.requestAlwaysAuthorization()
locationManager.allowsBackgroundLocationUpdates = true
if canGetLocationUpdate() {
locationManager.desiredAccuracy = desiredAccuracy
locationManager.distanceFilter = distanceFilter
locationManager.activityType = activityType
locationManager.delegate = self
}
}
func startLocationMonitoring() {
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
} else {
log.error("Unable to start location monitoring")
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
for location in locations {
log.debug("Got location \(location.coordinate) with speed …Run Code Online (Sandbox Code Playgroud) 我正在为现有的Objective-C项目添加swift代码.我在引用现有前缀标头中的定义时遇到问题.
我有一个名为MyClass的类,在Objective-C中用.h定义:
@interface MyClass
+(instancetype)myClass;
-(void)doStuff;
@end
Run Code Online (Sandbox Code Playgroud)
而.m:
@implementation MyClass
+ (instancetype) myClass
{
// More to it than this, but this illustrates the setup
return [[MyClass alloc] init];
}
- (void)doStuff
{
// Do something interesting
}
@end
Run Code Online (Sandbox Code Playgroud)
前缀头MyProj-Prefix.pch包含:
#define MYCLASS MyClass
#define SOMEVAR @"Hello"
Run Code Online (Sandbox Code Playgroud)
我创建了一个桥接头,其中包含:
#import "MyProj-Prefix.pch"
Run Code Online (Sandbox Code Playgroud)
该项目包含使用类定义的Objective-C代码,例如
[[MYCLASS myClass] doStuff];
Run Code Online (Sandbox Code Playgroud)
我想在我的新swift代码中反映这一点.我可以看到并引用已定义的变量,但是定义的类是不可见的.例如
let someStr = SOMEVAR // This compiles
MYCLASS.myClass.doStuff() // MYCLASS isn't visible
Run Code Online (Sandbox Code Playgroud)
关于如何实现这一点的任何指针?我甚至不确定这是否可以迅速实现.