from ios8 Start , Apple has further strengthened the protection of users' privacy .

When app When you want to access a user's privacy information   The system will no longer automatically pop up a dialog box for user authorization

resolvent :

(1) call ios8.0 Of API Actively request user authorization

- (void)requestAlwaysAuthorization // Request permission to obtain the privilege of the user's location on both front and back platforms

- (void)requestWhenInUseAuthorization // Request permission to get the privilege of the user's location in the foreground

So now we're starting to use two methods :

Our project of this award Let's go back to the last time ios8 The previous positioning of that project We just need to make a little change :

1. First, switch our deployment version to 8.4

2. And then we'll have our ViewController.m file Add the following code to the lazy loading location manager of :

3. Switch the simulator to 6

4. Allow programs

We click on the screen You'll find that You've done nothing at all? No box pops up

So what's the reason for this ?

Let's go into the header file of this method and see

See this sentence ?

This sentence means that if your info.plist There's no such thing in the file key: NSLocationWhenInUseUsageDescription So this method doesn't do anything

So now we are info.plist File to configure such key

After configuration Let's allow the program to see again

It's ok now We click allow

Then click on the screen , You'll find that the console doesn't print any information , This is a simulator problem We just need to restart the simulator

Restart the emulator, such as :

Then we run the program again You'll find out All right :

Pay attention to this :requestWhenInUseAuthorization  Method by default Waves can get their location in the background Background mode needs to be checked

At this point we allow the program to Back to the background Look at the effect

See the difference ? There will be a blue bar

The above is the use of requestWhenInUseAuthorization  Do location Authorization   Now let's see requestAlwaysAuthorization Front and back Authorization

He followed requestWhenInUseAuthorization The difference is that There is no need to check background mode And there won't be blue bars

At the same time, he also needs to be in info.plist The configuration in the file is as follows key :

Let's configure

Let's simply modify the code Turn on the front and back Authorization :

Then we run the program :