Ben Clayton..
51
如果您仍在使用iOS SDK 6.0进行编译(就像我一样),您必须比@Luis E. Prado更加间接,因为requestRecordPermission方法不存在.
这就是我做到的.如果您使用ARC,请删除自动释放位.在iOS6上没有任何反应,在iOS7上会记录"麦克风已启用"消息或弹出警报.
AVAudioSession *session = [AVAudioSession sharedInstance];
if ([session respondsToSelector:@selector(requestRecordPermission:)]) {
[session performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
if (granted) {
// Microphone enabled code
NSLog(@"Microphone is enabled..");
}
else {
// Microphone disabled code
NSLog(@"Microphone is disabled..");
// We're in a background thread here, so jump to main thread to do UI work.
dispatch_async(dispatch_get_main_queue(), ^{
[[[[UIAlertView alloc] initWithTitle:@"Microphone Access Denied"
message:@"This app requires access to your device's Microphone.\n\nPlease enable Microphone access for this app in Settings / Privacy / Microphone"
delegate:nil
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil] autorelease] show];
});
}
}];
}
编辑:事实证明,withObject块是在后台线程中执行的,所以不要在那里做任何UI工作,否则你的应用程序可能会挂起.我已经调整了上面的代码.一位客户指出了这一点,感谢测试版.为这个错误道歉.