【问题标题】:iOS Display Alert Dependent On Country User IniOS 显示警报取决于国家/地区用户
【发布时间】:2016-11-08 04:59:58
【问题描述】:

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

     }];




}

【问题讨论】:

    标签: ios objective-c cllocationmanager clgeocoder


    【解决方案1】:

    而不是使用...

    [self.locationManager startUpdatingLocation];
    

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

    [self.locationManager requestLocation];
    

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

    【讨论】:

    • 使用什么委托方法,因为它当前使应用程序崩溃。
    • 参考文档:developer.apple.com/reference/corelocation/cllocationmanager/…(它使用的委托方法是:locationManager:didUpdateLocations:)
    • 你把它放在代码的什么地方?你能发布你更新的代码吗?您可以发布崩溃日志吗?
    • 这是因为我没有放入 didFailWithError 委托。
    • 啊,我没想到...很高兴知道...感谢您让我们知道额外的要求(我现在看到它在文档中,但没有注意到它当我之前看的时候)。
    【解决方案2】:

    取一个布尔值。第一次应该是假的。然后用这个替换你的代码。

    -(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];
             }
    }
         }];
    
    }
    

    【讨论】:

    • 我认为您的 boolvar = true 代码行可能有问题
    • 在显示警报之前,您只需检查该布尔变量是否为假,然后它将显示警报。第一次它会是假的然后你把它设置为真它永远不会再显示警报。
    • 对,我只是说你在这个答案中的代码需要编辑它的格式,它不会让我通过简单地声明boolvar = true;来设置布尔变量
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-27
    • 2013-05-19
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多