为什么我的应用不请求使用麦克风的权限?



正在录制应用程序。所以我从零开始,基本上直接进入录音。我确保添加了"隐私-麦克风使用说明"字符串。

让我感到奇怪的是,该应用程序直接启动录制。第一次请求用户许可时不会出现任何警报,我认为这是常态。

我误解什么了吗?

override func viewDidLoad()   {
super.viewDidLoad()
record3()
}
func record3()   {
print ("recording")
let node = audioEngine.inputNode
let recordingFormat = node.inputFormat(forBus: 0)
var silencish = 0
var wordsish  = 0
makeFile(format: recordingFormat)
node.installTap(onBus: 0, bufferSize: 8192, format: recordingFormat, block: {
[self]
(buffer, _) in
do {
try audioFile!.write(from: buffer);
x += 1;
if x > 300 {
print ("it's over sergio")
endThis()
}
} catch {return};})

audioEngine.prepare()
do {
try audioEngine.start()
} catch let error {
print ("oh catch (error)")
}
}

即使模拟器还记得,如果您以前授权使用麦克风,他们也不会再问。如果需要,删除重新安装应用程序。

如果不是这样:

我从未在swift应用程序中使用过麦克风,但我确实有一些使用iOS权限的经验。例如,当我使用location时,我使用了一个请求用户许可的函数:

class ContentModel: NSObject, CLLocationManagerDelegate, ObservableObject {
var locationManager = CLLocationManager()
func requestGeolocationPermission() {

// Request permission from the user
locationManager.requestWhenInUseAuthorization()
}
}

类似的东西不能应用于你的情况吗?

尝试

声明权限请求函数

func requestRecordPermission(_ response: @escaping (Bool) -> Void)

然后请求录制许可。

AVAudioSession.sharedInstance().requestRecordPermission { granted in
if granted {
// The user granted access. Present recording interface.
} else {
// Present message to user indicating that recording
// can't be performed until they change their preference
// under Settings -> Privacy -> Microphone
}
}

这来自苹果开发者文档,如果你想进一步阅读,这里有链接:https://developer.apple.com/documentation/avfaudio/avaudiosession/1616601-requestrecordpermission

最新更新