我目前正在尝试找到一种方法,在iOS的后台使用位置监控,然后触发范围,如下所示:
func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
if (state == .Inside) {
locationManager.startRangingBeaconsInRegion((region as? CLBeaconRegion)!)
}
}然后,我将尝试在信标范围内进行API调用
func locationManager(manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], inRegion region: CLBeaconRegion) {
for beacon in beacons {
let minor = beacon.minor as Int
let major = beacon.major as Int
do {
try APICall.canSeeBeacons(major, minor: minor)
} catch {
print("Error making API call")
}
}
}然而,这只能在手机处于后台时工作大约10分钟,10分钟后它不再工作,但我希望它能连续工作,这样当找到信标时,API调用总是可以进行的。我还在权限中设置了正确的密钥,并且在我的locationManager上使用了requestAlwaysAuthorization()
发布于 2016-02-07 00:45:57
看看苹果关于后台执行的iOS开发工具库:https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html
如果您的应用程序的类型是可接受的较长后台执行类型之一,请使用一个数组将密钥Required background modes添加到您的Info.plist中,该数组包含一个或多个启用的类型:audio、location、voip、newsstand-content、external-accessory和/或bluetooth-central。
只需注意,在app Store上发布您的应用程序之前,Apple将对此进行审查。
编辑:
我有一段时间没有使用它了,但现在测试了一下,从Xcode 6开始它似乎更容易了。
1.将密钥Required background modes添加到Info.plist
2.转到Capabilities

3.选择适合的背景模式。

https://stackoverflow.com/questions/35243224
复制相似问题