在swift我正在使用CLLocationManager,问题是当应用程序启动然后2秒后(当显示splashScreen时)按主页按钮然后应用程序转到后台stat然后位置弹出显示仅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应用程序和优步应用程序在这个应用程序上按下主页按钮在启动画面时弹出是在后台的应用程序停留,但在我的代码我不能做。请帮我 。
三江源 问候, Nishant Chandwani
答案 0 :(得分:0)
为什么在应用启动时按下homeButton?
我建议您在显示的第一个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中使用它,即使应用程序变为后台,也会保留要求userpermission的弹出窗口。