【发布时间】:2015-03-05 18:20:24
【问题描述】:
我一直在做一个应用程序来检测信标。当应用程序处于前台或屏幕打开时,该应用程序运行良好。但是屏幕关闭时不会调用 locationManager:didEnterRegion 方法。据我所知, locationManager:didEnterRegion 应该以任何一种方式调用。
我尝试过很多场景,比如进入区域后等待 15 分钟以上,不同的配置(notifyEntryStateOnDisplay=true 或 false)......但结果是一样的。 locationManager:didEnterRegion 方法直到解锁手机才会被调用。
PS: locationManager:didExitRegion 在屏幕关闭时被正确调用。
我使用 iPhone 5、5S 和 6 plus 在 IOS 8.1 上测试应用
编辑:“位置更新”和“使用蓝牙 LE 配件”已启用
感谢您的回答。
import UIKit
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
var locationManager: CLLocationManager?
var lastProximity: CLProximity?
let beaconRegion = CLBeaconRegion(
proximityUUID: NSUUID(UUIDString:"d2c36ec5-dfsb-46d2-b069-d2f3r51996e0"),
identifier: "My region"
)
func application(application: UIApplication,
didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
self.beaconRegion.notifyOnEntry = true
self.beaconRegion.notifyOnExit = true
self.beaconRegion.notifyEntryStateOnDisplay = false
locationManager = CLLocationManager()
if(locationManager!.respondsToSelector("requestAlwaysAuthorization")) {
locationManager!.requestAlwaysAuthorization()
}
locationManager!.delegate = self
locationManager!.pausesLocationUpdatesAutomatically = false
locationManager!.startMonitoringForRegion(beaconRegion)
locationManager!.startRangingBeaconsInRegion(beaconRegion)
locationManager!.startUpdatingLocation()
return true
}
func locationManager(manager: CLLocationManager!,
didDetermineState state: CLRegionState,
forRegion region: CLRegion!) {
NSLog("State determined")
}
func locationManager(manager: CLLocationManager!,
didRangeBeacons beacons: [AnyObject]!,
inRegion region: CLBeaconRegion!) {
NSLog("didRangeBeacons")
}
func locationManager(manager: CLLocationManager!,
didEnterRegion region: CLRegion!) {
NSLog("didEnterRegion")
manager.startRangingBeaconsInRegion(region as CLBeaconRegion)
manager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!,
didExitRegion region: CLRegion!) {
NSLog("didExitRegion")
manager.stopRangingBeaconsInRegion(region as CLBeaconRegion)
manager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager!,
monitoringDidFailForRegion region: CLRegion!,
withError error: NSError!) {
NSLog("monitoringDidFailForRegion - error: %@", [error.localizedDescription]);
}
}
【问题讨论】:
-
尝试删除设备上的密码锁定屏幕,然后重复测试。根据用户进入该区域时您尝试执行的操作,锁定屏幕可能会干扰。显然,关闭锁定屏幕不是解决方案,但可能会为真正的问题提供线索。
-
在我测试应用时密码已被禁用
标签: ios swift ios8 core-bluetooth ibeacon