iphone - 在 appDelegate 中获取用户位置

标签 iphone ios cllocationmanager cllocation appdelegate

这是场景。我有两个 UIViewControllers。可以说,PresentinLondonViewControllerNotPresentinLondonViewController。我想要首先加载的 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/

相关文章:

swift - 是否可以拥有两个不同的位置管理员授权?

iphone - 哪个工具栏后面的阴影效果更有效?

ios - 无法更改 UITableViewCell contentView 高度

IOS UIPickerView - 动态添加到静态表

ios - Swift - 使用闭包在 init 方法中设置属性

iphone - 谁能告诉我为什么我的代码无法创建数据库和表,然后在其中存储数据。

ios - Swift 按用户与 MKMapItems 的距离组织 UITableView

ios - 受密码保护的 iOS 自定义设置包

iphone - 如何将int值附加到字符串?

iphone - 如何在图像 (*.png) 周围绘制轮廓/描边 - 内部示例