Skip to content

fix(darwin): create MIDINetworkSession lazily, not at plugin init - #175

Merged
mortenboye merged 3 commits into
InvisibleWrench:masterfrom
aleksei-svezhevskii:fix/lazy-network-session
Sep 3, 2026
Merged

fix(darwin): create MIDINetworkSession lazily, not at plugin init#175
mortenboye merged 3 commits into
InvisibleWrench:masterfrom
aleksei-svezhevskii:fix/lazy-network-session

Conversation

@aleksei-svezhevskii

Copy link
Copy Markdown

Problem

On iOS 14+, touching MIDINetworkSession.default() in setup() makes the system show the "Allow [app] to find devices on local networks" permission prompt at app startup — even though the network session is disabled by default (isEnabled == false) and the app may only use Bluetooth/USB/virtual MIDI.

Users see a confusing network-permission dialog before the first screen, with no way for the app to avoid it (the standard NSLocalNetworkUsageDescription string does not remove the prompt, it only changes the body text).

Fix

Stop creating the session at plugin init. Create it lazily inside setNetworkSessionEnabled(true) — which is already the documented, explicit opt-in for network (RTP) MIDI (see the iOS-specific API added in 0.4.15).

func setNetworkSessionEnabled(enabled: Bool) throws {
#if os(iOS)
    if enabled && session == nil {
        session = MIDINetworkSession.default()
        session?.connectionPolicy = MIDINetworkConnectionPolicy.anyone
    }
    session?.isEnabled = enabled
#endif
}

Impact

  • Bluetooth / USB / virtual MIDI: unaffected — they go through CoreMIDI without a network session.
  • Network MIDI via setNetworkSessionEnabled(true): unchanged — the session is created at the same call, identical behavior afterwards.
  • isNetworkSessionEnabled() still reports false before any session exists, matching the disabled-by-default semantics.

Testing

Verified on the iOS simulator with an app using this plugin:

  • the system Local Network prompt no longer appears at launch;
  • USB/virtual device enumeration is unchanged;
  • setNetworkSessionEnabled(true) still creates and enables the network session.

Touching MIDINetworkSession.default() in setup() makes iOS 14+ show the
system 'Allow [app] to find devices on local networks' permission prompt
at app startup, even though network MIDI is disabled by default. Apps
that only use Bluetooth/USB/virtual MIDI get a confusing permission
dialog before the first screen.

Create the session lazily in setNetworkSessionEnabled(true) instead —
the existing explicit opt-in for network (RTP) MIDI. Bluetooth/USB
MIDI go through CoreMIDI without the network session and are unaffected.

Verified on iOS simulator: prompt no longer appears at launch; BLE/USB
device enumeration unchanged; setNetworkSessionEnabled(true) still
enables the network session.
mortenboye and others added 2 commits September 3, 2026 09:35
Package changelogs in this repo are generated by `melos version` from
conventional commits. A hand-written `## Unreleased` heading would be
stranded above the next version section and ship to pub.dev permanently.
The entry is generated from this PR's `fix(darwin):` commit at release
time instead.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Drops the now-empty `#if os(iOS)` block from `setup()` and moves the
rationale to `setNetworkSessionEnabled`, where the session is actually
created. Without it, a later change can re-add
`MIDINetworkSession.default()` to `setup()` and silently bring back the
iOS 14+ local-network prompt at launch — a regression that only shows on
a real device, not in CI.

Also notes in the README that the system local-network prompt now
appears on the first `setNetworkSessionEnabled(true)` rather than at app
launch.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@mortenboye

Copy link
Copy Markdown
Contributor

Thank you for your contribution, this will go into the next reelase.

@mortenboye
mortenboye merged commit fb9a270 into InvisibleWrench:master Sep 3, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants