【问题标题】:Iphone simulator not getting locationIphone模拟器没有获取位置
【发布时间】:2016-09-19 08:59:10
【问题描述】:

我在 AppDelegate 中获取用户的经度和纬度,当我删除时它要求一次许可\在模拟器上再次重新安装后卸载应用程序它没有请求许可,所以应用程序在这里一直崩溃是我的代码

 func initLocationManager() {

    if( CLLocationManager.authorizationStatus() == CLAuthorizationStatus.AuthorizedWhenInUse ||
        CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized){

        seenError = false
        locationFixAchieved = false
        locationManager = CLLocationManager()
        locationManager.delegate = self
        //        locationManager.locationServicesEnabled = true
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()
        locationManager.requestAlwaysAuthorization()

    }
}

// Location Manager Delegate stuff

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
    locationManager.stopUpdatingLocation()
    if ((error) != nil) {
        if (seenError == false) {
            seenError = true
            print(error)
        }
    }
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    if (locationFixAchieved == false) {
        locationFixAchieved = true
        var locationArray = locations as NSArray
        var locationObj = locationArray.lastObject as! CLLocation
        var coord = locationObj.coordinate

        LATITUDE = coord.latitude
        LONGITUDE = coord.longitude

        print(coord.latitude)
        print(coord.longitude)
    }
}



func locationManager(manager: CLLocationManager!,
                     didChangeAuthorizationStatus status: CLAuthorizationStatus) {
    var shouldIAllow = false

    switch status {
    case CLAuthorizationStatus.Restricted:
        locationStatus = "Restricted Access to location"
    case CLAuthorizationStatus.Denied:
        locationStatus = "User denied access to location"
    case CLAuthorizationStatus.NotDetermined:
        locationStatus = "Status not determined"
    default:
        locationStatus = "Allowed to location Access"
        shouldIAllow = true
    }
    NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil)
    if (shouldIAllow == true) {
        NSLog("Location to Allowed")
        // Start location services
        locationManager.startUpdatingLocation()
    } else {
        NSLog("Denied access: \(locationStatus)")
    }
}

【问题讨论】:

  • 所以模拟器上不行?
  • 还是不行。如何快速获取用户位置
  • 你在哪里打电话给requestWhenInUseAuthorization
  • 它适用于模拟器。点击simulator.go to debug menu in xcode.than go to location.you can select location from here
  • 我正在使用requestAlwaysAuthorization

标签: ios swift xcode ios-simulator core-location


【解决方案1】:

第一次尝试在 didFinishLaunchingWithOptions 获取用户位置,然后调用此方法获取更新的用户位置。

locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()


func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[]) {
println("locations = \(locations)")
gpsResult.text = "success"
}

【讨论】:

  • 但是用户位置每次都会改变
  • 我想在每次应用启动时获取用户位置
  • 我的答案适用于每次启动应用程序时获取用户位置。获取更新的用户位置呼叫
猜你喜欢
  • 2014-11-13
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 2020-10-14
  • 2011-01-04
  • 2010-09-17
  • 1970-01-01
相关资源
最近更新 更多