xcode swift 应用程序,位置在模拟器上有效,但在实际设备上无效

标签 xcode swift

我正在使用 xcode 和 swift 为苹果制作一个运行(慢跑)应用程序,我使用的 xcode 版本是 7.0/7.1 当您开始新的运行时,会出现一个 map 并跟踪您正在运行的位置并显示时间、距离和步速。当我使用调试模拟器时,这一切都有效,但是当我将其放在运行 ios 8.4 的 ipad(我为其开发了应用程序)的设备上时, map 会出现,但不会显示位置,因此无法计算距离或步速当您尝试保存运行时,我收到一条错误消息:未找到位置。不确定在代码中要检查的内容与在模拟器上工作相比在实际设备上会有所不同。我尝试了在这个网站上找到的一些东西,但它们不起作用。我关闭了模拟位置并将调试模拟设置为无。完成此操作后,我删除了设备上的应用程序,退出了 Xcode,重新启动并在设备上重建了它。感谢您的帮助

  @IBAction func startPressed(sender: AnyObject) {
    startButton.hidden = true
    promptLabel.hidden = true

    timeLabel.hidden = false
    distanceLabel.hidden = false
    paceLabel.hidden = false
    stopButton.hidden = false

    seconds = 0.0
    distance = 0.0
    locations.removeAll(keepCapacity: false)
    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: "eachSecond:", userInfo: nil, repeats: true)
    startLocationUpdates()

    mapView.hidden = false
  }

    > // MARK: - CLLocationManagerDelegate extension NewRunViewController:
    > CLLocationManagerDelegate {   func locationManager(manager:
    > CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
    >     for location in locations as! [CLLocation] {
    >       let howRecent = location.timestamp.timeIntervalSinceNow

      if abs(howRecent) < 10 && location.horizontalAccuracy < 20 {
        //update distance
        if self.locations.count > 0 {
          distance += location.distanceFromLocation(self.locations.last)

          var coords = [CLLocationCoordinate2D]()
          coords.append(self.locations.last!.coordinate)
          coords.append(location.coordinate)

          let region = MKCoordinateRegionMakeWithDistance(location.coordinate, 500, 500)
          mapView.setRegion(region, animated: true)

          mapView.addOverlay(MKPolyline(coordinates: &coords, count: coords.count))
        }

        //save location
        self.locations.append(location)
      }
    }
  }
}

最佳答案

您是否请求用户允许使用他的 GPS 位置?您是否向 Info.plist 添加了键:

<key>NSLocationAlwaysUsageDescription</key>
    <string>Get Your Location </string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Get Your Location For </string>

你能发布你的代码吗?

关于xcode swift 应用程序,位置在模拟器上有效,但在实际设备上无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928296/

相关文章:

xcode - 如何知道 Cocoa 应用程序何时即将退出?

ios - quartz 2D : draw on a view without subclassing it?

arrays - 快速比较数组

swift - CAGradientLayer 框架无法正确显示

ios - 将本地通知与 CoreData 结合使用作为每月提醒

swift - 如何在给定 XCUITest 中的文本的情况下获取单元格的索引?

xcode - 创建新项目组时强制 Xcode 创建 Finder 文件夹

ios - 我如何设置它以使泛识别器增加一个值

swift - 如何在 Swift 中使用嵌套的便利可失败初始值设定项检查 nil?

iOS swift 3 : Unrecognized selected sent to instance when trying to Segue from custom tableView header button