- swift >= 2.0
- Deployment Target == 9.1
corelocation.framework をLinked
info.plistに NSLocationAlwaysUsageDescription:String(アプリの説明文) を追加
ViewController.swift
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
var lm: CLLocationManager! = nil
override func viewDidLoad() {
super.viewDidLoad()
lm = CLLocationManager()
lm.delegate = self
lm.requestAlwaysAuthorization()
lm.desiredAccuracy = kCLLocationAccuracyBest
lm.distanceFilter = 300
lm.startUpdatingLocation()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
/** 位置情報取得 成功 */
func locationManager(manager: CLLocationManager, didUpdateToLocation newLocation: CLLocation, fromLocation oldLocation: CLLocation){
print("緯度:"+String(newLocation.coordinate.latitude))
print("経度:"+String(newLocation.coordinate.longitude))
}
/** 位置情報取得 失敗 */
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Error")
}
}