How to define the rights to change privacy (for example, to access the camera)

We've all been there. Do you want to take photos in your app or access your photos, microphone, contacts, etc. But first, iOS must ask the user for permission. In many cases, the user denies access.

If your app detects that the user has been denied access, you can navigate to the app's privacy settings with this:

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];

      

Handy. However....

I noticed that if you convince the user to toggle the toggle to on, the application will not detect the change.

Consider this code. The user will immediately be prompted for permission to access the camera (this is only shown the first time the app is launched). Let's assume the user has refused permission. Then they decided they wanted to turn on camera access eventually. No problems. The user clicks a button that brings up the privacy panel. The user toggles the switch to allow access. The user then switches back to the application. The block is fired for UIApplicationDidBecomeActiveNotification, which reads the permission again. However, it does not reflect the user's changes (it still reads "Rejected").

If the application is cleared from memory and restarted, it will read the state correctly.

Not all permissions behave like this. For example, CoreLocation seems to detect user changes. I also found a way to detect changes for notifications. But for contacts, calendars, camera, microphone, basic motion (etc.), changes are not detected until the app is closed and restarted.

Any ideas?

#import "ViewController.h"
@import AVFoundation;

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        [self printPermission];
    }];

    [AVCaptureDevice requestAccessForMediaType:AVMediaTypeVideo completionHandler:^(BOOL granted) {
        [self printPermission];
    }];

}


-(void)printPermission{
    dispatch_async(dispatch_get_main_queue(), ^{
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if(status == AVAuthorizationStatusNotDetermined){
            NSLog(@"VWWPermissionStatusNotDetermined");
            self.view.backgroundColor = [UIColor whiteColor];
        } else if(status == AVAuthorizationStatusAuthorized){
            NSLog(@"VWWPermissionStatusAuthorized");
            self.view.backgroundColor = [UIColor greenColor];
        } else if(status == AVAuthorizationStatusDenied) {
            NSLog(@"VWWPermissionStatusDenied");
            self.view.backgroundColor = [UIColor redColor];
        } else if(status == AVAuthorizationStatusRestricted) {
            NSLog(@"VWWPermissionStatusRestricted");
            self.view.backgroundColor = [UIColor redColor];
        }
    });
}

- (IBAction)buttonAction:(id)sender {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
}

@end

      

+3


source to share


1 answer


So this turned out to be a bug related to iOS 9b1.

Discovery resolution works fine on iOS 8.



I found out that you need to check the permission on the main queue. If you do, it will reflect the updates.

+2


source







All Articles