简体   繁体   中英

How to get user's current location without asking for permission?

can any one tell me how can i get the current location-latitude/longitude info of the user without asking his permission? Like getting user's location automatically? Thanks in Advance. Shreya

From apple's documentation

Important: In addition to hardware not being available, the user has the option of denying an application's access to location service data. During its initial uses by an application, the Core Location framework prompts the user to confirm that using the location service is acceptable. If the user denies the request, the CLLocationManager object reports an appropriate error to its delegate during future requests. You can also check the application's explicit authorization status using the authorizationStatus method.

So apple wont allow to use CoreLocation framework without user's permission. I don't thing there is no other way without using CoreLocation framework, atleast with public API..

I wouldn't recommend trying to get around Apple's TOS regarding getting user's permission to enable CoreLocation for an app. It seems like a sure fire way to get your app rejected. Apple may be sensitive about this kind of stuff .

Here's a related question I found .

I managed to get the country without asking for location permissions using the following approach:

import MapKit

class CountryDectectorViewController: UIViewController {
    var didDetectCountryCode: ((String?) -> Void)?

    override func viewDidLoad() {
        super.viewDidLoad()
        // Map view setup
        let mapView = MKMapView()
        view.addSubview(mapView)
        mapView.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            mapView.topAnchor.constraint(equalTo: view.topAnchor),
            mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])
        mapView.layoutIfNeeded()
        // Reverse geocoding map region center
        let location = CLLocation(
            latitude: mapView.region.center.latitude,
            longitude: mapView.region.center.longitude
        )
        CLGeocoder().reverseGeocodeLocation(location) { placemarks, _ in
            self.didDetectCountryCode?(placemarks?.first?.isoCountryCode)
        }
    }
}

To ISO Country Code can be then obtained using:

let controller = CountryDectectorViewController()
controller.didDetectCountryCode = { countryCode in
    print(countryCode)
}
controller.loadViewIfNeeded()

Some context

I realised that UIKit has this information already because everytime the MKMapView is shown, the region is automatically set to fit the current user's country. Using this hypothesis I needed to find a solution to load the map without presenting it and then to reverse geocode the center coordinates to identify the country.

I implemented this solution taking into consideration the following limitations I found:

  • Don't ask for user permissions ideally
  • Device locale was not considered a reliable alternative
  • Detecting the sim carrier actually returns the original carrier country and not the current connected carrier (via roaming)
  • Retrieving the country by IP can be easily faked using VPNs which are becoming more popular lately

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM