Getting user location in swift - ios

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.

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.

Location manager stops working after reboot swift ios

I am trying to use location manager in my application. I set the code as I saw in many posts and examples and everything is working fine, and I get the location manager to show the current location as set in the simulator. I then change the location in the simulator and my app gets the new location.
I then reboot my machine, and restart my application, but the location manager stops working. I tried to change the code from using addObserver() to using didUpdateLocations, and sometimes I manage to restart the location manager to work. But when I reboot the machine it stops working again. I tried resetting the simulator and authorizing again the app to get location updates, but with no success. In all cases my app prints that it has authorization to get location update but it does not work. I also tried cleaning the app and rebuilding it, but with no success.
Below are the 2 versions of the code I use.
CODE with addObserver
class TViewController: UIViewController, GMSMapViewDelegate, CLLocationManagerDelegate {
var myLocation: CLLocation?
var locationManager = CLLocationManager()
#IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView!.settings.myLocationButton = true
}
// get updates of myLocation
override func observeValueForKeyPath(keyPath: String?,
ofObject object: AnyObject?,
change: [String : AnyObject]?,
context: UnsafeMutablePointer<Void>)
{
myLocation = change![NSKeyValueChangeNewKey] as? CLLocation
print("in newlocation = \(myLocation)")
}
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == CLAuthorizationStatus.AuthorizedWhenInUse {
print("in myLocation is authorized")
locationManager.startUpdatingLocation()
mapView!.myLocationEnabled = true
mapView!.addObserver(self, forKeyPath: "myLocation", options: NSKeyValueObservingOptions.New, context: nil)
}
}
}
CODE with didUpdateLocations
extension TViewController: CLLocationManagerDelegate {
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .AuthorizedWhenInUse {
print("in myLocation extension is authorized")
isLocationAvailable = true
locationManager.startUpdatingLocation()
mapView.myLocationEnabled = true
mapView.settings.myLocationButton = true
}
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
myLocation = locations.first
print("in newlocation extension = \(myLocation)")
mapView.camera = GMSCameraPosition(target: myLocation!.coordinate, zoom: 15, bearing: 0, viewingAngle: 0)
}
class TViewController: UIViewController, GMSMapViewDelegate {
var myLocation: CLLocation?
var locationManager = CLLocationManager()
#IBOutlet weak var mapView: GMSMapView!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
mapView!.settings.myLocationButton = true
}

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

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

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"

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)
}
}

Resources