How can I get the current location from user in iOS?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- back button text does not change
相关文章
- 现在使用swift开发ios应用好还是swift?
- Could I create “Call” button in HTML 5 IPhone appl
- TCC __TCCAccessRequest_block_invoke
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- How can I add media attachments to my push notific
- How do you detect key up / key down events from a
- “Storyboard.storyboard” could not be opened
Try this Simple Steps....
NOTE: Please check device location latitude & logitude if you are using simulator means. By defaults its none only.
Step 1: Import
CoreLocation
framework in .h FileStep 2: Add delegate CLLocationManagerDelegate
Step 3: Add this code in class file
Step 4: Method to detect current location
Step 5: Get location using this method
The answer of RedBlueThing worked quite well for me. Here is some sample code of how I did it.
Header
MainFile
In the init method
Callback function
iOS 6
In iOS 6 the delegate function was deprecated. The new delegate is
Therefore to get the new position use
iOS 8
In iOS 8 the permission should be explicitly asked before starting to update location
You also have to add a string for the
NSLocationAlwaysUsageDescription
orNSLocationWhenInUseUsageDescription
keys to the app's Info.plist. Otherwise calls tostartUpdatingLocation
will be ignored and your delegate will not receive any callback.And at the end when you are done reading location call stopUpdating location at suitable place.
In iOS 6, the
is deprecated.
Use following code instead
For iOS 6~8, the above method is still necessary, but you have to handle authorization.
This is the delegate method which handle user's authorization
iOS 11.x Swift 4.0 Info.plist needs these two properties
And this code ... making sure of course your a CLLocationManagerDelegate
And of course this code too which you can put in viewDidLoad().
And these two for the requestLocation to get you going, aka save you having to get out of your seat :)
In Swift (for iOS 8+).
Info.plist
First things first. You need to add your descriptive string in the info.plist file for the keys
NSLocationWhenInUseUsageDescription
orNSLocationAlwaysUsageDescription
depending on what kind of service you are requestingCode
Usage
You can use this service I wrote to handle everything for you.
This service will request the permissions and handle dealing with the CLLocationManager so you don't have to.
Use like this: