Last active
July 5, 2018 07:01
-
-
Save neojou/def16ae96b84156f5eb6 to your computer and use it in GitHub Desktop.
iOS : Swift : to get current location from iphone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// GetLocation | |
// | |
// Created by neojou on 2014/11/23. | |
// Copyright (c) 2014年 neojou. All rights reserved. | |
// | |
import UIKit | |
import CoreLocation | |
class ViewController: UIViewController, CLLocationManagerDelegate { | |
@IBOutlet weak var longitude: UILabel! | |
@IBOutlet weak var lagitude: UILabel! | |
var locationManager : CLLocationManager! | |
var seenError : Bool = false | |
var curLocation : CLLocation? | |
var locationStatus : NSString = "Not Started" | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
self.initLocationManager() | |
} | |
override func didReceiveMemoryWarning() { | |
super.didReceiveMemoryWarning() | |
// Dispose of any resources that can be recreated. | |
} | |
func initLocationManager(){ | |
locationManager = CLLocationManager() | |
locationManager.delegate = self | |
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer | |
locationManager.requestAlwaysAuthorization() | |
locationManager.startUpdatingLocation() | |
} | |
// get error | |
func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!){ | |
locationManager.stopUpdatingLocation() | |
if ((error) != nil) { | |
if (seenError == false) { | |
seenError = true | |
println(error) | |
} | |
} | |
} | |
func locationManager(manager:CLLocationManager!, didUpdateLocations locations:[AnyObject]!) { | |
var locationArray = locations as NSArray | |
var locationObj = locationArray.lastObject as CLLocation | |
var coord = locationObj.coordinate | |
longitude.text = coord.longitude.description | |
lagitude.text = coord.latitude.description | |
} | |
func locationManager(manager: CLLocationManager!, | |
didChangeAuthorizationStatus status: CLAuthorizationStatus) { | |
var shouldIAllow = false | |
switch status { | |
case CLAuthorizationStatus.Restricted: | |
locationStatus = "Restricted Access to location" | |
case CLAuthorizationStatus.Denied: | |
locationStatus = "User denied access to location" | |
case CLAuthorizationStatus.NotDetermined: | |
locationStatus = "Status not determined" | |
default: | |
locationStatus = "Allowed to location Access" | |
shouldIAllow = true | |
} | |
NSNotificationCenter.defaultCenter().postNotificationName("LabelHasbeenUpdated", object: nil) | |
if (shouldIAllow == true) { | |
NSLog("Location to Allowed") | |
// Start location services | |
locationManager.startUpdatingLocation() | |
} else { | |
NSLog("Denied access: \(locationStatus)") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment