gpt4 book ai didi

objective-c - 如何在启动时检查麦克风访问权限?

转载 作者:太空狗 更新时间:2023-10-30 03:36:39 32 4
gpt4 key购买 nike

在我的应用中,我将使用麦克风进行一些录音。从iOS7.0开始,要求用户在开始音频之前检查访问麦克风的权限。

我的应用程序中有一个“开始录制”按钮。这里首先检查用户的录音权限。

这是执行此操作的代码:

if([[AVAudioSession sharedInstance] respondsToSelector:@selector(requestRecordPermission:)]) {
[[AVAudioSession sharedInstance] performSelector:@selector(requestRecordPermission:)
withObject:permissionBlock];
}
#ifndef __IPHONE_7_0
typedef void (^PermissionBlock)(BOOL granted);
#endif

PermissionBlock permissionBlock = ^(BOOL granted) {
NSLog(@"permissionBlock");
if (granted) {
[self doActualRecording];
} else {
// Warn no access to microphone
}
};

现在,我想请求用户在用户启动应用程序时授权使用麦克风。然后当用户选择录制按钮时,它会再次弹出消息。

位置服务也有类似的功能。我如何才能访问麦克风?

最佳答案

一旦用户拒绝了您应用的麦克风访问权限,您就无法再次向他们显示权限对话框。使用保存的设置。相反,您可以提示用户进入他们的设置并进行更改。

[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
NSLog(@"granted");
} else {
NSLog(@"denied");

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
message:@"You must allow microphone access in Settings > Privacy > Microphone"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
}];

关于objective-c - 如何在启动时检查麦克风访问权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21142630/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com