iOS 检查应用程序是否可以访问麦克风
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24981333/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
iOS check if application has access to microphone
提问by 130e13a
With the introduction of iOS 7, applications have to request microphone access when they want to record audio.
随着 iOS 7 的推出,应用程序在想要录制音频时必须请求麦克风访问权限。
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?
如何检查应用程序是否可以访问麦克风?
在 iOS 8 SDK 中,我可以使用AVAudioSessionRecordPermission
枚举,但如何在 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
}
回答by CodeBender
You can check the with recordPermission(), which has been available since iOS 8.
您可以使用从 iOS 8 开始提供的recordPermission()进行检查。
Keep in mind that starting with iOS 10, you must setthe NSMicrophoneUsageDescription
property in your info.plist
for microphone permissions. You must provide a value that will be shown in the access request, and if localizing your app, be sure to include your plist strings for translation.
请记住,从 iOS 10 开始,您必须NSMicrophoneUsageDescription
在info.plist
麦克风权限中设置该属性。您必须提供一个将显示在访问请求中的值,并且如果本地化您的应用程序,请确保包含您的 plist 字符串以进行翻译。
Failure to do so will result in a crash when attempting to access the microphone.
否则将在尝试访问麦克风时导致崩溃。
This answer has been cleaned up again for Swift 4.x
此答案已为Swift 4.x再次清理
import AVFoundation
switch AVAudioSession.sharedInstance().recordPermission {
case AVAudioSessionRecordPermission.granted:
print("Permission granted")
case AVAudioSessionRecordPermission.denied:
print("Pemission denied")
case AVAudioSessionRecordPermission.undetermined:
print("Request permission here")
AVAudioSession.sharedInstance().requestRecordPermission({ (granted) in
// Handle granted
})
}
Swift 3.0
斯威夫特 3.0
This has been tested against an iOS 10 environment for checking the microphone's current state.
这已经针对 iOS 10 环境进行了测试,以检查麦克风的当前状态。
import AVFoundation
switch AVAudioSession.sharedInstance().recordPermission() {
case AVAudioSessionRecordPermission.granted:
print("Permission granted")
case AVAudioSessionRecordPermission.denied:
print("Pemission denied")
case AVAudioSessionRecordPermission.undetermined:
print("Request permission here")
default:
break
}
Objective-C
目标-C
I have tested this code with iOS 8 for the purpose of checking for microphone permission and obtaining the current state.
我已经用 iOS 8 测试了这段代码,目的是检查麦克风权限并获取当前状态。
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
.
与往常一样,请确保import AVFoundation
.
回答by codester
In iOS7
there is no way to get the current status of microphone authorization
.They have given the enum in iOS8
as AVAudioSessionRecordPermission
在iOS7
没有办法获得当前状态的microphone authorization
. 他们已经将枚举iOS8
作为AVAudioSessionRecordPermission
In iOS7
you have to request permission every time with
在iOS7
你每次都必须请求许可
[[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
之前已经问过同样的问题,但没有这样的 api 可以让您知道当前状态 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 timeand 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
另一个选项是您可以第一次显示popup
或请求许可并保存选择的用户选项的状态,然后不请求许可。从文档中,如果您不需要获得用户的许可,则您明确不需要调用它。当您尝试录制时,它会在第一次自动调用 NSUserDefaults
AVAudioSession
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
录制音频需要用户的明确许可。当您的应用的音频会话第一次尝试使用音频输入路由时,同时使用启用录音的类别(参见“音频会话类别”),系统会自动提示用户授予权限;或者,您可以调用 requestRecordPermission: 在您选择的时间提示用户
回答by mriaz0011
Swift 3 Complete Solution Code
Swift 3 完整解决方案代码
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
}
回答by user3873271
There is another way you can try following code for ios 7 and 8 :
还有另一种方法可以尝试为 ios 7 和 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
}
回答by souvickcse
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
// Microphone enabled code
}
else {
// Microphone disabled code
}
}];
And include <AVFoundation/AVAudioSession.h>
并包括 <AVFoundation/AVAudioSession.h>
回答by Hugh
Since none of the other answers here mentioned this, you need to add the permissions to your info.plist. Specifically, add an entry for:
由于这里的其他答案都没有提到这一点,因此您需要将权限添加到 info.plist。具体来说,添加一个条目:
Privacy - Microphone Usage Description
隐私 - 麦克风使用说明
For the String value, enter something like: (App name) needs access to your microphone.
对于字符串值,输入如下内容:(应用程序名称)需要访问您的麦克风。
Otherwise, you get a mysterious crash
否则,您会遇到神秘的崩溃
回答by varunrathi28
import AVFoundation and use the following function
导入 AVFoundation 并使用以下函数
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
}
回答by itMaxence
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
}