Not getting authorization for location iOS8 - ios

I've tried looking at other solutions such as: requestWhenInUseAuthorization() not Work in iOS 8 With NSLocationWhenInUseUsageDescription Key in Info.plist
But I can't get authorization prompt to get location.
That person's issue in that other question was that he had is manager inside his viewDidLoad, I do not, but still an authorization prompt is not showing up. I've updated my plist:
and have my manager outside of my viewDidLoad:
import UIKit
import AVFoundation
import MapKit
class SecondViewController: UIViewController, CLLocationManagerDelegate {
let locationManager: CLLocationManager = CLLocationManager()
var player = AVPlayer()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
println("Got to end of locationmanager")
//playentryaudio() // User plugged in mic -- start playing entry audio.
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("Got to locationmngsr")
var userlocation = locations[0] as! CLLocation
var latitude: CLLocationDegrees = userlocation.coordinate.latitude
var longitude: CLLocationDegrees = userlocation.coordinate.longitude
println(latitude)
println(longitude)
} //snip rest of code
Since It's not getting auth, It's never hitting the locationManager function.

The prompt only shows if CLLocationManager.authorizationStatus() == .NotDetermined. If the user previously denied access, you won't see the prompt anymore. You should check the status before self.locationManager.requestWhenInUseAuthorization(). If it shows anything but .NotDetermined, look at the location settings for the app and the device.
If the status is .NotDetermined and the prompt still does not show, I would double check spelling of the NSLocationWhenInUseUsageDescription.

Related

how to get user latitude and longitude

I am trying to get a user's latitude and longitude values, but the CLLocationManagerDelegate's methods are not getting called. Thanks in advance to anyone who can help me solve this.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var locationmanager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationmanager.delegate = self;
locationmanager.desiredAccuracy = kCLLocationAccuracyBest
locationmanager.requestAlwaysAuthorization()
locationmanager.startUpdatingLocation()
}
//MARK: - location delegate methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation :CLLocation = locations[0] as CLLocation
print("user latitude = \(userLocation.coordinate.latitude)")
print("user longitude = \(userLocation.coordinate.longitude)")
}
}
You can typically okay. To access user's current location you need physical device.
Still, there are second way to simulate location on simulator using Simulate Location option available above console in toolbar. This option available when your application is running.
Click on it to see list of location and select location to get device location on simulator. It's will be not current location but Simulate Location will set simulator location to your selected location. See following image:
You can also add your custom GPX file to load your current location. You can also add your Customer Location from debug menu of simulator. See following image:
I hope this will help you.
import following framework
Import CoreLocation
if you want Display map
Import MapKit
You can determine user’s current location by declaring
CLLocationManager instance:
let locationManager = CLLocationManager()
In viewDidLoad() you have to instantiate the CLLocationManager class,
like so:
// 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()
}
Then in CLLocationManagerDelegate method you can get user's current
location coordinates:
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
print("locations = \(locValue.latitude) \(locValue.longitude)")
}
In the info.plist you will have to add
Make sure you set following privacy permission.
Also, make sure simulator’s location service is ON.
Privacy - Location Always and When In Use Usage Description
Privacy - Location When In Use Usage Description

current location update on Google Maps SDK for Xcode not working

