Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer of RedBlueThing worked quite well for me. Here is some sample code of how I did it.</p> <h3>Header</h3> <pre><code>#import &lt;UIKit/UIKit.h&gt; #import &lt;CoreLocation/CoreLocation.h&gt; @interface yourController : UIViewController &lt;CLLocationManagerDelegate&gt; { CLLocationManager *locationManager; } @end </code></pre> <h3>MainFile</h3> <p>In the init method</p> <pre><code>locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyBest; [locationManager startUpdatingLocation]; </code></pre> <p>Callback function</p> <pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude); NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude); } </code></pre> <h2>iOS 6</h2> <p>In iOS 6 the delegate function was deprecated. The new delegate is</p> <pre><code>- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations </code></pre> <p>Therefore to get the new position use</p> <pre><code>[locations lastObject] </code></pre> <h2>iOS 8</h2> <p>In iOS 8 the permission should be explicitly asked before starting to update location</p> <pre><code>locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self; locationManager.distanceFilter = kCLDistanceFilterNone; locationManager.desiredAccuracy = kCLLocationAccuracyBest; if ([[[UIDevice currentDevice] systemVersion] floatValue] &gt;= 8.0) [self.locationManager requestWhenInUseAuthorization]; [locationManager startUpdatingLocation]; </code></pre> <p>You also have to add a string for the <a href="https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26" rel="noreferrer"><code>NSLocationAlwaysUsageDescription</code></a> or <a href="https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html#//apple_ref/doc/uid/TP40009251-SW26" rel="noreferrer"><code>NSLocationWhenInUseUsageDescription</code></a> keys to the app's Info.plist. Otherwise calls to <code>startUpdatingLocation</code> will be ignored and your delegate will not receive any callback.</p> <p>And at the end when you are done reading location call stopUpdating location at suitable place.</p> <pre><code>[locationManager stopUpdatingLocation]; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload