私はユーザーを見つけて、彼がいる国に応じていくつかの番号を変更するためのこの方法を持っています:
- (void) localizing {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
DbOperations *dbOp = [[DbOperations alloc] init];
geoCoder = [[CLGeocoder alloc] init];
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get nearby address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
countryCode = [placemark.addressDictionary valueForKey:@"CountryCode"];
country = [placemark.addressDictionary valueForKey:@"Country"];
NSString *s2 = NSLocalizedString(@"You're currently in %@", nil);
lblLocation.text = [NSString stringWithFormat:s2, self.country];
[dbOp UsefulNumbers:self.countryCode];
NSUserDefaults *loc = [NSUserDefaults standardUserDefaults];
[loc setObject:country forKey:@"Country"];
[loc setObject:countryCode forKey:@"CountryCode"];
[loc synchronize];
}];
[locationManager stopUpdatingLocation];
NSUserDefaults *loc = [NSUserDefaults standardUserDefaults];
if ([loc objectForKey:@"Country"] == NULL) {
btnUno.enabled = NO;
btnDue.enabled = NO;
btnTre.enabled = NO;
lblLocation.text = NSLocalizedString(@"Unable to locate you", nil);
}
}
メソッドは'viewDidLoad'メソッドによって呼び出されますが、アプリを初めて実行したときに、場所を特定できません。マップを開いて修正を取得してからアプリを開くと、すべてうまくいきます。GPS修正を取得するのに十分な時間がアプリに与えられていないか、間違っているだけかもしれないと思いました。
助言がありますか?
編集:コードを変更しましたが、まだ機能していません。
- (id) init {
self = [super init];
if (self != nil) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
}
return self;
}
- (void) viewDidLoad {
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
[locationManager startUpdatingLocation];
[super viewDidLoad];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
DbOperations *dbOp = [[DbOperations alloc] init];
geoCoder = [[CLGeocoder alloc] init];
[self.geoCoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get nearby address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
countryCode = [placemark.addressDictionary valueForKey:@"CountryCode"];
country = [placemark.addressDictionary valueForKey:@"Country"];
NSString *s2 = NSLocalizedString(@"You're currently in %@", nil);
lblLocation.text = [NSString stringWithFormat:s2, self.country];
[dbOp UsefulNumbers:self.countryCode];
NSUserDefaults *loc = [NSUserDefaults standardUserDefaults];
[loc setObject:country forKey:@"Country"];
[loc setObject:countryCode forKey:@"CountryCode"];
[loc synchronize];
NSLog(@"Country: %@", country);
}];
NSUserDefaults *loc = [NSUserDefaults standardUserDefaults];
if ([loc objectForKey:@"Country"] == NULL) {
btnUno.enabled = NO;
btnDue.enabled = NO;
btnTre.enabled = NO;
lblLocation.text = NSLocalizedString(@"unable to locate", nil);
}
}
メソッドlocationManager:didUpdateToLocation:fromLocationは呼び出されません。