Can i turn on location service by the code programmatically

493 views Asked by At

I am new to the tvOS platform. I want to know how to get current location latitude and longitude at the application launch. I am writing this code to get the location coordinate.

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    dispatch_async(dispatch_get_main_queue()) { () -> Void in
            self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
            self.locationManager.delegate = self
            self.locationManager.requestLocation()
            self.locationManager.requestWhenInUseAuthorization()
        }
        return true
    }

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        locationValue = manager.location?.coordinate
        print("=========didUpdateLocations==========Latitude>>>>>",locationValue.latitude)
        print("=========didUpdateLocations==========Longitude>>>>",locationValue.longitude)
    }
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {

        print("localizedDescription===================",error.localizedDescription)
    }

This code provide the coordinate after the view controller loads. While my requirement is that I want to get coordinate before loading the view controller. Can i enable location service programmatically for single application?

2

There are 2 answers

4
Frank C. On

It requires setting the app information and grants, see the discussion Here

I also found the solution and explanation for this identical question Here

0
Codo On

The location service determines the location on demand and it requires time to do so. For that reason, it only offers an asynchronous interface.

So there is now way you can retrieve the location before the view controller is shown. You will need to write your app such that it can display the view controller without knowing the location and later update the screen when the location information is available.