按下主页按钮时,Swift CLLocationManager 弹出窗口不会停留
Posted
技术标签:
【中文标题】按下主页按钮时,Swift CLLocationManager 弹出窗口不会停留【英文标题】:Swift CLLocationManager Popup not stay when home button press 【发布时间】:2016-04-29 05:36:15 【问题描述】:我正在使用 CLLocationManager,问题是当应用程序启动时,然后在 2 秒后(显示 splashScreen 时)按主页按钮,然后应用程序转到后台统计,然后位置弹出窗口仅显示 1 秒并自动隐藏,用户无法单击因为位置弹出窗口自动隐藏,然后应用程序转到后台到前台位置弹出窗口丢失,当我杀死应用程序然后再次打开应用程序弹出窗口显示或一段时间不显示,并且设置屏幕一般没有位置允许选项与否,我在做什么,我的代码哪里有问题,我的问题是 ios 框架 这是我的代码
Plist
<key>NSLocationAlwaysUsageDescription</key>
<string>Get Your Location </string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Get Your Location For </string>
AppDelegate.swift
import CoreLocation
class AppDelegate: UIResponder, UIApplicationDelegate ,CLLocationManagerDelegate
let locationManager = CLLocationManager()
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
// Override point for customization after application launch.
//NSThread.sleepForTimeInterval(3);
self.initLocationManager();
return true
func initLocationManager()
// locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.distanceFilter = 40 // Must move at least 1km
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
//self.locationManager.requestAlwaysAuthorization()
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
print(locations)
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
print("Error")
请帮助我,当我从苹果商店下载 ola 应用程序和 Uber 应用程序在此应用程序中检查相同的想法时,在启动屏幕中按下主页按钮时,当应用程序在后台时,弹出窗口保持不变,但在我的代码中我无法做到。请帮我 。
谢谢你 问候, 尼桑特·钱德瓦尼
【问题讨论】:
【参考方案1】:为什么在应用启动时才按下主页按钮?
我建议您在显示的第一个 ViewController 中请求许可,并且您必须在请求许可之前初始化 CLLocationManager。为 CLLocationManager
使用单例也很方便,所以如果你有一个名为 YourLocationManager.swift
的类,你可以使用这个:
YourLocationManager.swift
class YourLocationManager: NSObject, CLLocationManagerDelegate
static let sharedInstance = YourLocationManager()
var locationManager: CLLocationManager?
func startLocationManager()
NSLog("Start UpdatedManagerLocation")
if locationManager == nil
NSLog("Initialize location Manager")
locationManager = CLLocationManager()
if let manager = locationManager
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.distanceFilter = 5.0
manager.delegate = self
manager.activityType = CLActivityType.Fitness
manager.pausesLocationUpdatesAutomatically = true
self.locationManager?.startUpdatingLocation()
func stopLocationManager()
NSLog("Stop LocationUpdatedManager")
self.locationManager?.stopUpdatingLocation()
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
print(locations)
// Maybe you also want to just print locations.last which is the last position received
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus)
// I recommend you to send a NSNotificationCenter to detect if the user changes it in the settings so that you can handle it
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
print("Error")
然后,如果您想请求用户许可,只需在您的 viewController 甚至您的 AppDelegate 中开始)您的位置管理器:
YourLocationManager.sharedInstance.startLocationManager()
然后你请求许可:
YourLocationManager.sharedInstance.locationManager?.requestWhenInUseAuthorization()
如果我不在我的 AppDelegate 中使用它,即使应用程序进入后台,要求用户权限的弹出窗口也会保留。
【讨论】:
以上是关于按下主页按钮时,Swift CLLocationManager 弹出窗口不会停留的主要内容,如果未能解决你的问题,请参考以下文章