IOS __connection_block_invoke_2 Swift 2.0 xCode 7.1 El Capitan 10.1 - ios

Running Xcode 7.1 on IOS 9.1 on a MBR with El Capitan 10.1. Wrote this simple piece of code that appears run but then doesn't update it would seem and noted this error message; is a silent crash?
"error in __connection_block_invoke_2: Connection interrupted"
Unclear how I can move forward with this? CLLocation does't appear to be updating itself after initially getting a correct value of sorts. Is it related to the above message?
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var Latitude: UILabel!
#IBOutlet weak var Longitude: UILabel!
#IBOutlet weak var HorizontalAccuracy: UILabel!
#IBOutlet weak var CurrentAlltitude: UILabel!
#IBOutlet weak var VerticalAccuracy: UILabel!
#IBOutlet weak var Distance: UILabel!
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
startLocation = nil
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let latestLocation: AnyObject = locations[locations.count - 1]
Latitude.text = String(format: "%.6f",
latestLocation.coordinate.latitude)
Longitude.text = String(format: "%.6f",
latestLocation.coordinate.longitude)
HorizontalAccuracy.text = String(format: "%.4f",
latestLocation.horizontalAccuracy)
CurrentAlltitude.text = String(format: "%.4f",
latestLocation.altitude)
VerticalAccuracy.text = String(format: "%.4f",
latestLocation.verticalAccuracy)
if startLocation == nil {
startLocation = latestLocation as! CLLocation
}
let distanceBetween: CLLocationDistance =
latestLocation.distanceFromLocation(startLocation)
Distance.text = String(format: "%.2f", distanceBetween)
}
func locationManager(manager: CLLocationManager,
didFailWithError error: NSError) {
}
#IBAction func ResetDistance(sender: AnyObject) {
startLocation = nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Doesn't seem to update my location if I walk around with my iPad?

Wrote a comment; double checked and found my target was 8.1; running on an iPad with 9.1. Changed it and now it works!!
Still getting that odd error message; but doesn't seem to stop it working correctly!!

Related

Swift 4 CoreLocation not working

why this code does not working? I guess it's something with didUpdateLocations overrated method? How I can correctly connect them with my labels and make them a live :)
import UIKit
import CoreLocation
class MainVC: UIViewController, CLLocationManagerDelegate {
var walking = false
var pause = false
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
var speed: CLLocationSpeed = CLLocationSpeed()
#IBOutlet weak var latitudeLabel: UILabel!
#IBOutlet weak var longitudeLabel: UILabel!
#IBOutlet weak var horizontalAccuracyLabel: UILabel!
#IBOutlet weak var verticalAccuracyLabel: UILabel!
#IBOutlet weak var distanceLabel: UILabel!
#IBOutlet weak var altitudeLabel: UILabel!
#IBOutlet weak var speedLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
locationManager.startUpdatingHeading()
startLocation = nil
// 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()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func startDistance(_ sender: UIButton) {
startLocation = nil
walking = true
}
#IBAction func stopDistance(_ sender: UIButton) {
walking = false
}
#IBAction func resetDistance(_ sender: UIButton) {
startLocation = nil
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let latestLocation: AnyObject = locations[locations.count - 1]
latitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.latitude)
longitudeLabel.text = String(format: "%.4f",latestLocation.coordinate.longitude)
horizontalAccuracyLabel.text = String(format: "%.4f",latestLocation.horizontalAccuracy)
verticalAccuracyLabel.text = String(format: "%.4f",latestLocation.verticalAccuracy)
altitudeLabel.text = String(format: "%.4f",latestLocation.altitude)
if walking == true {
if startLocation == nil {
startLocation = latestLocation as! CLLocation
speed = locationManager.location!.speed
speedLabel.text = String(format: "%.0f km/h", speed * 3.6)
}
let distanceBetween: CLLocationDistance =
latestLocation.distance(from: startLocation)
distanceLabel.text = String(format: "%.2f", distanceBetween)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading)
{
// capLabel.text = String(format: "%.4f",newHeading.magneticHeading)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
}
}
I also added: Location Always Usage Description in my info.plist
And i allowed Location in the Settings on my simulator too.
Thank you.
To configure always authorization in Swift 4 for location services which I can see you have written in your code self.locationManager.requestAlwaysAuthorization(), do the following:
There is a new key NSLocationAlwaysAndWhenInUsageDescription which is also required in your info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string> location permission text.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string> location permission text.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string> location permission text.</string>
Take a look in the documentation here
You are required to include the NSLocationWhenInUseUsageDescription
and NSLocationAlwaysAndWhenInUsageDescription keys in your app's
Info.plist file. (If your app supports iOS 10 and earlier, the
NSLocationAlwaysUsageDescription key is also required.) If those keys
are not present, authorization requests fail immediately.
So we will have a single prompt now.

