Detect permission of media library ios - ios

In my app, I want to detect that if user give the permission to his media library or not. User may denied media library permission when system popup ask or later from setting. Is there any way to detect the status of media library permission?
Here is my code that access list of songs.
MPMediaQuery *everything = [MPMediaQuery songsQuery];
NSArray *songArray = [everything items];
Please see below screenshot where user can change Media Library permissions.

-(void) checkMediaLibraryPermissions {
[MPMediaLibrary requestAuthorization:^(MPMediaLibraryAuthorizationStatus status){
switch (status) {
case MPMediaLibraryAuthorizationStatusNotDetermined: {
// not determined
break;
}
case MPMediaLibraryAuthorizationStatusRestricted: {
// restricted
break;
}
case MPMediaLibraryAuthorizationStatusDenied: {
// denied
break;
}
case MPMediaLibraryAuthorizationStatusAuthorized: {
// authorized
break;
}
default: {
break;
}
}
}];
}

Temporarily, i solved my problem by checking songArray object in below code
MPMediaQuery *everything = [MPMediaQuery songsQuery];
NSArray *songArray = [everything items];
If, user denied permission then songArray object is always nil, but if user allows permission to access to Media Library then songArray object have array of songs. Even if there will be no songs in device but user give permission to access Media Library then there will be array with 0 count.

Swift 4 access check. The simple solution is as follows, and you can alter to include the other alternatives however in my case it was all access or nothing.
private func checkPermissionForMusic() -> Bool {
switch MPMediaLibrary.authorizationStatus() {
case .authorized:
return true
default:
return false
}
}
Caution about using the above solutions - they do perform as a block statement and do not return a value (return true or return "authorised") on the same thread; the result is handled on a background thread. If you decide to use the suggestions above, use a handler (call another function) to handle the result you're expecting. This solution on the other hand tells you immediately if you have access or not. No waiting required.
More info is available in the Apple Docs

Related

iOS Ask Permission Before Used Page [duplicate]

With the introduction of iOS 7, applications have to request microphone access when they want to record audio.
How do I check if the application has access to the microphone?
In the iOS 8 SDK I can use the AVAudioSessionRecordPermission enum, but how do I check this in iOS 7?
Info:
I don't want to request permission, I just want to check if the app has access to the microphone. (Like Location access):
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
// Do something
}
You can check the with recordPermission(), which has been available since iOS 8.
Keep in mind that starting with iOS 10, you must set the NSMicrophoneUsageDescription property in your info.plist for microphone permissions and include a message for the user. This message is shown to the user at time of the request. Finally, if localizing your app, be sure to include your plist strings for translation.
Failure to do so will result in a crash when attempting to access the microphone.
This answer has been cleaned up again for Swift 5.x
import AVFoundation
switch AVAudioSession.sharedInstance().recordPermission {
case .granted:
print("Permission granted")
case .denied:
print("Permission denied")
case .undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ granted in
// Handle granted
})
#unknown default:
print("Unknown case")
}
Objective-C
I have tested this code with iOS 8 for the purpose of checking for microphone permission and obtaining the current state.
switch ([[AVAudioSession sharedInstance] recordPermission]) {
case AVAudioSessionRecordPermissionGranted:
break;
case AVAudioSessionRecordPermissionDenied:
break;
case AVAudioSessionRecordPermissionUndetermined:
// This is the initial state before a user has made any choice
// You can use this spot to request permission here if you want
break;
default:
break;
}
As always, make sure to import AVFoundation.
In iOS7 there is no way to get the current status of microphone authorization.They have given the enum in iOS8 as AVAudioSessionRecordPermission
In iOS7 you have to request permission every time with
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
NSLog(#"Permission granted");
}
else {
NSLog(#"Permission denied");
}
}];
The same question has been asked before but there is no such api with which you know current status as in iOS8
You can refer Check for mic permission on iOS 7 without showing prompt
Solution:
Another option is you can show the popup or ask for permission first time and save the states of user option selected in NSUserDefaults and than onwards do not ask for permission.
From docs you explicitly do not need to call this if each you do not need to get the permission of user.It will automatically called by AVAudioSession first time when you try to record
Recording audio requires explicit permission from the user. The first
time your app’s audio session attempts to use an audio input route
while using a category that enables recording (see “Audio Session
Categories”), the system automatically prompts the user for
permission; alternatively, you can call requestRecordPermission: to
prompt the user at a time of your choosing
Swift 3 Complete Solution Code
func checkMicPermission() -> Bool {
var permissionCheck: Bool = false
switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
permissionCheck = true
case AVAudioSessionRecordPermission.denied:
permissionCheck = false
case AVAudioSessionRecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
permissionCheck = true
} else {
permissionCheck = false
}
})
default:
break
}
return permissionCheck
}
There is another way you can try following code for ios 7 and 8 :
let microPhoneStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeAudio)
switch microPhoneStatus {
case .Authorized:
// Has access
case .Denied:
// No access granted
case .Restricted:
// Microphone disabled in settings
case .NotDetermined:
// Didn't request access yet
}
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
// Microphone enabled code
}
else {
// Microphone disabled code
}
}];
And include <AVFoundation/AVAudioSession.h>
Since none of the other answers here mentioned this, you need to add the permissions to your info.plist. Specifically, add an entry for:
Privacy - Microphone Usage Description
For the String value, enter something like:
(App name) needs access to your microphone.
Otherwise, you get a mysterious crash
What I often end up doing for a quick check on objects working with audio record:
// swift 5
static public func isAuthorized() -> Bool {
return AVCaptureDevice.authorizationStatus(for: .audio) == .authorized
}
import AVFoundation and use the following function
var permissionCheck:Bool = false
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSession.RecordPermission.granted:
permissionCheck = true
case AVAudioSession.RecordPermission.denied:
permissionCheck = false
case AVAudioSession.RecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
permissionCheck = true
} else {
permissionCheck = false
}
})
default:
break
}

How to check MPMusicPlayerController permission?

I have a viewController in my objective c application to show song list of my iPhone. The first time when I open this viewController, show the permission to use the music, but when I accept the permission, the tableView of all songs not refresh.
Like this:
How can I check when the user click on allow to refresh the view?
MPMediaLibraryAuthorizationStatus authorizationStatus = MPMediaLibrary.authorizationStatus;
switch (authorizationStatus)
{
case MPMediaLibraryAuthorizationStatusAuthorized:
{
break;
}
case MPMediaLibraryAuthorizationStatusNotDetermined:
{
break;
}
case MPMediaLibraryAuthorizationStatusRestricted:
case MPMediaLibraryAuthorizationStatusDenied:
{
break;
}
}

How to detect if the user doesn't allow Apple Health Kit Authorisation?

I am using AppleHealthKit in my application. Everything is working correctly. The problem is that I am not able to detect if the user taps "Don't Allow" button when asking for permission.
With this method, my application uses HealthKit, even though the user doesn't allow this.
requestAuthorizationToShareTypes(healthKitTypesToWrite, readTypes: healthKitTypesToRead) { (success, error) -> Void in
if( completion != nil ) {
completion(success:success,error:error)
}
Apple Documentation:
So basically my question is how to detect this?
You cannot detect it, by design:
To help prevent possible leaks of sensitive health information, your app cannot determine whether a user has granted permission to read data. If you are not given permission, it simply appears as if there is no data of the requested type in the HealthKit store.
(from HKHealthStore)
You can do this but only for one specific type and only for write data(u can do the same with other type too if u have to check them)
HKHealthStore *_healthStore;
HKQuantityType *stepsType = [HKQuantityType quantityTypeForIdentifier:HKQuantityTypeIdentifierStepCount];
HKAuthorizationStatus status = [_healthStore authorizationStatusForType:stepsType];
BOOL isActive;
switch (status) {
case HKAuthorizationStatusSharingAuthorized:
isActive = YES;
break;
case HKAuthorizationStatusSharingDenied:
isActive = NO;
break;
case HKAuthorizationStatusNotDetermined:
isActive = NO;
break;
default:
break;
}
NSLog(#"STATUS-------%#", isActive ? #"yes" : #"no");

Read camera permission for iOS in Xamarin

I have an iOS app developed in Xamarin. When the app does not have permission to access the microphone, if the user tries to access the microphone from the app, I check the settings using AVAudioSession.SharedInstance().RequestRecordPermission (delegate(bool granted)) and display a message.
Now I need to do the same if the app does not have permission to access the camera. I need to check if permission is granted for camera and display a message accordingly. How can I do this?
I have got the answer. Here is what I have done:
AVCaptureDevice.RequestAccessForMediaType (AVMediaType.Video, (bool isAccessGranted) => {
//if has access
if(isAccessGranted)
{
//do something
}
//if has no access
else
{
//show an alert
}
});
Did you checked this answer?
Detect permission of camera in iOS
I think that's the solution you are looking for :).
EDIT:
Here is the highest voted answer's code ported to C#
// replace the media type to whatever you want
AVAuthorizationStatus authStatus = AVCaptureDevice.GetAuthorizationStatus(AVMediaType.Video);
switch (authStatus)
{
case AVAuthorizationStatus.NotDetermined:
break;
case AVAuthorizationStatus.Restricted:
break;
case AVAuthorizationStatus.Denied:
break;
case AVAuthorizationStatus.Authorized:
break;
default:
throw new ArgumentOutOfRangeException();
}

iOS check if application has access to microphone

With the introduction of iOS 7, applications have to request microphone access when they want to record audio.
How do I check if the application has access to the microphone?
In the iOS 8 SDK I can use the AVAudioSessionRecordPermission enum, but how do I check this in iOS 7?
Info:
I don't want to request permission, I just want to check if the app has access to the microphone. (Like Location access):
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied) {
// Do something
}
You can check the with recordPermission(), which has been available since iOS 8.
Keep in mind that starting with iOS 10, you must set the NSMicrophoneUsageDescription property in your info.plist for microphone permissions and include a message for the user. This message is shown to the user at time of the request. Finally, if localizing your app, be sure to include your plist strings for translation.
Failure to do so will result in a crash when attempting to access the microphone.
This answer has been cleaned up again for Swift 5.x
import AVFoundation
switch AVAudioSession.sharedInstance().recordPermission {
case .granted:
print("Permission granted")
case .denied:
print("Permission denied")
case .undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ granted in
// Handle granted
})
#unknown default:
print("Unknown case")
}
Objective-C
I have tested this code with iOS 8 for the purpose of checking for microphone permission and obtaining the current state.
switch ([[AVAudioSession sharedInstance] recordPermission]) {
case AVAudioSessionRecordPermissionGranted:
break;
case AVAudioSessionRecordPermissionDenied:
break;
case AVAudioSessionRecordPermissionUndetermined:
// This is the initial state before a user has made any choice
// You can use this spot to request permission here if you want
break;
default:
break;
}
As always, make sure to import AVFoundation.
In iOS7 there is no way to get the current status of microphone authorization.They have given the enum in iOS8 as AVAudioSessionRecordPermission
In iOS7 you have to request permission every time with
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
NSLog(#"Permission granted");
}
else {
NSLog(#"Permission denied");
}
}];
The same question has been asked before but there is no such api with which you know current status as in iOS8
You can refer Check for mic permission on iOS 7 without showing prompt
Solution:
Another option is you can show the popup or ask for permission first time and save the states of user option selected in NSUserDefaults and than onwards do not ask for permission.
From docs you explicitly do not need to call this if each you do not need to get the permission of user.It will automatically called by AVAudioSession first time when you try to record
Recording audio requires explicit permission from the user. The first
time your app’s audio session attempts to use an audio input route
while using a category that enables recording (see “Audio Session
Categories”), the system automatically prompts the user for
permission; alternatively, you can call requestRecordPermission: to
prompt the user at a time of your choosing
Swift 3 Complete Solution Code
func checkMicPermission() -> Bool {
var permissionCheck: Bool = false
switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
permissionCheck = true
case AVAudioSessionRecordPermission.denied:
permissionCheck = false
case AVAudioSessionRecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
permissionCheck = true
} else {
permissionCheck = false
}
})
default:
break
}
return permissionCheck
}
There is another way you can try following code for ios 7 and 8 :
let microPhoneStatus = AVCaptureDevice.authorizationStatusForMediaType(AVMediaTypeAudio)
switch microPhoneStatus {
case .Authorized:
// Has access
case .Denied:
// No access granted
case .Restricted:
// Microphone disabled in settings
case .NotDetermined:
// Didn't request access yet
}
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
// Microphone enabled code
}
else {
// Microphone disabled code
}
}];
And include <AVFoundation/AVAudioSession.h>
Since none of the other answers here mentioned this, you need to add the permissions to your info.plist. Specifically, add an entry for:
Privacy - Microphone Usage Description
For the String value, enter something like:
(App name) needs access to your microphone.
Otherwise, you get a mysterious crash
What I often end up doing for a quick check on objects working with audio record:
// swift 5
static public func isAuthorized() -> Bool {
return AVCaptureDevice.authorizationStatus(for: .audio) == .authorized
}
import AVFoundation and use the following function
var permissionCheck:Bool = false
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSession.RecordPermission.granted:
permissionCheck = true
case AVAudioSession.RecordPermission.denied:
permissionCheck = false
case AVAudioSession.RecordPermission.undetermined:
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
if granted {
permissionCheck = true
} else {
permissionCheck = false
}
})
default:
break
}

Resources