【问题标题】:Ios app that starts when a Local Region event occurs发生本地区域事件时启动的 Ios 应用程序
【发布时间】:2018-07-30 16:04:48
【问题描述】:

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

     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
}

根据找到的其他讨论,我已经实现了此代码,但正如字符串“由 s.o. 打开的应用程序”之前所说的那样。永远不会写在我的日志文件上。我怎么解决这个问题?提前谢谢大家!

【问题讨论】:

    标签: ios swift geolocation location core-location


    【解决方案1】:

    只需打印调试您的代码。

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

    您应该会看到“已注册推送通知”,然后会看到 locationManager 是什么。也许它不是 nil 所以 Logger 甚至没有被执行

    【讨论】:

    • 是的!你说得对!现在它起作用了!事实上,当应用程序以这种方式启动时,locationManager 总是为零!谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    相关资源
    最近更新 更多