fatal error: unexpectedly found nil while unwrapping an Optional value while printing on UILabel

I'm trying to display the users current location and calculate the distance he traveled and i'm getting error at displaying it on distance1.text, can somebody please help me. Thanks in advance and I'm very new to iOS app development, currently using swift3
import UIKit
import MapKit
import CoreLocation
class MapView1: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
#IBOutlet weak var mapView: MKMapView!
#IBOutlet weak var distance1: UILabel!
let locationManager = CLLocationManager()
var startLocation:CLLocation!
var lastLocation: CLLocation!
var traveledDistance:Double = 0
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
startLocation = nil
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//MARK: Location Delegate Methods
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center1 = CLLocationCoordinate2D(latitude: location!.coordinate.latitude , longitude: location!.coordinate.longitude)
let region1 = MKCoordinateRegion(center: center1, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region1, animated: true)
self.locationManager.stopUpdatingLocation()
if startLocation == nil {
startLocation = location
}
let distanceBetween: CLLocationDistance =
location!.distance(from: startLocation)
distance1.text = String(format: "%.2f", distanceBetween)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Error: " + error.localizedDescription)
}
}

What code does iPhone Compass use to display location coordinates even without an internet connection?

I have made an app that will display the co-ordinates of the user location. It works fine with Wifi connectivity but fails to display anything despite waiting for 2-3 minutes, when there isn't internet connection. I was curious how Compass, iphone's inbuilt app could display the coordinates in a matter of seconds after calibration.
My following code, which is written in swift, finds the address based on the co-ordinates from GPS. I'm just confused why this code isn't able to display latitude and longitude in the absence of internet connectivity.
Any help is greatly appreciated. Thank you!
import UIKit
import CoreLocation
class ViewController2: UIViewController, CLLocationManagerDelegate
{
#IBOutlet var locality: UILabel?
#IBOutlet var postalCode: UILabel?
#IBOutlet var AdministrativeArea: UILabel?
#IBOutlet var country: UILabel?
#IBOutlet var latitude: UILabel?
#IBOutlet var longitude: UILabel?
var currentLocation = CLLocation()
let locationManager = CLLocationManager()
override func viewDidLoad()
{
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
currentLocation = locationManager.location
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!)
{
CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in
if (error != nil)
{
println("Error: " + error.localizedDescription)
return
}
if placemarks.count > 0
{
let pm = placemarks[0] as! CLPlacemark
self.displayLocationInfo(pm)
}
else
{
println("Error with the data.")
}
})
}
func displayLocationInfo(placemark: CLPlacemark)
{
/
locality?.text = "\(placemark.locality)"
postalCode?.text = "\(placemark.postalCode)"
AdministrativeArea?.text = "\(placemark.administrativeArea)"
country?.text = "\(placemark.country)"
latitude?.text = "\(currentLocation.coordinate.latitude)"
longitude?.text = "\(currentLocation.coordinate.longitude)"
}

location constantly updating problems iOS Swift

