【问题标题】:xcode swift app, location works on simulator but not on actual devicexcode swift应用程序,位置在模拟器上有效,但在实际设备上无效
【发布时间】:2016-04-30 10:02:56
【问题描述】:

我正在使用 xcode 和 swift 为苹果制作一个跑步(慢跑)应用程序,我使用的 xcode 版本是 7.0/7.1 当您开始新的跑步时,会出现一张地图并跟踪您正在跑步的位置并显示时间、距离和速度。当我使用调试模拟器时,这一切都有效,但是当我把它放在设备上时,一个运行 ios 8.4 的 ipad,我为它开发了应用程序,地图出现但不显示位置,因此它无法计算距离或速度当您尝试保存运行时,我收到未找到位置的错误。与在模拟器上工作相比,不确定在实际设备上检查的代码会有所不同。我尝试了一些我在这个网站上找到的东西,但它们没有用。我关闭了模拟位置并将调试模拟设置为无。在我这样做之后,我删除了设备上的应用程序,退出 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)
      }
    }
  }
}

【问题讨论】:

    标签: xcode swift


    【解决方案1】:

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

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

    你能发布你的代码吗?

    【讨论】:

    • 是的,我做到了,一旦我在运行实际开始之前进入新的运行页面,它就会弹出
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多