Location Demo in iOS 8 With Swift
Tech-Today

Location Demo in iOS 8 With Swift


What we will be doing is a simple app the tells your location base on the longitude and latitude.


   For making things simpler, I already set-up the UI in this link. Once you have downloaded the files go to storyboard and change the size class from wAny|hANy to wCompact|hRegular. Here is what the story board should look like:





First, add "CoreLocation" to the framework.

Once added, inside the "ViewController.swift" import CoreLocation and add CLLocationManagerDelegate.

Then declare the following variables:

    var locationManager : CLLocationManager = CLLocationManager()

var geocoder : CLGeocoder = CLGeocoder()
var placemark : CLPlacemark = CLPlacemark()

Then insert the following lines of code:

func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println("didFailWithError %@", error)
var errorAlert : UIAlertView = UIAlertView(title: "Error", message: "Failed to get your location", delegate: nil, cancelButtonTitle: "OK")

}

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
//println("didUpdateToLocation %@",locations)

var currentLocation : CLLocation = locations[0] as CLLocation

if currentLocation != nil{
var stringLongitude : NSString = NSString(format: "%0.8f", currentLocation.coordinate.longitude)
var stringLatitude : NSString = NSString(format: "%0.8f", currentLocation.coordinate.latitude)
lbl_longitude.text = stringLongitude
lbl_latitude.text = stringLatitude
}

locationManager.stopUpdatingLocation()


CLGeocoder().reverseGeocodeLocation(manager.location, completionHandler: {(placemarks, error)->Void in

if error {
//println(“Reverse geocoder failed with error” + error.localizedDescription)
println("Reverse geocode failed with error")
return
}

if placemarks.count > 0 {
let pm = placemarks[0] as CLPlacemark
self.displayLocationInfo(pm)
} else {
// println(“Problem with the data received from geocoder”)
println("Problem with the date recieved from geocoder")
}

})


}

func displayLocationInfo(placemark: CLPlacemark) {
if placemark != nil {

var tempString : String = ""

if(placemark.locality != nil){
tempString = tempString + placemark.locality + "\n"
}
if(placemark.postalCode != nil){
tempString = tempString + placemark.postalCode + "\n"
}
if(placemark.administrativeArea != nil){
tempString = tempString + placemark.administrativeArea + "\n"
}
if(placemark.country != nil){
tempString = tempString + placemark.country + "\n"
}

lbl_address.text = tempString

}
}

After inserting the lines of code, insert this lines of code inside the button method:


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

Last, inside the Info.plist, add NSLocationAlwaysUsageDescription and NSLocationWhenInUseUsageDescription with both boolean value set to YES.

And that's it. Build and Run the project and you should now be able see your address.




- Swift - Displaying Actionsheet For Ipad And Iphone
Swift - Displaying Actionsheet for Ipad and IphoneA simple tutorial on making an alert view of type action sheet that works for iphone and ipad. 1.) First you'll need a button where you can attach your pop over when you're using iPad (in iPhone...

- How To Display A Date With Textfield Click With Swift
Displaying Date with TextField Click with Swift First you must have a textfield connected to storyboard. @IBOutlet weak var txtField_helloDatePicker: UITextField! Next you should have a constant of UIDatePicker type. let datePicker...

- Simple To-do List Using Core Data In Swift
Note: The steps 1-9 are all simply about setting-up the controllers. The main "core data" begins the next step after. Here's the finish project from step-1 to step-9. 1.) First, for us to have a better view, we have to disable the size classes....

- How To Implement Local Notification In Swift
The following steps will guide us on how to develop an application in Swift that implements Local Notification. Requirements are:MacXcode6 - BetaXiOS8StepsOpen xcode.Create a Single View Application, let's name it LocalNotification.Register for user...

- Xcode How To Read Raw Data From Pipe-delimited Text File
Open Xcode and create a new Single View Application. For product name, use ReadPipeDelimitedTextfield and then fill out the Organization Name and Company Identifier with your customary values. Select iPhone for Devices. I have prepared a text file to...



Tech-Today








.