我只能让我的CLLocationManager进行授权.(在ios8下迅速)我甚至添加了一个显式的requestAlwaysAuthorization调用(在ios7下我不需要使用objC)
func finishLaunch() {
//ask for authorization
let status = CLLocationManager.authorizationStatus()
if(status == CLAuthorizationStatus.NotDetermined) {
self.locationManager.requestAlwaysAuthorization();
}
else {
self.startMonitoring()
}
...
}
Run Code Online (Sandbox Code Playgroud)
回调永远不会得到任何东西,但NotDermined并没有向用户显示UIAlertView.
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if(status == CLAuthorizationStatus.NotDetermined) {
println("Auth status unkown still!");
}
self.startMonitoring()
}
Run Code Online (Sandbox Code Playgroud)
我做错了吗? - 对我来说感觉像个臭虫,但我想要一些反馈
我的Swift-iOS应用程序旨在显示用户在地图上的位置.但是,XCode调试控制台告诉我,我需要请求权限才能显示用户的位置.我想,我这样做,但对话永远不会出现.
这是错误消息,在我调用的ViewController下面 CLLocationManager::requestWhenInUseAuthorization()
错误:
2014-06-30 21:25:13.927 RowingTracker2 [17642:1608253]尝试在不提示位置授权的情况下启动MapKit位置更新.必须首先调用 - [CLLocationManager requestWhenInUseAuthorization]或 - [CLLocationManager requestAlwaysAuthorization].
视图控制器:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapview: MKMapView = nil
var locationmgr : CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationmgr = CLLocationManager()
locationmgr.requestWhenInUseAuthorization()
mapview.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Run Code Online (Sandbox Code Playgroud)
如何申请授权使用该位置?你可以在这里 …
我使用这里的代码:
import UIKit
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var window: UIWindow?
var locationManager: CLLocationManager!
var seenError : Bool = false
var locationFixAchieved : Bool = false
var locationStatus : NSString = "Not Started"
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
initLocationManager();
return true
}
// Location Manager helper stuff
func initLocationManager() {
seenError = false
locationFixAchieved = false
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.locationServicesEnabled
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
}
// Location …Run Code Online (Sandbox Code Playgroud) 这是我的VC代码:
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView.showsUserLocation = true
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
mapView.showsUserLocation = (status == .AuthorizedAlways)
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
}
Run Code Online (Sandbox Code Playgroud)
我还在plist文件中添加了NSLocationWhenInUseUsageDescription.我有CoreLocation和MapKit框架,任何想法为什么这不起作用?它不会在地图上显示用户位置,也不会打印出用户的坐标.在堆栈溢出时没有在网上找到任何东西.