Handling Built In iOS Permission Dialogs - ios

When loading my view controller for the first time, the user is prompted with a built in iOS permissions message: "Allow "appName" to access your location while you use the app?"
is there a way I can pause the app until the user either selects Don't Allow or Allow?
After the user selects Allow Or Dont Allow, I can handle it by checking the value like so:
//INSIDE OF ViewDidAppear
if(authstate != CLAuthorizationStatus.Denied)
{
// do something
}
else if (authstate == CLAuthorizationStatus.AuthorizedWhenInUse)
{
// do something
}
Right now the code steps through the if statement even though the iOS permissions dialog is still showing on the screen. Therefore, I want to pause it until the user selects Allow or Dont Allow. Then after the user selects either Allow or Dont Allow, continue onto the if statement

no. the permission request happens asynchronously. you have to implement the CLLocationManagerDelegates method
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
// check the status
}
that gets called after the user answered the permission dialogue.

Related

What makes provisional Always authorization provisional?

According to Apple, if you ask for your Core Location app to get Always authorization when the authorization is "not determined", the user sees the dialog for When In Use authorization but in fact your app gets Always authorization — provisionally.
This is supposed to mean that if you don't actually use your Always powers, you will lose them, reverting to When In Use.
Okay, but when will that reversion happen? I can't seem to make it happen. My app just stays at Always authorization, even though the user thinks it is only When In Use authorization.
Here's the entire code of my test app (iOS 14):
class ViewController: UIViewController, CLLocationManagerDelegate {
#IBOutlet weak var label: UILabel!
let locman = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locman.delegate = self
}
#IBAction func doAskForAlways(_ sender: Any) {
self.checkForLocationAccess(always:true)
}
func checkForLocationAccess(always:Bool = false, andThen f: (()->())? = nil) {
let status = self.locman.authorizationStatus()
switch status {
case .authorizedWhenInUse:
if always { // try to step up
self.locman.requestAlwaysAuthorization()
} else {
f?()
}
case .authorizedAlways:
f?()
case .notDetermined:
if always {
self.locman.requestAlwaysAuthorization()
} else {
self.locman.requestWhenInUseAuthorization()
}
case .restricted:
break
case .denied:
break
default: fatalError()
}
}
fileprivate func updateStatus(_ status: CLAuthorizationStatus) {
self.label.text = {
switch status {
case .authorizedAlways: return "Always"
case .authorizedWhenInUse: return "When In Use"
default: return ""
}
}()
}
func locationManagerDidChangeAuthorization(_ manager: CLLocationManager) {
let status = manager.authorizationStatus()
print("authorization is", status.rawValue)
updateStatus(status)
}
#IBAction func doStatus(_ sender: Any) {
self.updateStatus(self.locman.authorizationStatus())
}
}
You need two buttons and a label. Tap the first button to ask for Always authorization when you have no authorization to start with ("not determined"). You see the When In Use authorization dialog. Grant authorization. Now play with the app and keep watching the status display in the label. You can tap the second button to update the status if needed.
The problem is that it stays at Always. When will my "provision" come to an end so that the authorization reverts to When In Use? How can I encourage this to happen?
In WWDC 2019's What's New in Core Location, they outline the basic process in iOS 13.0:
Your app requests “always” permission.
The user sees “when in use” permissions alert, not an “always” permission alert:
If the user grants “when in use” the app is in “provisional always” state.
In this case, and somewhat confusingly, the authorizationStatus will return .authorizedAlways when you are in this “provisional always” state and the Settings app on the phone will suggest it’s in “when in use” state. But in reality, it’s in this “provisional always” state, not quite what one might infer from authorizationStatus nor from what you see in the Settings app.
Needless to say, if the user doesn't even grant “when in use” (e.g. they deny or chose “only once”), then obviously you won’t be in “provisional always” state.
It remains in this provisional state until, as the video says, you “start using ‘always’ powers”. For example, if you start significant change service and move a distance sufficient to trigger a significant change.
When the app does “start using ‘always’ powers”, the OS will ask the user if they are is willing to upgrade “when in use” to “always”. (It won't always happen immediately, but will wait until the user is not busy doing other things, to reduce the risk that they'll dismiss the alert just to get back to what they were doing.)
So, it’s not a question of “reverting” to some other state. The app will remain in this “provisional always” state until there is final “agreement” (where the user sees the second alert and either agrees to upgrade to .authorizedAlways or denies and it is set to .authorizedWhenInUse).
I know you know this, but for the sake of future readers:
In WWDC 2020 video What's new in location, they describe a change introduced in iOS 13.4. Instead of the flow above (where you ask for “always”, the user sees “when in use” permissions, and they don’t see the “upgrade to always” until “always” services are actually triggered), iOS 13.4 introduced a new flow, where you can ask for “when in use” (rather than “always”) and assuming the user granted it, you can ask for “always” later, where appropriate in the app, and the user get the second alert (this time asking if the user would like to upgrade to “always” or not). You just need the appropriate permissions strings.

PhotoLibrary access .notDetermined, when denying then enabling

