这是场景。我有两个 UIViewControllers。可以说,PresentinLondonViewController
和 NotPresentinLondonViewController
。我想要首先加载的 View 应该取决于当前用户。可以说,当且仅当当前用户位置是伦敦时,那么我希望 PresentViewController 显示,否则 NotPresentViewController。
我做了很多研究,即使尝试了协议(protocol)也找不到任何具体的东西。对我不起作用。
最佳答案
您可以使用CLLocationManager
获取您当前的位置,然后您可以进行反向地理编码
从坐标中获取地址。
在该地址中,如果准确的话,您会找到符合您需求的“伦敦
”一词。
在 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中执行此操作:
CLLocationManager *manager = [[CLLocationManager alloc] init];
manager.delegate = self;
//Here you set the Distance Filter that you need
manager.distanceFilter = kCLDistanceFilterNone;
// Here you set the Accuracy
manager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[manager startUpdatingLocation];
在委托(delegate)函数中:
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
//Picked the first placemark
CLPlacemark *placemark = placemarks[0];
//you can play and see how to grab the key name that you want.
NSLog(@"Address %@",[NSString stringWithFormat:@"%@ %@",placemark.thoroughfare,addressNumber]);
//Do your additional setup here
}];
[geocoder release];
[location release];
}
如果您想延迟加载,以便有时间获取所有信息,那么您可以提供另一个带有闪屏
的viewcontroller
并在那里/或在 appDelegate
中进行检查。
关于iphone - 在 appDelegate 中获取用户位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17521202/