Ios 未设置UIApplicationAunChoptionSkey中的位置键
我正在监视一个CLBeaconRegion,当应用程序在后台以及终止时(通过用户向上滑动),会调用我的didEnterRegion和didExitRegion回调。但是,当应用程序被唤醒时,UIApplicationLaunchOptionsKey.location为零 以下是我的AppDelegate代码:Ios 未设置UIApplicationAunChoptionSkey中的位置键,ios,core-location,ibeacon,appdelegate,Ios,Core Location,Ibeacon,Appdelegate,我正在监视一个CLBeaconRegion,当应用程序在后台以及终止时(通过用户向上滑动),会调用我的didEnterRegion和didExitRegion回调。但是,当应用程序被唤醒时,UIApplicationLaunchOptionsKey.location为零 以下是我的AppDelegate代码: class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? var bea
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
var beaconDelegate: BeaconDelegate = BeaconDelegate()
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
if launchOptions?[UIApplicationLaunchOptionsKey.location] != nil {
var regionCounter = UserDefaults.standard.object(forKey: "regionCounter") as! Int
regionCounter += 5
UserDefaults.standard.set(regionCounter, forKey: "regionCounter")
UserDefaults.standard.synchronize()
}
return true
}
func applicationWillResignActive(_ application: UIApplication) {
UserDefaults.standard.synchronize()
}
func applicationWillTerminate(_ application: UIApplication) {
UserDefaults.standard.synchronize()
}
}
这是我的大使,是迪登特地区:
class BeaconDelegate: NSObject {
var locationManager: CLLocationManager
override init() {
locationManager = CLLocationManager()
locationManager.requestAlwaysAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = 1
super.init()
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion){
var regionCounter = UserDefaults.standard.object(forKey: "regionCounter") as! Int
regionCounter += 1
UserDefaults.standard.set(regionCounter, forKey: "regionCounter")
UserDefaults.standard.synchronize()
// Create Local Notification
let content = UNMutableNotificationContent.init()
content.title = "Beacon Region"
content.body = body
content.sound = UNNotificationSound.default()
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 3, repeats: false)
let identifier = "MyLocalNotification"
let request = UNNotificationRequest(identifier: identifier,
content: content, trigger: trigger)
let center = UNUserNotificationCenter.current()
center.add(request, withCompletionHandler: { (error) in
if let error = error {
print("Cannot add notification, error: \(error)")
}
})
}
}
在my RootViewController的UserDefaults中,regionCounter设置为0。从前台或后台调用my didEnterRegion时,regionCounter将递增1。但是本地通知显示,当应用程序被唤醒时,它也只增加1(而不是5)
为什么没有设置位置键,但调用了my didEnterRegion?如果在日志行中打印启动选项(可从XCode->Devices->{your device}查看),有哪些键?当应用程序被唤醒时,我在日志中看不到任何内容。它在后台时是否写入日志?另外,我在Xcode->Devices->MyDevice中没有看到控制台日志,所以我查看了控制台应用程序。但是,应用程序的日志不会在后台写入。我确实看到系统消息显示正在后台唤醒。但我更改了检查位置键的appDelegate代码,并将其保存在UserDefaults中。稍后,当我打开应用程序时,我会打印出UserDefaults。现在我确实看到正在设置位置键。现在我只需这样检查:if(launchOptions!=nil)并循环通过launchOptions键。谢谢你的帮助!