Location Services not working in iOS 8

前端 未结 26 2499
滥情空心
滥情空心 2020-11-21 10:23

My app that worked fine on iOS 7 doesn\'t work with the iOS 8 SDK.

CLLocationManager doesn\'t return a location, and I don\'t see my app under

26条回答
  •  别那么骄傲
    2020-11-21 10:45

    Objective-C Procedure Follow the below instructions:

    For iOS-11 For iOS 11 have a look at this Answer: iOS 11 location access

    Need to Add two Keys into plist and provide message as below image:

     1. NSLocationAlwaysAndWhenInUseUsageDescription 
     2. NSLocationWhenInUseUsageDescription
     3. NSLocationAlwaysUsageDescription
    

    For iOS-10 and below:

    NSLocationWhenInUseUsageDescription
    

    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
    if([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
        [locationManager requestWhenInUseAuthorization];
    }else{
        [locationManager startUpdatingLocation];
    } 
    

    Delegate Methods

    #pragma mark - Lolcation Update 
    - (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 didChangeAuthorizationStatus:(CLAuthorizationStatus)status
    {
        switch (status) {
            case kCLAuthorizationStatusNotDetermined:
            case kCLAuthorizationStatusRestricted:
            case kCLAuthorizationStatusDenied:
            {
                // do some error handling
            }
                break;
            default:{
                [locationManager startUpdatingLocation];
            }
                break;
        }
    }
    - (void)locationManager:(CLLocationManager *)manager
         didUpdateLocations:(NSArray *)locations
    {
        CLLocation *location = [locations lastObject];
        userLatitude =  [NSString stringWithFormat:@"%f", location.coordinate.latitude] ;
        userLongitude =  [NSString stringWithFormat:@"%f",location.coordinate.longitude];
        [locationManager stopUpdatingLocation];
    }
    

    Swift Procedure

    Follow the below instructions:

    For iOS-11 For iOS 11 have a look at this Answer: iOS 11 location access

    Need to Add two Keys into plist and provide message as below image:

     1. NSLocationAlwaysAndWhenInUseUsageDescription 
     2. NSLocationWhenInUseUsageDescription
     3. NSLocationAlwaysUsageDescription
    

    For iOS-10 and below:

    import CoreLocation
    class ViewController: UIViewController ,CLLocationManagerDelegate {
    var locationManager = CLLocationManager()
    
    //MARK- Update Location 
    func updateMyLocation(){
        locationManager.delegate = self;
        locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
        if locationManager.respondsToSelector(#selector(CLLocationManager.requestWhenInUseAuthorization)){
           locationManager.requestWhenInUseAuthorization()
        }
        else{
            locationManager.startUpdatingLocation()
        }
    }
    

    Delegate Methods

    //MARK: Location Update
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        NSLog("Error to update location :%@",error)
    }
    func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
        switch status {
        case .NotDetermined: break
        case .Restricted: break
        case .Denied:
                NSLog("do some error handling")
            break
        default:
            locationManager.startUpdatingLocation()
        }
    }
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
         let location = locations.last! as CLLocation
        var latitude = location.coordinate.latitude
        var longitude = location.coordinate.longitude
    
    }
    

提交回复
热议问题