Files
swiftcontrol/lib/utils/requirements/local_network.dart
Jonas Bark 905c2641cd feat(ios/macos): detect the Local Network permission instead of failing silently
Denial of Apple's Local Network privacy permission is the most common cause of
"BikeControl can't see my trainer app" on iOS and macOS, and it fails
completely silently: Bonjour browses return nothing forever, advertisements
never leave the device, nothing throws. An app watching only for errors cannot
tell "permission is off" from "there is nothing out there".

Apple ships no authorization-status API for it (not iOS 14...26, not macOS
15...26), so the new local_network_permission plugin measures it: an NWListener
and an NWBrowser on _bikecontrol-preflight._tcp, then

  browser discovers the listener        -> granted
  kDNSServiceErr_PolicyDenied (-65570)  -> denied
  neither, before the deadline          -> unknown

unknown is a real outcome, not an error — a device with no usable network never
completes the round trip even when the permission is fine — so everything fails
open on it and only a positive denial blocks the user.

The probe *is* the request: the system prompt fires on the first Bonjour
operation, so there is no separate request step, and after the user answers,
System Settings is the only way back.

- LocalNetworkAccess caches the result (probes cost a real round trip while
  getStatus() runs on every tile mount, tap and resume) and collapses
  concurrent probes into one, since two listeners on the same service type
  would resolve each other.
- LocalNetworkRequirement gates the three LAN tiles: MyWhoosh Link, Zwift mDNS
  and OpenBikeControl mDNS. Suppressed in screenshot/demo mode like the rest.
- Diagnostics drop the old guess (inferred from whether a discovery scan
  returned anything, which could not tell denied from an empty LAN) for the
  measured local-network=granted|denied|unknown.
- _bikecontrol-preflight._tcp added to NSBonjourServices on both platforms;
  without it the browse fails for a reason unrelated to the permission.

The Xcode project deployment-target bumps (ios 13.0->15.0, macos 10.15->12.0)
are Flutter's own migration aligning project defaults with the Podfiles; they
happen on any first build.

Verified with real builds on both platforms, not just analysis: flutter build
macos --debug and flutter build ios --no-codesign both succeed, the framework
is linked in, and the preflight service type is present in both built
Info.plists. 35 tests cover the mapping, cache, concurrency and fail-open
behaviour.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-21 07:58:18 +02:00

60 lines
2.4 KiB
Dart

import 'dart:io';
import 'package:bike_control/gen/l10n.dart';
import 'package:bike_control/main.dart' show screenshotMode;
import 'package:bike_control/services/local_network_access.dart';
import 'package:bike_control/utils/demo_mode.dart';
import 'package:bike_control/utils/requirements/platform.dart';
import 'package:flutter/foundation.dart';
import 'package:local_network_permission/local_network_permission.dart';
import 'package:shadcn_flutter/shadcn_flutter.dart';
/// Gates the LAN bridging methods (MyWhoosh Link, Zwift/OpenBikeControl mDNS)
/// on Apple's Local Network permission.
///
/// Unlike every other requirement here there is no "request" step to speak
/// of: the probe behind [getStatus] *is* the request, because the system
/// prompt fires on the first Bonjour operation. Once the user has answered,
/// [call] is the only way back — the prompt never returns.
class LocalNetworkRequirement extends PlatformRequirement {
LocalNetworkRequirement()
: super(
AppLocalizations.current.localNetworkAccess,
description: !kIsWeb && Platform.isIOS
? AppLocalizations.current.localNetworkAccessDeniedIos
: AppLocalizations.current.localNetworkAccessDeniedMacos,
icon: Icons.wifi_tethering,
);
/// Only a positive denial blocks the user.
///
/// [LocalNetworkStatus.unknown] means the probe could not tell — no usable
/// network, or a build without the plugin — and failing closed on that would
/// break setups that work today.
@override
Future<bool> getStatus() async {
status = await LocalNetworkAccess.isUsable();
return status;
}
@override
Future<void> call(BuildContext context, VoidCallback onUpdate) async {
await LocalNetworkPermission.openSettings();
// Whatever we cached stopped being true the moment the user left for
// System Settings; the resume path re-reads the status from scratch.
LocalNetworkAccess.invalidate();
onUpdate();
}
}
/// The Local Network requirement on the platforms that have one, else nothing.
///
/// Suppressed for screenshots and demos alongside every other permission, so
/// a machine that happens to have the toggle off can't silently switch the
/// bridging methods back off mid-capture.
List<PlatformRequirement> localNetworkRequirements() {
if (screenshotMode || demoHidePermissions) return const [];
if (kIsWeb || !(Platform.isIOS || Platform.isMacOS)) return const [];
return [LocalNetworkRequirement()];
}