在我的应用中,我需要检查用户的国家/地区,然后根据该状态显示警报。在AppDelegate文件中,我有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];
然后代表我有这个。它有效,但警报不断重现。我怎么只让它运行一次?
-(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];
}
}];
}
答案 0 :(得分:1)
而不是使用......
[self.locationManager startUpdatingLocation];
...告诉位置管理员每次获取位置信息时都会发送有关位置的更新,为什么不使用:
[self.locationManager requestLocation];
只会向您发送位置管理员获得的第一个位置。
答案 1 :(得分:0)
取一个bool值。它应该是第一次是假的。然后用这个替换你的代码。
-(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];
}
}
}];
}