Search code examples
iosavfoundationios-simulator

AudioUnitInitialize causes Allow Microphone access prompt on iOS Simulator


I'm using AudioUnit to playback audio from a TeamSpeak server but when I call AudioUnitInitialize on the iOS Simulator, I'm getting constantly the macOS prompt to allow microphone access even if I want to playback only.

enter image description here

On a real device everything works fine without any native prompts but it is really annoying when running the app in the simulator because this prompts appear every time I run the app.

- (void)setupRemoteIO
{
    AudioUnit audioUnit;
    // Describe audio component
    AudioComponentDescription desc;
    desc.componentType = kAudioUnitType_Output;
    desc.componentSubType = kAudioUnitSubType_RemoteIO;
    desc.componentFlags = 0;
    desc.componentFlagsMask = 0;
    desc.componentManufacturer = kAudioUnitManufacturer_Apple;

    // Get component
    AudioComponent inputComponent = AudioComponentFindNext(NULL, &desc);

    // Get audio unit
    OSStatus status = AudioComponentInstanceNew(inputComponent, &audioUnit);
    if (status != noErr)
    {
        printf("AudioIO could not create new audio component: status = %i\n", status);
    }

    UInt32 enableIO;
    AudioUnitElement inputBus = 1;
    AudioUnitElement outputBus = 0;
    //Disabling IO for recording
    enableIO = 0;

    AudioUnitSetProperty(audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Input, inputBus, &enableIO, sizeof(enableIO));

    //Enabling IO for playback
    enableIO = 1;
    AudioUnitSetProperty(audioUnit, kAudioOutputUnitProperty_EnableIO, kAudioUnitScope_Output, outputBus, &enableIO, sizeof(enableIO));

    // initialize
    status = AudioUnitInitialize(audioUnit);
    if (status != noErr)
    {
        printf("AudioIO could not initialize audio unit: status = %i\n", status);
    }
}

Solution

  • This is a known bug with Xcode (previous to 10.2) from macOS Mojave (I say known because it has happened to me a lot of time when playing video but also because when I was looking for it I found a lot of people having the same issue); althought I couldn't find any report from Apple. Probably there can be some workaround depending on the environment, the way you launch the app, the version of Xcode and the version of macOS Mojave you have.

    This will happen only in the simulator, and as you also said it won't happen on real device as most of the apps don't need microphone access for playing with Audio/Video features.

    In the meantime this bug get resolved, you can try:

    1. Going to "Security & Privacy" settings on your macOS

    2. "Microphone" on the left panel

    3. Then on the right panel disable the option for Xcode

    disable microphone

    Another thing you can try to get rid of the message is to change Hardware Audio Input to Internal Microphone:

    Hardware Audio Input

    Update in Xcode 10.2:

    You’re now only prompted once to authorize microphone access to all simulator devices. (45715977)