How to notify user when he enters a CLLocation?

雨燕双飞 提交于 2019-12-11 04:13:32

问题


SITUATION:

I followed the following tutorial:

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


PROBLEM:

The following functions never get triggered:

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)
    }
}

QUESTION:

The tutorial was written for iOS 8. I am currently on iOS 9.3. What caused this issue in your opinion and how do I fix it ?


回答1:


Make sure of two things :-

1.) You have added These to your viewDidLoad() :-

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

Another alternative to requestWhenInUseAuthorization() and startUpdatingLocation() initialisation in specific to Swift 2.2, since in Swift 2.2 the string literals for selectors is deprecated, and instead there this new operator #selector that you need to be using. :-

you can also use :-

    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.) You have updated your info.plist with :-

NSLocationAlwaysUsageDescription : String :-> I need location.

NSLocationWhenInUseUsageDescription: String :-> I need location.

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

Edit I need location according to the app's need

PS :- If it still not calls your locationManager functions

Simulator :- look for location settings of your app in your simulator settings.

Device: - Go in settings > Privacy > Location services > Your app > Always.

you also might find this explanation useful : - https://stackoverflow.com/a/26090094/6297658




回答2:


You didn't show the code that you use to set up CL - which is probably where your problem lies.

Did you edit info.plist?

Are you requesting permission?

Did you call one of the start functions on the CL manager?




回答3:


initialize your location manager in app delegate on did finish launching



来源:https://stackoverflow.com/questions/38960160/how-to-notify-user-when-he-enters-a-cllocation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!