swift ios9: Trying to start MapKit location updates without prompting for location authorization - ios

I write a easy example for mapView suing swift, but I get the print Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
I add a mapView to viewController and start location. I also call requestWhenInUseAuthorization() before startUpdatingLocation()
I set the Info.plist
now I set both NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription , it doesn't work.
Tere is my code, what's wrong?
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
var locationManager: CLLocationManager?
var mapView: MKMapView?
override func viewDidLoad() {
super.viewDidLoad()
let locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.distanceFilter = 10
locationManager.delegate = self
locationManager.pausesLocationUpdatesAutomatically = true
if CLLocationManager.locationServicesEnabled() {
let status: CLAuthorizationStatus = CLLocationManager.authorizationStatus()
if status == CLAuthorizationStatus.NotDetermined {
if #available(iOS 8.0, *) {
locationManager.requestWhenInUseAuthorization()
}
}
locationManager.startUpdatingLocation()
self.locationManager = locationManager
} else {
print("locationServices disenabled")
}
let mapview = MKMapView(frame: self.view.bounds)
mapview.mapType = .Standard
mapview.showsUserLocation = true
mapview.delegate = self
self.mapView = mapview
self.view.addSubview(mapview)
}
}

As the warning is telling you are missing one of the two required Strings into your plist
NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription

This might be the problem:
let locationManager = CLLocationManager()
you put let and you already declared like a variable:
var locationManager: CLLocationManager?
use it without "let"

Related

locationManager.location returns nil

i am using swift4.2 and Xcode 10 and i am trying to make iOS app uses location service and it gives me exception: Fatal error: Unexpectedly found nil while unwrapping an Optional value
so i tried to check if location returns nil so i copy my code and print location and it returns null , i simulated location in Xcode from product>scheme>edit scheme>default location and checked location in debug area and it simulated to location i choose any one know the problem?
import CoreLocation
class LocationVC: UIViewController,CLLocationManagerDelegate {
let locationManager = CLLocationManager()
var currentlocation:CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
authorizelocationstates()
}
func authorizelocationstates(){
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
currentlocation = locationManager.location
print(currentlocation)
}
else{
locationManager.requestWhenInUseAuthorization()
authorizelocationstates()
}
}
I have run your code and I just added missing key Privacy - Location When In Use Usage Description in info.plist file.
And I have made some changes in your code and get nil value because method getting called multiple time and when user give permission and method again called and going to print location detail but the fact is stilllocationManager variable has not user location data yet.
get location details in locationManager when delegate didUpdateLocations called
I have done some changes in your code:
import UIKit
import CoreLocation
class ViewController: UIViewController,CLLocationManagerDelegate {
var locationManager = CLLocationManager()
var currentlocation:CLLocation!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
// authorizelocationstates()
}
func authorizelocationstates(){
if CLLocationManager.authorizationStatus() == .authorizedWhenInUse {
currentlocation = locationManager.location
print(currentlocation)
}
else{
// Note : This function is overlap permission
// locationManager.requestWhenInUseAuthorization()
// authorizelocationstates()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
locationManager = manager
// Only called when variable have location data
authorizelocationstates()
}
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
// Get Location Permission one time only
locationManager.requestWhenInUseAuthorization()
// Need to update location and get location data in locationManager object with delegate
locationManager.startUpdatingLocation()
locationManager.startMonitoringSignificantLocationChanges()
}
}
Hope it will help you.

Swift - Trying to get current location [duplicate]

This question already has answers here:
Get User's Current Location / Coordinates
(16 answers)
Closed 6 years ago.
I am trying to get the long and lat of my users locations, I have done the following:
imported Core Location
import CoreLocation
Add Core Location Delegate
class ViewController: UIViewController, CLLocationManagerDelegate, AVCaptureMetadataOutputObjectsDelegate, DTDeviceDelegate {
defined this variable:
var locationManager: CLLocationManager!
and then added this method:
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let userLocation:CLLocation = locations[0]
let long = userLocation.coordinate.longitude;
let lat = userLocation.coordinate.latitude;
print(long, lat)
//Do What ever you want with it
}
but the location does not get printed, my method does not even get it.
I add the items to use Core Location to my plist and the app ask me to use the location services when I first run it. But now location is getting printed....what am I doing wrong?
Maybe you forgot to set value for the delegate
In your viewDidLoad() add this:
if CLLocationManager.locationServicesEnabled() {
locationManager.requestAlwaysAuthorization()
locationManager.requestWhenInUseAuthorization()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
mapView.showsUserLocation = true
}
Use this Code,
declare locationManager
var locationManager = CLLocationManager()
Set the Delegate in viewDidLoad, shown below code,
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
if CLLocationManager.authorizationStatus() == .NotDetermined {
self. locationManager.requestWhenInUseAuthorization()
}
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.startUpdatingLocation()
}
Add this in viewdidload,
if (CLLocationManager.locationServicesEnabled())
{
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
if ((UIDevice.currentDevice().systemVersion as NSString).floatValue >= 8)
{
locationManager.requestWhenInUseAuthorization()
}
locationManager.startUpdatingLocation()
}
else
{
#if debug
println("Location services are not enabled");
#endif
}
Then add this two delegate method to get location,
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!)
{
locationManager.stopUpdatingLocation()
if ((error) != nil)
{
print(error)
}
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
var locationArray = locations as NSArray
var locationObj = locationArray.lastObject as! CLLocation
var coord = locationObj.coordinate
print(coord.latitude)
print(coord.longitude)
}
This way you will get your current location.
You neeed to initialize the locationManageger and enable the location flag in xcode also, it appaear bottom of the xcode
self.locationManager = CLLocationManager()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
if #available(iOS 8.0, *) {
if Bundle.main.infoDictionary?["NSLocationAlwaysUsageDescription"] != nil {
self.locationManager.requestAlwaysAuthorization()
}
else {
self.locationManager.requestWhenInUseAuthorization()
}
}
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.startUpdatingLocation()
You can add this code to your viewDidLoad() method:
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
This will call the locationManagers didUpdate delegate-methods if location services are available for your app

Not getting authorization for location iOS8

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.

Detect when mapItemForCurrentLocation fails to locate your device

I can't figure out how to test for a lack of success when getting the map's current location.
let source = MKMapItem.mapItemForCurrentLocation()
// returns an object with:
// isCurrentLocation = 1
// name="Unknown location"
I could test source.name == "Unknown location" but that would be terrible and bad.
So... how do I detect failure/nil in this case?
Something like this ?
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
in viewDidLoad:
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
...
Delegates
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
if (!locations.isEmpty)
{
let myLocation = locations[0] as! CLLocation
mapView.setRegion(MKCoordinateRegionMake(CLLocationCoordinate2DMake(myLocation.coordinate.latitude, myLocation.coordinate.longitude),
MKCoordinateSpanMake(0.06, 0.06)), animated: true)
}
}

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