Reputation: 111
In my app, I need to check the user's country, and then display an alert based on that. In the AppDelegate file, I have in didFinishLaunchingWithOptions:
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
//Here you set the Distance Filter that you need
self.locationManager.distanceFilter = kCLDistanceFilterNone;
// Here you set the Accuracy
self.locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[self.locationManager startUpdatingLocation];
Then for the delegate I have this. It works, but the alert is constantly re-appearing. How do I only have it run this once?
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"Running");
CLLocation *newLocation = [locations lastObject];
CLGeocoder *reverseGeocoder = [[CLGeocoder alloc] init];
[reverseGeocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
CLPlacemark *myPlacemark = [placemarks objectAtIndex:0];
NSString *countryCode = myPlacemark.ISOcountryCode;
NSString *countryName = myPlacemark.country;
NSLog(@"My country code: %@ and countryName: %@", countryCode, countryName);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([countryName isEqualToString:@"Thailand"]) {
[defaults setBool:NO forKey:@"TestMode"];
[defaults synchronize];
}
else {
[defaults setBool:YES forKey:@"TestMode"];
[defaults synchronize];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test Mode" message:@"Currently, live distribution is limited to the country of Thailand. You may still use the app in 'Test Mode' to store distribution data on your machine. When your country is added, you will have the option to upload it to our server." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}];
}
Upvotes: 0
Views: 65
Reputation: 1779
Instead of using...
[self.locationManager startUpdatingLocation];
...which tells the location manager to send updates about locations every time it gets a location update, why don't you use:
[self.locationManager requestLocation];
Which will only send you the first location the location manager gets.
Upvotes: 1
Reputation: 1486
Take a bool value. It should be false for the first time. Then replace your code with this.
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(@"Running");
CLLocation *newLocation = [locations lastObject];
[manager stopUpdatingLocation];
CLGeocoder *reverseGeocoder = [[CLGeocoder alloc] init];
[reverseGeocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
CLPlacemark *myPlacemark = [placemarks objectAtIndex:0];
NSString *countryCode = myPlacemark.ISOcountryCode;
NSString *countryName = myPlacemark.country;
NSLog(@"My country code: %@ and countryName: %@", countryCode, countryName);
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([countryName isEqualToString:@"Thailand"]) {
[defaults setBool:NO forKey:@"TestMode"];
[defaults synchronize];
}
else {
[defaults setBool:YES forKey:@"TestMode"];
[defaults synchronize];
if(boolvar == false){
boolvar = true
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Test Mode" message:@"Currently, live distribution is limited to the country of Thailand. You may still use the app in 'Test Mode' to store distribution data on your machine. When your country is added, you will have the option to upload it to our server." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
}
}];
}
Upvotes: 0