如何在Xcode 6.3.2中请求用户位置

时间:2015-06-17 15:19:18

标签: swift dictionary ios8 xcode6

我在尝试使用该应用时请求用户的位置。我已将NSLocationWhenInUseUsage添加到我的plist中。该应用程序不会崩溃或以其他方式显示任何错误。该应用程序刚刚启动,并且没有任何弹出窗口请求用户的权限。

import UIKit
import MapKit 
import CoreLocation

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var map: MKMapView!

var locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.


    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    locationManager.requestWhenInUseAuthorization()

我还将此功能添加到我的代码中:

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {

    println(locations)

另外,我不是一个熟练的程序员,所以更简单的答案会更好。

2 个答案:

答案 0 :(得分:0)

  

没有任何弹出窗口请求用户的许可

即使您拨打locationManager.requestWhenInUseAuthorization(),也不会有对话,除非(1)位置服务已开启,(2)此应用的位置授权未确定。

换句话说,仅当用户可以进行有意义的授权时,才会显示该对话框。如果位置服务不可用,授权是否已被拒绝,或者此设备是否受限制,则用户无法授权。

答案 1 :(得分:0)

正如亚特在回答中所说,你需要先检查授权状态。如果你没有确定,那么你应该致电requestWhenInUseAuthorization。

完成后,您需要等待调用您的委托方法locationManager:didChangeAuthorizationStatus。当用户完成授权位置服务(或拒绝它)时,将调用委托方法。

相关问题