11

I am new to IOS. I am trying to use Maps to get user's current location. The tutorial i am following is for IOS 10.

I went through this post and did everything it said but still it doesn't work Location Services not working in iOS 11

Following is my code

class ViewController: UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

    var locationManager=CLLocationManager()
    // @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var mapView: MKMapView!

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

        locationManager.delegate=self
        locationManager.desiredAccuracy=kCLLocationAccuracyBest
        locationManager.requestWhenInUseAuthorization()
        locationManager.startUpdatingLocation()
    }

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

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        let userLocation:CLLocation = locations[0]
        let latitude=userLocation.coordinate.latitude
        let longitude=userLocation.coordinate.longitude
        let latDelta:CLLocationDegrees=0.05
        let lonDelta:CLLocationDegrees=0.05
        let span=MKCoordinateSpan(latitudeDelta:latDelta, longitudeDelta:lonDelta)
        let location=CLLocationCoordinate2D(latitude:latitude,longitude:longitude)
        let region=MKCoordinateRegion(center:location,span:span)
        self.mapView.setRegion(region,animated:true)

    }



}

I have added following in my info.plist file

<key>NSLocationAlwaysUsageDescription</key>
    <string>Program requires GPS to track cars and job orders</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Program requires GPS to track cars and job orders</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Program requires GPS to track cars and job orders</string>

Any help would be greatly appreciated.

FargoArgoCargo
  • 119
  • 1
  • 1
  • 8
  • Define "doesn't work". That's not a useful description. What is the exact issue with the code you posted? – rmaddy Dec 15 '17 at 16:06
  • I am not able to get user's current location. It doesn't even give me the permission dialog? – FargoArgoCargo Dec 15 '17 at 16:07
  • Are you working in the simulator or a real device? If a simulator (and provided GPS works there), try resetting it and then re-installing it. If a device, try deleting the app and re-installing it. –  Dec 15 '17 at 16:13
  • @dfd How to reset GPS in simulator – FargoArgoCargo Dec 15 '17 at 16:24
  • No. The Simulator. *"Hardware|Erase all Content and Settings"*. (My memory says it changed in Xcode 9. Beware!) More importantly... please, be **100% sure** that the GPS works in the simulator! (I honestly do not know. I know the camera doesn't. So why - IMHO - should the GPS?) The behavior you've described can be... both. –  Dec 15 '17 at 17:26
  • 1
    @dfd GPS does work in simulator, but in the menu bar you need to go to Debug -> Location -> And then either do a custom or predefined. – Dallas Dec 15 '17 at 21:45
  • 1
    An ios project has several info.plist files, Make sure you're editing the correct one! – ComDubh Jun 15 '18 at 11:03

2 Answers2

22

If you are using the simulator, make sure you have a location chosen. Select the simulator, in the menu bar you need to go to Debug -> Location -> And then either do a custom or predefined.

If you've done that, try the below.

Rather than manually entering that text in the info.plist, I would just open it with the default editor.

Click the plus button that is shown in the blue highlighted area ( you can really select any of them) This will add a new row. enter image description here

Second, Type in Privacy - Location When ... Select the one that applies to you. enter image description here

After you have the left side filled out, double tap the right side and enter the text you want shown to the user. enter image description here

Dallas
  • 1,788
  • 2
  • 13
  • 24
2

There should be three descriptions in info.plst.

Info.plst

Valerika
  • 366
  • 1
  • 3
  • 8