Different behavior for current location latitude and longitude in iOS7 to IOS8

Using xamarin for current location currentLocation example I used the above link to get the current latitude and longitude of a location.
In IOS 7 before upgrading to IOS 8, it shows my current latitude and longitude location on the simulator and device. (It looks strange to me for a simulator to get the current latitude and longitude of a location).

After upgrading to iOS 8 and running the same code, it gives

In the simulator: (Turn on Wi-Fi without a SIM card. The service is always on.)

Result:
latitude = 37.785834.
longitude = -122.406517.

      

On the IPhone5c device (Turn on Wi-Fi without a SIM card. Search service is always.)

Result:
latitude = 37.785834.
longitude = -122.406517.

      

(But I expected my current location latitude and longitude)

After the update, it doesn't ask which app is using your current GPS location. "Warning" message with "YES", "No".

How do I get my current latitude and longitude?

+2


source to share


3 answers


You need to request permission explicitly from iOS 8.0 onwards. Actions for this:

  • Update the Info.plist file. Insert a new key: either NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription and write a custom message to be displayed when your app asks for permission.

Make sure to have one of these keys in your plist file, otherwise the permission request won't show up! If you don't want to show the explain text in the alert view, leave the key value string blank, but you must still have the key present in the Info.plist file!



  1. Make sure you are on iOS8 before starting tracking, then send a RequestAlwaysAuthorization or RequestWhenInUseAuthorization from the CLLocationManager instance. Example:

    var locationManager = new CLLocationManager();
    if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
        locationManager.RequestAlwaysAuthorization();
    }
    
          

In this case, use the NSLocationAlwaysUsageDescription key in the Info.plist file. Please note that Xamarin Studio does not currently offer you this key automatically from the dropdown, you need to enter / copy this key manually.

+6


source


Add LocationManger.cs file for method LocationManager

if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
    this.locMgr.RequestAlwaysAuthorization();
}

      

After the change



public LocationManager ()
{
    this.locMgr = new CLLocationManager();
    LocationUpdated += PrintLocation;
    if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0)) {
        this.locMgr.RequestAlwaysAuthorization();
    }
}

      

Add Info Plist manually NSLocationAlwaysUsageDescription is an empty string.

Then clean the assembly and run the app. Its showing my current location latitude and longtidu.

0


source


I tried the solutions but it didn't work. The problem is that you are targeting IOS> = 9.0, then the location manager changes some of the permission variables to be explicitly pointed to info.pList.

<key>NSLocationWhenInUseUsageDescription</key>
<string>Why you need Location</string>

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Why you need Location</string>

<key>NSLocationAlwaysUsageDescription</key>
<string>Why you need Location</string>

<key>NSLocationWhileInUsage</key>
<string>Why you need Location</string>

      

use these four keys if you want to target all versions .. before and after 9.0.

Also note that this line means the user is in the foreground or background use-places (apple discourages this as it has privacy potential) check the documentation here

[locationManager requestAlwaysAuthorization];

      

whereas that means

[locationManager requestWhenInUseAuthorization];

      

Use only if app is in foreground here check documentation

0


source







All Articles