If I deny at first, then go to settings and allow in settings, in both cases the status is notDetermined, instead of denied then authorized.
Why is that happening?
It doesn't save the image when i click "Don't allow", but status becomes .notDetermined not .denied .
It saves, after i go to settings->Photos, uncheck "Never" and check "Add Photos Only". But the status stays .notDetermined, does not become .authorized
func save(){
guard let image = imageView.image else {return}
UIImageWriteToSavedPhotosAlbum(image, self, nil, nil)
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
print("authorized")
return
case .notDetermined:
print("not determined")
case .denied, .restricted:
print("denied or restricted")
//please go to settings and allow access
promptToSettings()
}
}
I am asking permission to save an image to photo library.
When the first time the user tries to save, he gets asked: "App would like to add to Photos" "Don't Allow" "Ok"
If the user denied then tried to save again,i want to check and if the status is .denied, prompt the user to go to settings and allow.
But the code goes to .notDetermined block when the user does not give access the first time. It stays .notDetermined even after in settings the user allows access.
I downloaded your code and ran it. I was able to experience whatever you said. It always returned Not Determined status.
I did a little bit analysis on your code further. Please find my observation below.
In your current code, "PHPhotoLibrary.requestAuthorization" method is not called before reading the authorization status using "PHPhotoLibrary.authorizationStatus" method.
Though "UIImageWriteToSavedPhotosAlbum" in Save method triggers a Photos app permission pop up, it does not help here completely.
So, I called "askForAccessAgain" method in ViewDidLoad method in order to get the permission using "PHPhotoLibrary.requestAuthorization" method first.
After that, whenever i saved the photos using Save method, it returned the correct status, let it be either "Authorized" or "Denied".
If I choose "Never" and "Allow Photos Only", it returned "Denied or Restricted" status. When "Read and Write" is chosen, "authorized" is returned.
So, it looks like, We need to call "PHPhotoLibrary.requestAuthorization" method to get the permission first instead of relying on other Photo access methods ( like UIImageWriteToSavedPhotosAlbum) for getting the permission. Only then, correct status is returned by "PHPhotoLibrary.authorizationStatus".
One more addition info:
App Permissions are retained even after deleting the app and reinstalling it. Just take a look on this as well. It might help you in troubleshooting similar issues in future.
iPad remembering camera permissions after delete—how to clear?
Please let me know if this has resolved your issues.

How to refresh permission after getting access to it

I need to access users Photos, for this I ask for permission. There are possible outcomes :
Permission granted : In this condition application resumes it normal functioning.
Permission Denied : In this case application shows an alert wherein user has option to goToSettings or to just ignore it. If user selects goToSettings and provides access to photos and comes back to application then it still says that permission is not granted. Permissions are only refreshed when I restart my application.
My Question: How to refresh the application's permission settings without restarting my application. Similar problem to my question is posted here.
Edit:
My application is a navigation controller based application and I check for access in my controller's viewDidLoad. I just tried if request permissions are refreshed if I pop and push the controller again. No luck, the results where same - it still said permission denied.
Edit 2 : Code to check for permissions
func requestAccess() -> Void {
PHPhotoLibrary.requestAuthorization {
status in
switch(status) {
case .notDetermined, .denied :
// perform relavent action
break
case .authorized , .restricted :
// perform relavent action
break
}
}
}
iOS kills your app when you changed privacy settings. And after next tap on app's icon your app is started from scratch. application:didFinishLaunching is called and so on. So you don't need to update permissions.
In your AppDelegate you could have logic in applicationDidBecomeActive to re-query the permission store for your updated permissions.
In your viewDidLoad for the view you check permissions on you could observe the notification like so;
NSNotificationCenter.defaultCenter().addObserver(
self,
selector: #selector(yourClass.applicationDidBecomeActive(_:)),
name: UIApplicationDidBecomeActiveNotification,
object: nil)
The code below is Objective-C.
I think you can do this when user tapping a button or something:
PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
if (status == PHAuthorizationStatusNotDetermined) {
// ask for authorization
[PHPhotoLibrary requestAuthorization...
} else if (status == PHAuthorizationStatusAuthorized) {
// continue accessing photos
} else {
// access denied, go to settings if you want
}

Checking permissions in Swift 2.0

No matter where I add code to check permissions for things like camera/mic/photos, the popup confirmation always kills my app or sends me back a few view controllers.
An example is as follows.
I have few view controllers in (part way through a registration process) when I have a page that deals with the permissions. Users tap a button to deal with the camera permission which uses the following code.
if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) != AVAuthorizationStatus.Authorized {
AVCaptureDevice.requestAccessForMediaType(AVMediaTypeVideo, completionHandler: { (granted :Bool) -> Void in
if granted == true {
// do something
} else {
// determine whether not determined, denied etc and do something else
}
});
}
However as soon as the iOS confirmation pops up, it throws the app back 2 view controllers. In other cases (e.g. on a viewDidLoad) permission requests kill the app as soon as the choice is made.
Any ideas what am I missing in my setup or how to prevent this behaviour?
Thanks.
I think you misunderstood my comment, what I did meant was
if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) != AVAuthorizationStatus.Authorized { // here you are checking if it's not authorized i..e it's denied, NotDetermined or Restricted
....
}
else if if AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeVideo) == AVAuthorizationStatus.Authorized
// do the something in case it's autorized
I'm listing keys here -
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>
<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera.</string>

How to detect system wide Location Services changes

I'm going through potential cases where a User might turn off Location Services on their phone.
Right now, I have:
func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
if status == .Authorized || status == .AuthorizedWhenInUse {
manager.startUpdatingLocation()
} else {
manager.stopUpdatingLocation()
}
}
I assume this will trigger whenever the User goes into the Settings and changes my App's settings personally. Does this also trigger whenever the user turns off Location Services for all applications? The documentation doesn't seem to go over it.
Yes. Turning of location services globally will also trigger that method and it effectively means that the kCLAuthorizationStatusDenied will be sent as the current status.
Once the user enables the location services again, the method will be triggered again and one of the other enumerated options will be sent as the current status - And the value will depend on the location services settings history for that specific app.

Resources