当用户输入CLLocation时如何通知用户? [英] How to notify user when he enters a CLLocation?

查看:82
本文介绍了当用户输入CLLocation时如何通知用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:



我遵循了以下教程:



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






问题:



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



AppDelegate.swift

  func locationManager(管理员:CLLocationManager,didEnterRegion区域:CLRegion){
如果区域是CLCircularRegion {
handleRegionEvent(region)
}
}

func locationManager(manager: CLLocationManager,didExitRegion区域:CLRegion){
如果区域是CLCircularRegion {
handleRegionEvent(region)
}
}

func handleRegionEvent(region:CLRegion! ){

print(触发了Geofence!)

//如果应用程序处于活动状态,则显示警报
如果UIApplication.sharedApplic ation()。applicationState == .Active {
如果let message = notefromRegionIdentifier(region.identifier){
如果let viewController = window?.rootViewController {
showSimpleAlertWithTitle( Congratulations,消息: 您刚发现: + message,viewController:viewController)
}
}
} else {
//否则显示本地通知
let notifications = UILocalNotification( )
notification.alertBody =您刚刚找到: + notefromRegionIdentifier(region.identifier)!

notification.soundName =默认;
UIApplication.sharedApplication()。presentLocalNotificationNow(notification)
}
}






问题:



该教程是为iOS 8编写的。 iOS 9.3版。您认为是什么导致了此问题,以及如何解决该问题?

解决方案

请确保以下两点:-



1。)您已将它们添加到 viewDidLoad():-

  locationManager.delegate =自我
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.requestWhenInUseAuthorization()
locationManager。 startMonitoringSignificantLocationChanges()
locationManager.startUpdatingLocation()

的另一种选择专门针对Swift 2.2的requestWhenInUseAuthorization() startUpdatingLocation()初始化,因为在Swift 2.2中不赞成使用选择器的字符串文字,而是在其中您需要使用的新运算符#selector。 :-



您也可以使用:-

  locationManager.delegate =自我
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = kCLDistanceFilterNone

locationManager.startMonitoringSignificantLocationChanges()
如果locationManager.respondsToSelector(#selector(locationManager.requestWhenInUseAuthorization)) {
locationManager.requestWhenInUseAuthorization()
}
else {
locationManager.startUpdatingLocation()
}
//首选第一个。

2。)您已经用:-


更新了info.plist。

NSLocationAlwaysUsageDescription:字符串:->我需要位置。



NSLocationWhenInUseUsageDescription:String:->我需要位置。



privacy-位置用法描述:字符串:->我需要位置。



根据应用程序的需要编辑我需要位置

PS:-如果仍然不调用 locationManager 函数



Simulator:-在模拟器设置中查找应用程序的位置设置。



设备:-进入设置>隐私>定位服务>您的应用>始终。



您可能还会发现此说明有用:- https://stackoverflow.com/a/26090094/6297658


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 ?

解决方案

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

这篇关于当用户输入CLLocation时如何通知用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