2016-06-11 4 views
4

Ich baue eine CoreLocation basierte Anwendung, die die zeigt Benutzer ihre Position anhand von 6 Parametern wie Breite, Länge, Horizontal Genauigkeit, Höhe, vertikale Genauigkeit, zurückgelegte Entfernung .Meine Corelocation basierte Swift App Erlaubnis des Benutzers nicht zu fragen Standort zugreifen

Es wird vorausgesetzt, die Erlaubnis des Benutzers zu fragen, den Zugriff auf den Ort zum ersten Mal zu erlauben, aber ich habe versucht, auch alle Einstellungen des Simulators zurückzusetzen.

This is how my Main.storyboard look like

Grau Teil wird später mit Karte gefüllt werden.

Dies ist, wie mein View.Controller.swift wie folgt aussehen:

// Created by 16246 on 6/7/16. 
// Copyright © 2016 16246. All rights reserved. 
// 

import UIKit 
import CoreLocation 

class ViewController: UIViewController, CLLocationManagerDelegate { 
    private let LocationManager = CLLocationManager() 
    private var previousPoint:CLLocation? 
    private var totalMovementDistance:CLLocationDistance = 0 

    @IBOutlet var latitudeLabel: UILabel! 
    @IBOutlet var longitudeLabel: UILabel! 
    @IBOutlet var horizontalAccuracy: UILabel! 
    @IBOutlet var altitudeLabel: UILabel! 
    @IBOutlet var verticalAccuracyLabel: UILabel! 
    @IBOutlet var distanceTraveledLabel: UILabel! 



    override func viewDidLoad() { 
     super.viewDidLoad() 
     LocationManager.delegate = self 
     LocationManager.desiredAccuracy = kCLLocationAccuracyBest 
     LocationManager.requestAlwaysAuthorization() 


     // Do any additional setup after loading the view, typically from a nib. 
    } 

    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 
     print("Authorization Status Changed to \(status.rawValue)") 
     switch status { 
     case .Authorized, .AuthorizedWhenInUse: 
      LocationManager.startUpdatingLocation() 
     default: 
      LocationManager.stopUpdatingLocation() 
     } 
    } 

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) { 
     let errorType = error.code == CLError.Denied.rawValue ? "Access Denied": "Error \(error.code)" 
     let alertController = UIAlertController(title: "Location Manager Error", message: errorType, preferredStyle: .Alert) 
     let okAction = UIAlertAction(title: "OK", style: .Cancel, handler: {action in}) 
     alertController.addAction(okAction) 
     presentViewController(alertController, animated: true, completion: nil) 
    } 

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 
     let newLocation = (locations as [CLLocation]) [locations.count-1] 

     let latitudeString = String(format: "%g\u{00B0}", newLocation.coordinate.latitude) 
     latitudeLabel.text = latitudeString 

     let longitudeString = String(format: "%g\u{00B0}", newLocation.coordinate.longitude) 
     longitudeLabel.text = longitudeString 

     let horizontalAccuracyString = String(format: "%g\u{00B0}", newLocation.horizontalAccuracy) 
     horizontalAccuracy.text = horizontalAccuracyString 

     let altitudeString = String(format: "%g\u{00B0}", newLocation.altitude) 
     altitudeLabel.text = altitudeString 

     let verticalAccuracyString = String(format: "%g\u{00B0}", newLocation.verticalAccuracy) 
     verticalAccuracyLabel.text = verticalAccuracyString 

     if newLocation.horizontalAccuracy < 0 { 
      return 
     } 

     if newLocation.horizontalAccuracy > 100 || 
      newLocation.verticalAccuracy > 50 { 
       return 
     } 

     if previousPoint == nil { 
      totalMovementDistance = 0 
     } else { 
      totalMovementDistance += newLocation.distanceFromLocation(previousPoint!) 
     } 

     previousPoint = newLocation 

     let distanceString = String(format: "%gm", totalMovementDistance) 
     distanceTraveledLabel.text = distanceString 

    } 

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


} 

Das ist mein Info.plist Datei:

enter image description here

Das ist mein Simulator :

enter image description here

Ich möchte Ausgang wie folgt aus:

enter image description here

Ich habe seit letzten 2 Tagen auf diese aufgeklebt. Pint Bier für einen Aussenseiter, die mir helfen, damit auszukommen.

+0

Überprüfen Sie die Einstellungen Ihrer Anwendung und die Erlaubnis sicher Lage machen ändern muss nicht wurde bereits genehmigt/abgelehnt – Paulw11

+0

Ich hatte nie diese Pop-up-Funktion, um den Standortdienst zulassen/ablehnen zu lassen. Ich habe bereits Simulator 100s Zeit zurückgesetzt und versucht zu laufen, aber immer noch nicht in der Lage Popup zu sehen @ Paulw11 –

Antwort

6

Verschieben Sie LocationManager.requestAlwaysAuthorization() zu der viewDidAppear Methode.

EDIT:

Ok, Sie fragen requestAlwaysAuthorization aber in info.plist stellen Sie die When in usage... Tasteneingabe, so die requestAlwaysAuthorization zum requestWhenInUseAuthorization

+0

Ich habe nicht ViewDidAppear-Methode in meinem View.Controller.Swift obwohl –

+1

überschreiben Sie einfach diese Methode ... – tbilopavlovic

+0

versuchte dies, aber keinen Unterschied 'override func viewDidAppear (animiert: Bool) { LocationManager.requestAlwaysAuthorization() }' –

Verwandte Themen