【问题标题】:How to notify user when he enters a CLLocation?如何在用户输入 CLLocation 时通知用户?
【发布时间】:2016-08-15 17:45:11
【问题描述】:

情况:

我按照以下教程进行操作:

https://www.raywenderlich.com/95014/geofencing-ios-swift


问题:

以下函数永远不会被触发:

AppDelegate.swift

func locationManager(manager: CLLocationManager, didEnterRegion region: CLRegion) {
    if region is CLCircularRegion {
        handleRegionEvent(region)
    }
}

func locationManager(manager: CLLocationManager, didExitRegion region: CLRegion) {
    if region is CLCircularRegion {
        handleRegionEvent(region)
    }
}

func handleRegionEvent(region: CLRegion!) {

     print("Geofence triggered!")

    // Show an alert if application is active
    if UIApplication.sharedApplication().applicationState == .Active {
        if let message = notefromRegionIdentifier(region.identifier) {
            if let viewController = window?.rootViewController {
                showSimpleAlertWithTitle("Congratulations", message: "You just found: " + message , viewController: viewController)
            }
        }
    } else {
        // Otherwise present a local notification
        let notification = UILocalNotification()
        notification.alertBody = "You just found: " + notefromRegionIdentifier(region.identifier)!

        notification.soundName = "Default";
        UIApplication.sharedApplication().presentLocalNotificationNow(notification)
    }
}

问题:

本教程是为 iOS 8 编写的。我目前使用的是 iOS 9.3。在您看来是什么导致了这个问题,我该如何解决?

【问题讨论】:

  • 编辑:代码在 XCode 模拟器中运行良好(我可以使用 gpx 文件在 XCode 中模拟从 A 点到 B 点的移动),但在我的 iPhone 上却不行……为什么?肯定有解决方案。

标签: ios swift core-location


【解决方案1】:

您没有显示用于设置 CL 的代码 - 这可能是您的问题所在。

您是否编辑了 info.plist?

您在请求许可吗?

您是否调用了 CL 管理器上的启动函数之一?

【讨论】:

  • 让我编辑我的问题。编辑:完成。是的,我根据需要编辑了 info.plist。
【解决方案2】:

确保两件事:-

1.) 您已将这些添加到您的viewDidLoad() :-

    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone
    locationManager.requestWhenInUseAuthorization()
    locationManager.startMonitoringSignificantLocationChanges()
    locationManager.startUpdatingLocation()

另一种替代 requestWhenInUseAuthorization()startUpdatingLocation() 初始化特定于 Swift 2.2 的方法,因为在 Swift 2.2 中,选择器的字符串文字已被弃用,取而代之的是您需要使用的新运算符 #selector。 :-

你也可以使用:-

    locationManager.delegate = self 
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.distanceFilter = kCLDistanceFilterNone

    locationManager.startMonitoringSignificantLocationChanges()
     if locationManager.respondsToSelector(#selector(locationManager.requestWhenInUseAuthorization)) {
        locationManager.requestWhenInUseAuthorization()
    }
    else {
        locationManager.startUpdatingLocation()
    }
      //Prefer the FIRST ONE.

2.) 您已将 info.plist 更新为:-

NSLocationAlwaysUsageDescription : String :-> I need location.

NSLocationWhenInUseUsageDescription: String :-> I need location.

privacy - location usage description: String :-> I need location.

根据应用需要编辑I need location

PS :- 如果它仍然没有调用你的 locationManager 函数

模拟器:- 在模拟器设置中查找您的应用的 location 设置。

设备:-进入settings > Privacy > Location services > Your app > Always.

你也可能会发现这个解释很有用:-https://stackoverflow.com/a/26090094/6297658

【讨论】:

  • 嗨,它可以在我的模拟器上运行,但不能在我的手机上运行。请参阅我对我的问题的评论。
  • 看起来像!谢谢:D
【解决方案3】:

在完成启动时在应用委托中初始化您的位置管理器

【讨论】:

    猜你喜欢
    • 2018-09-02
    • 1970-01-01
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多