Ask Permissions before use in-built features in iOS - ios

I am using camera, microPhone and current location in my iOS App like SOS Button functionality. Its working fine but when I press this button first time its asking permissions for using these in-built features. How to ask permissions before using this SOS Button first time.

you can check in app delegate when app is launch so your permission is granted.
let mediatype = AVMediaType.video
let AuthoriseStatus = AVCaptureDevice.authorizationStatus(for: cameraMediaType)
switch AuthoriseStatus {
case .denied: break
case .authorized: break
case .restricted: break
case .notDetermined:
// permission prompt
AVCaptureDevice.requestAccess(for: mediatype) { granted in
if granted {
print("Granted access to \(mediatype)")
} else {
print("Denied access to \(mediatype)")
}
}
}

Related

App Tracking Transparency on mac Silicon crash

I have iOS application (that works properly on iOS) and it has enabled destination: Mac(Designed for iPad). When run it on mac M1, the dialog doesnt appear and the code that it used to ask permissions for tracking always returns ATTrackingManager.AuthorizationStatus.notDetermined
The code it here:
if #available(iOS 14, *) {
ATTrackingManager.requestTrackingAuthorization { [weak self] status in
switch status {
case .authorized:
DispatchQueue.main.async {
self?.didTrackingAuthorized?()
}
// Tracking authorization dialog was shown
// and we are authorized
case .denied:
// Tracking authorization dialog was
// shown and permission is denied
print("FB - Denied")
case .notDetermined:
self?.setup()
print("FB - Not Determined")
case .restricted:
print("FB - Restricted")
#unknown default:
print("FB - Unknown")
}
}
}
In documentation I read the Note about such behaviour:
If you call ATTrackingManager.trackingAuthorizationStatus in macOS, ATTrackingManager.AuthorizationStatus.notDetermined returns.
So the question is how in this case properly ask user permission for tracking activity on macos?

Photo Library and Camera are accessible after permission got denied

I'm currently working on an app where I need to access either the photo library or the camera (or even both, it's up to the user).
If I close the app and deny the access to both camera and library for the app.
After restarting my App, it's still possible to access both, I can open and use the camera or even pick a photo from the library.
How is this possible, I thought Apple would restrict the access.
I dont think so. Apple always restrict use of camera or photo library if access is denied. Please use following code to check current status and verify it.
import Photos
if type == .CAMERA{
if AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo) == AVAuthorizationStatus.authorized {
completionHander(true) //Allowed
} else {
//Dont Know
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (granted: Bool) -> Void in
completionHander(granted)
})
}
}else{
let status = PHPhotoLibrary.authorizationStatus()
switch status {
case .authorized:
completionHander(true)//Allowed
break
case .denied, .restricted :
completionHander(false)//Not Allowed
break
case .notDetermined:
//Dont Know
PHPhotoLibrary.requestAuthorization { status in
switch status {
case .authorized:
completionHander(true)
break
case .denied, .restricted:
completionHander(false)
break
case .notDetermined:
completionHander(false)
break
}
}
}
}
Okay I know the answer...
I just didn't get the concept.
If I deny the access to the photoLibrary it is not possible to save images but, if Camera access is granted I can still use the photoLibrary.
If camera access is denied, I still get the alert with the context from the info.plist

Not showing alert even when I add Camera Usage Description / Photo Library Usage Description

I have camera/ photo library in my app, so I add request permission in plist. However, after running app, it does not even pop up alert. Just access to pick photo / camera. Is there anything I miss to add?
Thanks!
Requesting Authorization for Media Capture on iOS:
https://developer.apple.com/documentation/avfoundation/cameras_and_media_capture/requesting_authorization_for_media_capture_on_ios
Configure Your App's Info.plist File:
If your app uses device cameras, include the 'NSCameraUsageDescription' key in your app’s Info.plist file.
Verify and Request Authorization for Capture:
override func viewDidLoad() {
super.viewDidLoad()
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized: // The user has previously granted access to the camera.
.....
case .notDetermined: // The user has not yet been asked for camera access.
AVCaptureDevice.requestAccess(for: .video) { granted in
if granted {
.....
}
}
case .denied: // The user has previously denied access.
return
case .restricted: // The user can't grant access due to restrictions.
return
}
}

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
}

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