Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My CoreLocation based Swift app is not asking user's permission to access location

I am building a CoreLocation based app which which show user their location based on 6 parameters like Latitude, Longitude, Horizontal Accuracy, Altitude, Vertical Accuracy, Distance Traveled.

It suppose to ask user's permission to allow access of location at first time but I have tried resetting all simulator's settings too.

This is how my Main.storyboard look like

Gray part will be filled with map later.

This is how my View.Controller.swift look like:

//  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.
    }


}

This is my Info.plist file:

enter image description here

This is my Simulator:

enter image description here

I want output like this:

enter image description here

I have been stuck on this since past 2 days. Pint of beer for a geek who help me to get along with this.

like image 730
Shyam Bhimani Avatar asked Mar 12 '23 00:03

Shyam Bhimani


1 Answers

Move LocationManager.requestAlwaysAuthorization() to the viewDidAppear method.

EDIT:

Ok, you are asking requestAlwaysAuthorization but in info.plist you set the When in usage... key entry, so change the requestAlwaysAuthorization to the requestWhenInUseAuthorization

like image 93
tbilopavlovic Avatar answered Apr 26 '23 08:04

tbilopavlovic