different behaviour for current location latitude and longitude in iOS7 to IOS8

两盒软妹~` 提交于 2020-01-13 20:30:07

问题


Using xamarin for current location currentLocation example I used the above Link for getting current location latitude and longitude.
In IOS 7 before upgrade to IOS 8 Its shows my current location latitude and longitude on both simulator and device.(Its looks strange for me in simulator for getting current location latitude and longitude).

After upgrade to iOS 8 and run same code it give

In simulator:(Turn on wifi no sim card location service App always.)

Result:
latitude = 37.785834.
longitude = -122.406517.

On Device IPhone5c (Turn on wifi no sim card location service App always.)

Result:
latitude = 37.785834.
longitude = -122.406517.

(But I expected my current location latitude and longitude )

After upgrade it does not ask, that application like use the Your Current Location GPS. an Alert message with YES, NO options.

How to get my current location latitude and longitude?


回答1:


You need to request permission explicitly from iOS 8.0 onwards. The steps to do so:

  1. Update your Info.plist file. Insert a new key: either NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription keys and write the custom message that will be displayed when your app requests the permission.

It is a must to have either of these keys in your plist file otherwise the permission request won't be displayed! If you don't want to show a custom explanation text in the alertview, leave the key's value string empty, but you still must have the key present in the Info.plist file!

  1. Before you start tracking, check that you are on iOS8 then send either 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 your Info.plist file. Please note, Xamarin Studio does not currently offer you this key automatically from the dropdown list, you have to enter / copy-paste this key manually there.




回答2:


When using the iOS simulator you need to set the custom location for the simulator hardware. Go to Debug, select location, custom location and then set the preferred co-ordinates.




回答3:


Add In LocationManger.cs file for method LocationManager

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

After change

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

Add Info Plist manually NSLocationAlwaysUsageDescription empty string.

Then clean the build and run the application. Its shows my current location latitude and longtidu.




回答4:


I tried above solutions but that did not work. Problem is when you are targeting IOS >= 9.0 then Location manager change some permission variables, which are supposed to mention explicitly in 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 whether user is in foreground or background use locations (apple discourage this because it has potential of privacy) check documentation here

[locationManager requestAlwaysAuthorization];

whereas this means,

[locationManager requestWhenInUseAuthorization];

Only use when app is in foreground check documentation here



来源:https://stackoverflow.com/questions/25953661/different-behaviour-for-current-location-latitude-and-longitude-in-ios7-to-ios8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!