简体   繁体   中英

How do I access CLLocationManager from within Swift?

Get User's Current Location / Coordinates has something that appears to be what I want:

you should do those steps:

  1. add CoreLocation.framework to BuildPhases -> Link Binary With Libraries
  2. import CoreLocation to your class - probably ViewController.swift
  3. add CLLocationManagerDelegate to your class decleration
  4. Add NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to plist
  5. init location manager:

     locationManager = CLLocationManager() locationManager.delegate = self; locationManager.desiredAccuracy = kCLLocationAccuracyBest locationManager.requestAlwaysAuthorization() locationManager.startUpdatingLocation() 
  6. get User Location By:

     func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) { var userLocation:CLLocation = locations[0] as! CLLocation let long = userLocation.coordinate.longitude; let lat = userLocation.coordinate.latitude; //Do What ever you want with it } 

I've poked around in Xcode, but I don't see where BuildPhases -> Link Binary with Libraries, or anything that follows.

If this is the approach I want, where in XCode do I add CoreLocation.framework to Link Binary with Libraries? If this is not the approach I want, how do I take advantage of Core Location?

Thanks,

You no longer have to manually link system frameworks (such as CoreLocation.framework) to your project. Just do a import CoreLocation at the top of the Swift file, and the framework will be seamlessly added for you (assuming you have the "Link Frameworks Automatically" setting to "Yes", which it is by default).

If you want to link to it manually, just go to "Build Phases" in your target settings:

在此处输入图片说明

In the Project navigator, the folder showing thingy to the left in xcode, click the project icon the one in the root of the project named like your project. Then in the top of the main window you will see several options, one of them being Build phases . Click that and you will find the option to add linked binaries.

Project navigator -> click project root -> Build phases -> linked binaries -> Add core locations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM