Reputation: 2027
In my app there's a button to add records to DataBase. Every time user tap that button, he adds a record to the DB. Part of that record should be the location of the user when he tapped the button.
So, in my code, when button is tapped, I invoke a method which track the user position. Trouble is that iPhone can't find immediately user's position so record is saved without coordinates (if I create another record around 10 seconds later everything works correctly).
Is there a way to track user only when he pushes the button and adding coordinates when available?
Upvotes: 0
Views: 286
Reputation: 1324
@property(nonatomic,strong) CLLocationManager *locationManager;
in ViewDidLoad
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate=self;
if ([self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
{
[self.locationManager requestAlwaysAuthorization];
}
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
Then
-(IBAction)button_Clicked:(id)sender
{
[self.locationManager startUpdatingLocation];
}
#pragma mark - Location Manager delegate
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *location = [locations firstObject];
CLLocationCoordinate2D coordinate = location.coordinate;
}
Upvotes: 3