I have been trying to implement google maps in my application. The first step what I want to do is to simply get is:
1. Get the app to ask permission to use current location
2. Find the users current location and have a blue dot marker to mark it.
3. Update and centre the map around the users current location.
I have tried various methods to get this working but always end up with an error or doesn't work. Initially when I simulated the code below, it did come up with the dialog about requesting current location permission and a blue dot but in the following simulations this seems to have disappeared and when changing my current location in simulator (by going to edit schemes and changing default location) , the new current location did not update. I have a feeling that the code isn't working beyond viewDidLoad. where am I going wrong?
Please can I get some advice on how to rectify these problems and get the above points working (as well as the simulator to always ask for permission for current location). Currently the code runs with no errors but only shows me a map of the UK. (I have already added NSLocationWhenInUse and NSLocationAlwaysUsage.... into info.plist. And I have disabled all breakpoints. I have also tried resetting simulator)
import UIKit
import GoogleMaps
class FirstViewController: UIViewController,CLLocationManagerDelegate {
#IBOutlet weak var googlemaps: GMSMapView!
//this is the connection from the storyboard to the view controller
override func viewDidLoad() {
super.viewDidLoad()
let camera = GMSCameraPosition.camera(withLatitude: 51.508742, longitude: -0.087891, zoom: 8.0)
let viewMap = GMSMapView.map(withFrame: CGRect.zero, camera: camera)
view = viewMap
viewMap.isMyLocationEnabled = true
let locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
override func loadView() {
super.viewDidLoad()
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
// verification of granted permission while app in use
let locationManager = CLLocationManager()
let mapView = GMSMapView()
if status == .authorizedWhenInUse {
locationManager.startUpdatingLocation()
// if permission granted- starting updating location
mapView.isMyLocationEnabled = true
mapView.settings.myLocationButton = true
//this is suppose to be button that is added when tapped centers to users location
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.first {
mapView.camera = GMSCameraPosition.camera(withTarget: location.coordinate, zoom: 20)
self.view = mapView
locationManager.stopUpdatingLocation()
}
}
}
}
}
Thanks in advance! I have spent days trying to work this out with no success :(
I was able to solve this problem by updating Cocoapods and ensuring that GoogleMaps is in my Podfile. Then in my viewController I had the following code which executed perfectly.
import UIKit
import GoogleMaps
class FirstViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: GMSMapView!
var locationManager = CLLocationManager()
var vwGMap = GMSMapView()
override func viewDidLoad() {
super.viewDidLoad()
let camera: GMSCameraPosition = GMSCameraPosition.camera(withLatitude: 22.300000, longitude: 70.783300, zoom: 10.0)
vwGMap = GMSMapView.map(withFrame: self.view.frame, camera: camera)
vwGMap.camera = camera
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.distanceFilter = 500
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
self.view = vwGMap
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
if (status == CLAuthorizationStatus.authorizedWhenInUse)
{
vwGMap.isMyLocationEnabled = true
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let newLocation = locations.last
vwGMap.camera = GMSCameraPosition.camera(withTarget: newLocation!.coordinate, zoom: 15.0)
vwGMap.settings.myLocationButton = true
self.view = self.vwGMap
let marker = GMSMarker()
marker.position = CLLocationCoordinate2DMake(newLocation!.coordinate.latitude, newLocation!.coordinate.longitude)
marker.map = self.vwGMap
}
}

unable to see locations: [CLLocation]

info.plist file:
<key>NSLocationWhenInUseUsageDescription</key>
<string>This project would like to know your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>location when app is in background</string>
As shown above, have added the key/value pairs for the info.plist
The problem i'm facing is that the first time i ran the app it prompted me for authorization, but, on subsequent runs the popup hasn't appeared.
second, I'm trying to print the locations from
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) using the print(location) but nothing is printed.
in the debug options i have set location as "city bicycle ride"
The code i'm using is as follows.
import UIKit
import MapKit
import CoreLocation
class ViewController: UIViewController, MKMapViewDelegate , CLLocationManagerDelegate {
#IBOutlet var map: MKMapView!
//use the locManager to get the users location
var locManager = CLLocationManager()
override func viewDidLoad() {
locManager.delegate = self
locManager.desiredAccuracy = kCLLocationAccuracyBest
locManager.requestWhenInUseAuthorization()
super.viewDidLoad()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print(locations)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
iOS will ask only once for location authorization. If the user denies, he will have to change this in the device settings manually. If he agrees, you will be able to obtain the location until he blocks it in the device settings.
So this code:
locManager.requestWhenInUseAuthorization()
will prompt a popup only 1 time. The problem is that you probably forget to call:
locManager.startUpdatingLocation()
You need to call this every time you want to start obtaining the location.
use this
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.requestLocation()
locationManager.requestWhenInUseAuthorization()
locationManager.requestAlwaysAuthorization()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}

Swift - User Location Issues

I've just started with swift and I'm having an issue. I've read the various threads about user location and map kits and can't solve my issue. I had the code running and could create regions as I wanted and I could zoom into the user location.
I've paired the code back to try and locate the issue and the code left is below. The issue is that the userlocation is coming back as a nil value when you try and run the simulator which crashes the app. What am I doing wrong as I've completed authorising user location so surely it shouldn't be coming back nil. At one point I had code to zoom on the user location AFTER initially setting a region elsewhere and calling a function to do the zoom, but if you initially try and call the user location its always nil so you can't initialise the map zooming into where the user is which is what I want.
import UIKit
import MapKit
class MapController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: MKMapView!
// MARK: - location manager to authorize user location for Maps app
var locationManager = CLLocationManager()
func checkLocationAuthorizationStatus() {
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
mapView.showsUserLocation = true
} else {
locationManager.requestWhenInUseAuthorization()
}
}
override func viewDidLoad() {
super.viewDidLoad()
checkLocationAuthorizationStatus()
var userLocation = locationManager.location
println("\(userLocation.coordinate.latitude)")
println("\(userLocation.coordinate.longitude)")
// Do any additional setup after loading the view.
}
}
Firstly, CLLocationManager updates user location asynchronously. That means that even after you call startUpdatingLocation() your location will be nil until location manager returns with the new location.
Secondly, in your code you are not actually calling this method. If you DO need to be able to store the user location then you should change your code to:
import UIKit
import MapKit
class MapController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var mapView: MKMapView!
// MARK: - location manager to authorize user location for Maps app
lazy var locationManager: CLLocationManager = {
var manager = CLLocationManager()
manager.delegate = self
return manager
}()
func checkLocationAuthorizationStatus() {
if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
mapView.showsUserLocation = true
locationManager.startUpdatingLocation()
} else {
locationManager.requestWhenInUseAuthorization()
}
}
override func viewDidLoad() {
super.viewDidLoad()
checkLocationAuthorizationStatus()
//location is nil at this point because location update is
//an asynchronous operation!
//var userLocation = locationManager.location
//println("\(userLocation.coordinate.latitude)")
//println("\(userLocation.coordinate.longitude)")
// Do any additional setup after loading the view.
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if let location = locations.last {
//this is the place where you get the new location
println("\(location.coordinate.latitude)")
println("\(location.coordinate.longitude)")
}
}
There is only one minor thing to note. In the last function I am using an argument locations: [CLLocation]. This is definitely correct in Swift 2.0, but in Swift 1.2 it might be locations: [AnyObject] in which case you have to do a conditional downcast yourself.
Let me know if this works for you

Getting user location in swift

This is my code:
import Foundation
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
#IBOutlet weak var locationLabel: UILabel!
var coord: CLLocationCoordinate2D?
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.authorizationStatus() == CLAuthorizationStatus.Authorized {
locationManager.startUpdatingLocation()
println(coord!.longitude)
locationLabel.text = "location found"
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as CLLocation
coord = locationObj.coordinate
}
}
}
This code does not return anything (it should print the longitude). Which seems O.K because if I move
locationManager.startUpdatingLocation()
println(coord!.longitude)
out of the if statement, Xcode throws a runtime error and says it unexpectedly found nil while unwrapping an Optional value. I do not understand why though? The systems asks me permission to use my location fine.
Thanks!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestAlwaysAuthorization()
locationManager.startUpdatingLocation()
}
func locationManager(manager:CLLocationManager, didUpdateLocations locations:AnyObject[] {
println("locations = \(locations)")
gpsResult.text = "success"
}
Are you running on iOS8? Assuming so:
1.You need to initialize
self.locationManager = CLLocationManager()
2.Right after, call self.locationManager.requestAlwaysAuthorization(); (before self.locationManager.delegate = self )
3.Add these keys to Info.plist, using an external text editor (change the texts accordingly)
<key>NSLocationWhenInUseUsageDescription</key>
<string>This is needed for iOS8 and up (when in use)</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This is needed for iOS8 and up (always)</string>
4.The place where you call println(coord!.longitude) may be too early. Move it into the locationManager function.
Then it should display an alert asking for permission for using location services.

Resources