2016-11-08 67 views
0

在我的應用程序中,我需要檢查用戶的國家,然後顯示基於此的警報。在AppDelegate的文件,我在didFinishLaunchingWithOptions:iOS顯示警告取決於國家用戶在

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

取一個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]; 
     } 
} 
    }]; 

} 
+0

我認爲你的boolvar = true代碼行可能會搞砸了 – user717452

+0

在顯示警報之前,你只需要檢查該bool變量是否爲false,然後就會顯示警報。第一次它將是錯誤的,那麼你已經將它設置爲true,它將不會再次顯示警告。 – User511

+0

對,我只是說你的代碼在這個答案需要編輯它的格式,它不會讓我設置boolvar只需陳述'boolvar = true;' – user717452

1

而不是使用...

[self.locationManager startUpdatingLocation]; 

...它告訴外景經理到每一個它得到一個位置更新時間發送有關位置的更新,你爲什麼不使用:

[self.locationManager requestLocation]; 

哪個只會向您發送位置管理員獲得的第一個位置。

+0

使用了什麼委託方法,因爲它現在會崩潰應用程序。 – user717452

+0

請參閱以下文檔:https://developer.apple.com/reference/corelocation/cllocationmanager/1620548-requestlocation?language=objc(它使用的代理方法是:locationManager:didUpdateLocations: ) –

+0

在您的代碼中,它?你能發佈你的更新代碼嗎?你能發佈崩潰日誌嗎? –