- Wenn die App offline geht (Flugzeugmodus oder kein WLAN), erkennt sie manchmal (in meinem Fall jedes Mal) nicht, dass sie wieder online ist.
- Ich habe Network.addListener('networkStatusChange'), manuelle Abfrage mit Network.getStatus() und das Einreihen von Offline-Aktionen in die Warteschlange ausprobiert – keine davon funktioniert zuverlässig.
- App auf iOS-Gerät oder Simulator ausführen.
- Offline wechseln.
- Zurück online wechseln.
- Beachten Sie, dass die Online-Erkennung fehlschlägt.
. In meinem Fall löste jedoch keine der dort vorgeschlagenen Lösungen das Problem.
Wie kann ich zuverlässig erkennen, wann die iOS-App wieder online ist? Gibt es bekannte Problemumgehungen oder Korrekturen für diese Einschränkung?
Minimal reproduzierbares Beispiel:
Code: Select all
// sandbox-code: https://github.com/sidharth74659/capacitor-network-ios-mwe
// Reproduces only on iOS (device & simulator). Android works correctly.
import { CommonModule } from '@angular/common';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { PluginListenerHandle } from '@capacitor/core';
import { Network } from '@capacitor/network';
import {
IonButton,
IonContent,
IonHeader,
IonItem,
IonList,
IonTitle,
IonToolbar
} from '@ionic/angular/standalone';
@Component({
standalone: true,
selector: 'app-home',
templateUrl: 'home.page.html',
imports: [
CommonModule,
IonHeader,
IonToolbar,
IonTitle,
IonContent,
IonButton,
IonList,
IonItem
],
})
export class HomePage implements OnInit, OnDestroy {
isOnline = false;
logs: string[] = [];
networkListener?: PluginListenerHandle;
async ngOnInit() {
const status = await Network.getStatus();
this.isOnline = status.connected;
this.log(`Initial getStatus(): ${this.state()}`);
this.networkListener = await Network.addListener(
'networkStatusChange',
status => {
this.isOnline = status.connected;
this.log(`networkStatusChange fired: ${this.state()}`);
}
);
}
ngOnDestroy() {
this.networkListener?.remove();
}
async checkNetworkStatus() {
const status = await Network.getStatus();
this.isOnline = status.connected;
this.log(`Manual getStatus(): ${this.state()}`);
}
private log(message: string) {
const time = new Date().toLocaleTimeString();
this.logs.unshift(`${time} - ${message}`);
}
private state() {
return this.isOnline ? 'ONLINE' : 'OFFLINE';
}
}
Code: Select all
Capacitor Network iOS MWE
{{ isOnline ? 'ONLINE' : 'OFFLINE' }}
Check Network.getStatus()
{{ log }}
Mobile version