I am getting the users current location and dropping this as a println(). The idea is that I am going to hit a button to get the new location, however currently the app keeps updating constantly (every second) instead. I have tried moving the getLocation() function inside my IBAction but that crashed the thing. I have updated the info.plist so thats not a problem. Heres le code:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
#IBOutlet var latitude : UILabel!
#IBOutlet var longitude : UILabel!
var locationManager = CLLocationManager()
var startLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
locationManager = CLLocationManager()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func findMyLocation(sender: AnyObject){
startLocation = nil
locationManager.startUpdatingLocation()
}
func getLocation(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
var userLocation:AnyObject = locations[0] as! CLLocation
var strlat = String(format: "%.4f", userLocation.coordinate.latitude)
var strlong = String(format: "%.4f",userLocation.coordinate.longitude)
latitude.text = String(format: "%.4f", userLocation.coordinate.latitude)
longitude.text = String(format: "%.4f",userLocation.coordinate.longitude)
println("latitude: " + strlat)
println("longitide: " + strlong)
if startLocation == nil {
startLocation = userLocation as! CLLocation
locationManager.stopUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!,
didFailWithError error: NSError!) {
}
}
Move the locationManager.startUpdatingLocation() to your findMyLocation function. This will start the locationManager when your button is pressed and begin calling the didUpdateLocations Inside your if startLocation == nil add locationManager.stopUpdatingLocation() this will stop the locationManager after you have set your startLocation var. Every time the user presses the button the process will run again.
One additional note, you should add more code into the didUpdateLocations to check the accuracy and timestamp of the location before you use it as it may not be a valid/accurate location for what you are trying to do.
UPDATE:
Had a chance to validate and the code will work with the changes suggested. Here is what your final code should look like. I am also assuming you have set your plist entries for locations services and your simulator is set to simulate locaitons.
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate{
#IBOutlet var latitude : UILabel!
#IBOutlet var longitude : UILabel!
var locationManager : CLLocationManager! = CLLocationManager()
var startLocation: CLLocation!
override func viewDidLoad() {
super.viewDidLoad()
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
#IBAction func findMyLocation(sender: AnyObject){
startLocation = nil
locationManager.startUpdatingLocation()
}
func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!){
var userLocation:AnyObject = locations[0] as! CLLocation
var strlat = String(format: "%.4f", userLocation.coordinate.latitude)
var strlong = String(format: "%.4f",userLocation.coordinate.longitude)
latitude.text = String(format: "%.4f", userLocation.coordinate.latitude)
longitude.text = String(format: "%.4f",userLocation.coordinate.longitude)
println("latitude: " + strlat)
println("longitide: " + strlong)
if startLocation == nil {
startLocation = userLocation as! CLLocation
locationManager.stopUpdatingLocation()
}
}
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("error with location manager: " + error.description)
}
}

What I am doing wrong with my CLLocation?

I am trying to make a simple app that shows:
Latitude
Longitude
Horizontal Accuracy
Altitude
Vertical Accuracy
Location From Start
My code is:
#IBOutlet weak var latitude: UILabel!
#IBOutlet weak var longitude: UILabel!
#IBOutlet weak var horizontalAccuracy: UILabel!
#IBOutlet weak var altitude: UILabel!
#IBOutlet weak var verticalAccuracy: UILabel!
#IBOutlet weak var distance: UILabel!
var locationManager: CLLocationManager = CLLocationManager()
var startLocation: CLLocation!
#IBAction func resetDistance(sender: AnyObject) {
startLocation = nil
}
func locationManager(manager: CLLocationManager!,
didUpdateLocations locations: [AnyObject]!)
{
var latestLocation: AnyObject = locations[locations.count - 1]
latitude.text = String(format: "%.4f",
latestLocation.coordinate.latitude)
longitude.text = String(format: "%.4f",
latestLocation.coordinate.longitude)
horizontalAccuracy.text = String(format: "%.4f",
latestLocation.horizontalAccuracy)
altitude.text = String(format: "%.4f",
latestLocation.altitude)
verticalAccuracy.text = String(format: "%.4f",
latestLocation.verticalAccuracy)
if startLocation == nil {
startLocation = latestLocation as! CLLocation
}
var distanceBetween: CLLocationDistance =
latestLocation.distanceFromLocation(startLocation)
distance.text = String(format: "%.2f", distanceBetween)
}
func locationManager(manager: CLLocationManager!,
didFailWithError error: NSError!) {
}
And then in the ViewDidLoad section:
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
startLocation = nil
I keep getting the error: on the latitude.text line (everything hooked up correctly)
fatal error: unexpectedly found nil while unwrapping an Optional value
(lldb)
How would I go about adding if let statements? Can you help me find the problem?
Thanks!
Welcome to Swift. You can't do anything with an AnyObject. Cast down like this:
let latestLocation = locations.last as! CLLocation
Also, you say "everything hooked up correctly", but I'm not sure I believe that; try doing println(latitude) to make sure.
Finally, this code is not going to work:
locationManager.requestWhenInUseAuthorization()
locationManager.startUpdatingLocation()
The problem is that the authorization request is asynchronous. Thus you possibly are starting to update locations before you've actually got authorization. You have to wait until you actually have authorization before you start asking for updates.
About how to get authorizaiton:
First add NSLocationWhenInUseUsageDescription in to your info.plist file
Then in viewDidload:
if NSString(string:UIDevice.currentDevice().systemVersion).doubleValue > 8 {
locationManager.requestWhenInUseAuthorization()
}
Then is this delegate method,start updateingLoation
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status != CLAuthorizationStatus.Denied{
locationManager.startUpdatingLocation()
}
}
Then when you get location,you can convert it to CLLocation as matt post
You may also refer to this link about details, this is an answer I answered before
Swift: Exception while trying to print CLLocationSpeed "unexpectedly found nil while unwrapping an Optional value"

Resources