Ios 本地区域事件发生时启动的应用

Ios app that starts when a Local Region event occurs

我正在尝试开发一个应用程序,当我进入之前创建的本地区域时,通知此事件。我已经阅读了很多讨论,但我仍然感到困惑。我正在这个社区的帮助下开发这个应用程序,因为我也是 swift 的新手。开发的应用程序在处于活动状态或在后台时运行良好,但如果关闭则无法运行。事实上,我想要 s.o。当 iphone 进入之前创建的本地区域时唤醒我的应用程序(为了做一些事情,比如通知)。阅读苹果的文档,我发现 lauchOptions 对我的问题很重要。所以在上面,我 post 我的 AppDelegate class 中应用程序函数的代码。

     func application(_ application: UIApplication, 
     didFinishLaunchingWithOptions launchOptions: 
     [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
     // Override point for customization after application 
    launch.

    registerForPushNotifications()

    if launchOptions?[UIApplicationLaunchOptionsKey.location] != nil {
        if locationManager == nil {
            locationManager = CLLocationManager()
            locationManager?.delegate = self
            locationManager?.distanceFilter = 10
            locationManager?.desiredAccuracy = kCLLocationAccuracyBest
            locationManager?.allowsBackgroundLocationUpdates = true
            locationManager?.startUpdatingLocation()
            Logger.write(text: "app opened by s.o.", to: logFile)
        }
    } else {
        locationManager?.delegate = self
        locationManager?.distanceFilter = 10
        locationManager?.desiredAccuracy = kCLLocationAccuracyBest
        locationManager?.allowsBackgroundLocationUpdates = true
        locationManager?.startUpdatingLocation()

        if CLLocationManager.authorizationStatus() == .notDetermined {
            locationManager?.requestAlwaysAuthorization()
        }
        else if CLLocationManager.authorizationStatus() == .denied {
        }
        else if CLLocationManager.authorizationStatus() == .authorizedAlways {
            locationManager?.requestAlwaysAuthorization()
        }
    }

    return true
}

根据发现的其他讨论,我已经实现了这段代码,但如前所述,字符串 "app opened by s.o." 从未写入我的日志文件。我怎么解决这个问题?提前谢谢大家!

好吧,只需打印调试您的代码。

print("registered for push notifications")
if launchOptions?[UIApplicationLaunchOptionsKey.location] != nil {
    print(locationManager)
    if locationManager == nil {
        ...

您应该看到 "registered for push notifications" 然后是 locationManager 是什么。也许它不是零,所以 Logger 甚至没有被执行