requestAlwaysAuthorization not showing permission

2019-02-05 15:36发布

I'm trying to use some fancy iBeacons without success, kCLAuthorizationStatusNotDetermined all time. According to other questions it's a requirement to add those keys to info.plist (some questions says one, other says both). According to an article for iBeacons I need the Always option.

<key>NSLocationWhenInUseUsageDescription</key>
<string>Nothing to say</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Permiso para acceder siempre</string>

At viewDidAppear:

self.locManager = [[CLLocationManager alloc]init];
self.locManager.delegate = self;
[self.locManager requestAlwaysAuthorization];
NSUUID* region1UUID = [[NSUUID alloc]initWithUUIDString:@""]; //ibeacon real UUID between "". Checked it's not nil.

self.beaconRegion = [[CLBeaconRegion alloc]
                                initWithProximityUUID:proximityUUID
                                identifier:@"myCoolString"];

self.beaconRegion.notifyEntryStateOnDisplay = YES;
self.beaconRegion.notifyOnEntry = YES;
self.beaconRegion.notifyOnExit = NO;
[self.locManager startMonitoringForRegion:self.beaconRegion];
[self.locManager startRangingBeaconsInRegion:self.beaconRegion];

Icon didn't appear at Settings/Privacy/Location until it was executed one of the two last methods. The Alert View to approve permissions never appears. If I perform a manual change at Location Settings and check it it will change status but at a few moments later Location at Settings will delete "Always" status for my app and will leave it blank again. Later I check with no luck

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {

Any ideas what is missing or wrong? Thank you

12条回答
Summer. ? 凉城
2楼-- · 2019-02-05 16:05

Swift 3.X Latest code easily usage

import CoreLocation

 public var locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()


        locationManager.delegate = self
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()

}



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

        let altitudeG = locations.last?.altitude
        let longitudeG = locations.last?.coordinate.longitude
        let latitudeG = locations.last?.coordinate.latitude

print("\(altitudeG) \(longitudeG) \(latitudeG)")

    }
查看更多
霸刀☆藐视天下
3楼-- · 2019-02-05 16:07

For iOS 11 developers, you should take a look at this post: Location Services not working in iOS 11.


TL;DR: you need ALL THREE location keys in the Info.plist:

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>...</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>...</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>...</string>

Along with InfoPlist.strings translations in case of multilingual apps.

查看更多
Luminary・发光体
4楼-- · 2019-02-05 16:08

I copied this tutorial ...

http://willd.me/posts/getting-started-with-ibeacon-a-swift-tutorial

It didn't work out the box, although the fix was very simple, don't declare

let locationManager = CLLocationManager()

But move it into the class as variable

var locationManager = CLLocationManager()

And it works!!

查看更多
你好瞎i
5楼-- · 2019-02-05 16:14

Please check this Reference link

It has described all the changes regarding iOS 11. Your issue is also seems to be one among the cases it described. It might give you an idea what changes you need to make in your code to correct the issue.

查看更多
欢心
6楼-- · 2019-02-05 16:17

Try to Start Updating Location (have helped for me)

[self.locationManager startUpdatingLocation];
查看更多
太酷不给撩
7楼-- · 2019-02-05 16:18

Just add this lines to your .plist file

<key>NSLocationAlwaysUsageDescription</key>
<string>Optional message</string>
查看更多
登录 后发表回答