2017-10-07 2 views
0

왜이 코드가 작동하지 않습니까? didUpdateLocations가 과대 평가 된 메소드가있는 것 같군요? 어떻게 제대로 내 레이블을 연결하고 라이브 만들 :Swift 4 CoreLocation이 작동하지 않습니다.

import UIKit 
import CoreLocation 

class MainVC: UIViewController, CLLocationManagerDelegate { 

var walking = false 
var pause = false 
var locationManager: CLLocationManager = CLLocationManager() 
var startLocation: CLLocation! 
var speed: CLLocationSpeed = CLLocationSpeed() 

@IBOutlet weak var latitudeLabel: UILabel! 
@IBOutlet weak var longitudeLabel: UILabel! 

@IBOutlet weak var horizontalAccuracyLabel: UILabel! 
@IBOutlet weak var verticalAccuracyLabel: UILabel! 

@IBOutlet weak var distanceLabel: UILabel! 
@IBOutlet weak var altitudeLabel: UILabel! 

@IBOutlet weak var speedLabel: UILabel! 

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

    locationManager.desiredAccuracy = kCLLocationAccuracyBest 
    locationManager.delegate = self 
    locationManager.requestWhenInUseAuthorization() 
    locationManager.startUpdatingLocation() 
    locationManager.startUpdatingHeading() 
    startLocation = nil 

    // Ask for Authorisation from the User. 
    self.locationManager.requestAlwaysAuthorization() 

    // For use in foreground 
    self.locationManager.requestWhenInUseAuthorization() 

    if CLLocationManager.locationServicesEnabled() { 
     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters 
     locationManager.startUpdatingLocation() 
    } 
} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

@IBAction func startDistance(_ sender: UIButton) { 
    startLocation = nil 
    walking = true 
} 

@IBAction func stopDistance(_ sender: UIButton) { 
    walking = false 
} 

@IBAction func resetDistance(_ sender: UIButton) { 
    startLocation = nil 
} 

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

    let latestLocation: AnyObject = locations[locations.count - 1] 

    latitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.latitude) 
    longitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.longitude) 

    horizontalAccuracyLabel.text = String(format: "%.4f",latestLocation.horizontalAccuracy) 
    verticalAccuracyLabel.text = String(format: "%.4f",latestLocation.verticalAccuracy) 

    altitudeLabel.text = String(format: "%.4f",latestLocation.altitude) 

    if walking == true { 
     if startLocation == nil { 
      startLocation = latestLocation as! CLLocation 
      speed = locationManager.location!.speed 
      speedLabel.text = String(format: "%.0f km/h", speed * 3.6) 
     } 

     let distanceBetween: CLLocationDistance = 
      latestLocation.distance(from: startLocation) 

     distanceLabel.text = String(format: "%.2f", distanceBetween) 
    } 
} 


func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) 
{ 
// capLabel.text = String(format: "%.4f",newHeading.magneticHeading) 
} 


func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) { 

} 

} 

또한 추가 할 수 있습니다 위치 항상 사용 설명을 제의 Info.plist 에 그리고 난 너무 내 시뮬레이터에 설정에서 위치를 허용했다.

감사합니다.

+0

정확히 같은 문제가 있습니다. 디버깅 후 didUpdateLocations가 어떤 이유로 호출하지 않는다고 생각합니다. –

+0

"작동하지 않음"을 정의하십시오. 그것은 쓸데없는 설명입니다. [편집] 귀하의 질문에 명확하게 당신의 코드가 당신이 기대하는 일을하지 않는 방법을 설명합니다. 컴파일러 오류/경고가 표시됩니까? 앱이 충돌하고 있습니까? 오류 메시지가 있습니까? 코드가 예상대로 실행되지 않습니까? 그래서, 어떤 방법으로? 문제에 대해 명확하고 구체적으로 설명하십시오. – rmaddy

+0

실제 장치에서 코드를 실행하십시오. 또는 시뮬레이터 위치 디버그 기능을 사용하십시오. – Aks

답변

0

다음을 수행, 난 당신이 코드 self.locationManager.requestAlwaysAuthorization()에서 작성한 볼 수있는 위치 서비스를 에서 스위프트 4을 항상 인증을 구성하려면 :

도에 요구되는 새로운 키 NSLocationAlwaysAndWhenInUsageDescription이 당신의 info.plist

<key>NSLocationAlwaysUsageDescription</key> 
    <string> location permission text.</string> 
<key>NSLocationWhenInUseUsageDescription</key> 
    <string> location permission text.</string> 
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key> 
    <string> location permission text.</string> 

하면 문서에서 살펴 보자 here

당신은 NSLocationWhenInUseUsageDescription 및 앱의 의 Info.plist 파일에 NSLocationAlwaysAndWhenInUsageDescription 키를 포함해야합니다. (앱이 iOS 10 이하를 지원하는 경우 NSLocationAlwaysUsageDescription 키가 필요합니다. 해당 키 이없는 경우 즉시 승인 요청이 실패합니다.

이제 한 번 프롬프트가 표시됩니다. enter image description here

관련 문제