我的问题与此处的问题基本相同:
iOS Geofence, how to handle when inside region when monitoring starts?
我需要一个Swift 3解决方案。
我正在使用关于Geofencing的优秀raywenderlich教程,它在我的项目中工作,几乎没有自定义。我的应用程序在穿越地理围栏边界时会做出完美的响应。
但我需要在app启动时确定用户是否已经在地理围栏内。
我假设我需要使用
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
... // There's some other stuff
locationManager.delegate = self
locationManager.requestAlwaysAuthorization()
loadAllGeotifications() // There will be only one !!
let homeRegion = region(withGeotification: geotifications[1]) as! CLRegion
locationManager.requestState(for: homeRegion)
return true
}
func loadAllGeotifications() {
geotifications = []
guard let savedItems = UserDefaults.standard.array(forKey: PreferencesKeys.savedItems) else { return }
for savedItem in savedItems {
guard let geotification = NSKeyedUnarchiver.unarchiveObject(with: savedItem as! Data) as? Geotification else { continue }
geotifications.append(geotification)
}
}
func region(withGeotification geotification: Geotification) -> CLCircularRegion {
let region = CLCircularRegion(center: geotification.coordinate, radius: geotification.radius, identifier: geotification.identifier)
return region
}
}
func locationManager(manager: CLLocationManager, didDetermineState state: CLRegionState, forRegion region: CLRegion) {
let defaults: UserDefaults = UserDefaults.standard
switch state {
case .inside:
defaults.set(true, forKey: "atHome")
case .outside:
defaults.set(false, forKey: "atHome")
default:
defaults.set(true, forKey: "atHome") // Assume user is at home unless/until determined otherwise
}
}
在我的appDelegate中。
我缺少的是如何将我的地理围栏区域加载为CLRegion,以便我可以在启动时使用它来调用该函数。
这是我尝试过的不起作用的东西。
//Iterates through all Posting Opjects
XPathNodeIterator loopIter = payload.getXpathList("//FolioInfo/Postings[NetAmount>10]");
foreach (XPathNavigator val in loopIter)
{
string xPathLookup = "//TrxInfo[Code=TrxCode]/Description" //Returns no result
//string xPathLookup = "//TrxInfo[Code=1]/Description" //Returns "Desc 2"
//string xPathLookup = "NetAmount" //Works Correctly
XPathExpression rgxLookup = XPathExpression.Compile(xPathLookup);
XmlNamespaceManager manager = new XmlNamespaceManager(val.NameTable);
rgxLookup.SetContext(manager);
string tmpText = val.Evaluate(rgxLookup); //Simplified - Changes depending on ReturnType
//Do stuff
}
didDetermineState永远不会回来。同样的问题是CLCircularRegion是否重新改写为CLRegion - 没有区别。
答案 0 :(得分:1)
我解决了!
这里有替代代码:
func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
... }
注意添加" _"在"经理"。
我的" atHome"现在,当应用程序启动时,旗帜已正确设置。
重新制作ULRegion是可以的,但没有必要。
let homeRegion = region(withGeotification: geotifications[1])
......工作正常