如何在没有用户共享/授予权限的情况下使用Core Location?

时间:2012-11-20 07:06:17

标签: objective-c ios xcode core-location

我已在我的应用中实施了核心位置。

当用户允许在应用程序启动时共享位置时,一切都很完美 但是当用户不共享他的位置时,“南大西洋”会显示在应用程序的位置字段中。

应该采取什么措施来避免这种情况以及当用户不共享其位置时应该显示的内容。

我正在使用此代码获取位置:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

    [self.window makeKeyAndVisible];


    locationManager = [[CLLocationManager alloc] init];
    locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [locationManager startUpdatingLocation];

    CLGeocoder *ceo = [[CLGeocoder alloc]init];
    CLLocation *loc = [[CLLocation alloc]initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude]; //insert your coordinates

    [ceo reverseGeocodeLocation: loc completionHandler:
     ^(NSArray *placemarks, NSError *error)
     {
         CLPlacemark *placemark = [placemarks objectAtIndex:0];
         NSLog(@"placemark %@",placemark);
         //String to hold address
         locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
         NSLog(@"addressDictionary %@", placemark.addressDictionary);

         NSLog(@"placemark %@",placemark.region);
         NSLog(@"placemark %@",placemark.country);  // Give Country Name
         NSLog(@"placemark %@",placemark.locality); // Extract the city name
         NSLog(@"location %@",placemark.name);
         NSLog(@"location %@",placemark.ocean);
         NSLog(@"location %@",placemark.postalCode);
         NSLog(@"location %@",placemark.subLocality);

         NSLog(@"location %@",placemark.location);
         //Print the location to console
         NSLog(@"I am currently at %@",locatedAt);
     }];


    return YES;
}



- (NSString *)deviceLocation
{
    NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
    return theLocation;

}

2 个答案:

答案 0 :(得分:4)

您可以检查[CLLocationManager authorizationStatus]以验证用户是否允许使用

之类的内容服务
int result = [CLLocationManager authorizationStatus];

它的返回值为

typedef enum {
    kCLAuthorizationStatusNotDetermined = 0,
    kCLAuthorizationStatusRestricted,
    kCLAuthorizationStatusDenied,
    kCLAuthorizationStatusAuthorized
} CLAuthorizationStatus;

答案 1 :(得分:2)

有关如何获取用户位置的信息,请参阅Location Awareness Programming Guide。最重要的是,您必须创建CLLocationManager位置管理器(就像您一样),然后启动位置服务(低功耗重要更改位置服务或标准位置服务)并等待{{ 3}}方法CLLocationManagerDelegate返回一个位置(或didUpdateLocations报告错误,因为应用程序授权失败,或者因为某些原因设备无法满足位置请求) 。但是,您无法从CLLocationManager抓取位置。在您可靠地开始使用经度和纬度之前,您必须等待服务呼叫didUpdateLocations

didFailWithError将引导您完成所有这些操作,包括一些非常有用的代码示例。