iOS 显示警报取决于用户所在的国家/地区

标签 ios objective-c cllocationmanager clgeocoder

在我的应用中,我需要检查用户所在的国家/地区,然后根据该国家/地区显示提醒。在 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];
         }

     }];




}

最佳答案

而不是使用...

[self.locationManager startUpdatingLocation];

...告诉位置管理器在每次获得位置更新时发送有关位置的更新,你为什么不使用:

[self.locationManager requestLocation];

这只会向您发送位置管理器获得的第一个位置。

关于iOS 显示警报取决于用户所在的国家/地区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40479305/

相关文章:

objective-c - iOS 6 UIGestures (Tap) 停止使用 QLPreviewController

swift - 在 Swift 4 更新后获取用户的位置不起作用

ios - 从 viewDidAppear 呈现时,模态弹出框 Controller 不显示其内容

ios - 覆盖 UIImage(名为 : )

ios - 如何从 GPUImageHarrisCornerDetector 获取角坐标?

objective-c - UITableViewController 在网络请求完成之前执行延迟功能

ios - 带有 "When In Use"位置访问错误代码 4 的 CLLocationManager requestStateForRegion

objective-c - iOS 应用程序后台位置(推送通知)

ios - UITableViewCell.accessoryView 将在 iOS 7 中消失

ios - 三.js:为什么CanvasRenderer的大小会影响iPhone5s浏览器(chrome、safari)的性能