如果没有wifi

时间:2015-07-28 14:03:28

标签: ios objective-c

您好我是Ios的初学者,在我的项目中,我使用CLLocationManager来获取当前位置,但是当我关闭时#34; Wifi"和我启用GPS服务,但当前位置没有在互联网上显示它显示错误消息我们如何使用不带互联网的GP获取当前位置(我使用i pad测试此应用程序)

我的代码如下:

- (void)viewDidLoad {

    [super viewDidLoad];

   locationManager = [[CLLocationManager alloc] init];

   locationManager.delegate = self;

    if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]){

        NSUInteger code = [CLLocationManager authorizationStatus];

        if (code == kCLAuthorizationStatusNotDetermined && ([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] || [locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])) {



            if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"]){
                [locationManager requestAlwaysAuthorization];

                [locationManager setDistanceFilter:10.0f];
                [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

            }

            else if([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"]) {
                [locationManager  requestWhenInUseAuthorization];

                [locationManager setDistanceFilter:10.0f];
                [locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];

            } else {
                NSLog(@"Info.plist does not contain NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription");
            }
        }
    }

    [locationManager startUpdatingLocation];

}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }

    NSLog(@"Resolving the Address");

    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);

        if (error == nil && [placemarks count] > 0) {

            placemark = [placemarks lastObject];

            adressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                                 placemark.subThoroughfare, placemark.thoroughfare,
                                 placemark.postalCode, placemark.locality,
                                 placemark.administrativeArea,
                                 placemark.country];
        }

        else {
            NSLog(@"%@", error.debugDescription);
        }

    } ];

}

1 个答案:

答案 0 :(得分:0)

在应用的info.plist文件中添加一行代码:NSLocationWhenInUseUsageDescription,它应该可以解决您的问题。