Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

objective c - 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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

enter image description here

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

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
    })
}

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...