Giter Site home page Giter Site logo

Comments (13)

thoechtl avatar thoechtl commented on April 28, 2024 14

If using Angular you have to run it in NgZone to see immediate network changes in your app.

Here is a working example:

import { BehaviorSubject, Observable } from 'rxjs';
import { NgZone } from '@angular/core';
import { Plugins } from '@capacitor/core';
const { Network } = Plugins;

export class NetworkService {
  isOnline$: Observable<boolean>;
  private statusSubject = new BehaviorSubject<boolean>(false);

  constructor(
     private zone: NgZone,
  ) {
    Network.getStatus().then(status => this.statusSubject.next(status.connected));
    this.isOnline$ = this.statusSubject.asObservable();

    Network.addListener('networkStatusChange', (status) => {
      this.zone.run(() => {
        this.statusSubject.next(status.connected);
      });
    });
  }

Then simply subscribe to isOnline$ and everthing should work as expected.

from capacitor-plugins.

hanifnouhi avatar hanifnouhi commented on April 28, 2024 5

Today, I face this problem on android (real device).
I have to mention that this is not the only issue with this plugin. After I turn off the wifi on real device the network plugin can't always show the correct network connection type. for example after turning the wifi off, my device connect to mobile data but the network type is shown as 'wifi'.

from capacitor-plugins.

miguelcarrascoq avatar miguelcarrascoq commented on April 28, 2024 5

NetworkService

that work for me (src/app/shared/services/network.service.ts):

import { Injectable } from '@angular/core';
import { NgZone } from '@angular/core';
import { Plugins } from '@capacitor/core';
const { Network } = Plugins;

@Injectable({
  providedIn: 'root',
})
export class NetworkService {
  isOnline: boolean;

  constructor(private zone: NgZone) {
    Network.getStatus().then((status) => {
      console.log('isOnline', status.connected);
      this.isOnline = status.connected;
    });

    Network.addListener('networkStatusChange', (status) => {
      this.zone.run(() => {
        console.log('isOnline', status.connected);
        this.isOnline = status.connected;
      });
    });
  }
}

from capacitor-plugins.

drtomato avatar drtomato commented on April 28, 2024 4

I have tested the Network plugin with an app on a real iOS device by walking out of and then into WiFi coverage (no SIM in the device so only connectivity via WiFi). The app was either in activated, deactivated or closed state when walking out of and then into coverage, which resulted in a test matrix with nine test cases. When back in coverage I activated the app and checked the status. Seven of the test cases worked but two did not:

  1. a) App in active and connected state. b) Closed the app (i.e., shut it down completely) c) Walked out of WiFi coverage d) Activated the app: the disconnected state was properly detected e) Entered WiFi coverage. Expected result: a network change event ['wifi', connected: true]. Actual result: no event fired.

  2. a) App in active and connected state. b) Closed the app (i.e., shut it down completely) c) Walked out of WiFi coverage d) Activated the app: the disconnected state was properly detected d) Deactivated the app (i.e. backgrounded it) e) Entered WiFi coverage. f) Activated the app. Expected result: a network change event and/or status ['wifi', connected: true]. Actual result: no event fired, status ['none', connected: false]

So this is still a problem. Unfortunately, I have no code to share but I hope this description helps to narrow down where the problem might be. These failing test cases were the only ones where the app was activated from closed state while the network was disconnected. The disconnected state was picked up by the application but no events seem to have been fired after that. Could there be a bug where the internal event handling is not properly set up when the app is started while the network is disconnected?

EDIT:

I tried this again, now with the listener running inside NgZone as shown above and now the events are received as they should! I have always regarded NgZone as an important part of Angular's change detection and I don't understand why running a listener to a Capacitor plugin inside NgZone is necessary to make it work. If someone can explain this I would appreciate it :)

from capacitor-plugins.

tigohenryschultz avatar tigohenryschultz commented on April 28, 2024 3

I have issues with this on a regular iOS phone. Is this ever going to be looked into, or is Ionic giving up?

from capacitor-plugins.

jcesarmobile avatar jcesarmobile commented on April 28, 2024

I can reproduce on the simulator, but not on real devices, setting as low priority

from capacitor-plugins.

jansgescheit avatar jansgescheit commented on April 28, 2024

On my case is iOS broken. The native CAPLog prints all network state changes correct!
But the events are not broadcasting to the web layer. if i run Plugins.Network.getStatus() for testing, the result is always wifi. It looks like that the bridge is on the Network interface broken.

Latest Dependencies:

  @capacitor/cli: 1.5.0
  @capacitor/core: 1.5.0
  @capacitor/android: 1.5.0
  @capacitor/ios: 1.5.0

Installed Dependencies:

  @capacitor/cli 1.5.0
  @capacitor/ios 1.5.0
  @capacitor/android 1.5.0
  @capacitor/core 1.5.0
    async ngOnInit() {

        this.platform.ready().then(async () => {
            // networkStatusChange is never fired
            Network.addListener('networkStatusChange', s => console.log('networkStatusChange', s));
           
             // always returns wifi
            const status = await Network.getStatus();
            console.log('networkStatus', status); 
       });
}

from capacitor-plugins.

kevinclarkadstech avatar kevinclarkadstech commented on April 28, 2024

Ah, same issues here. The status change does not fire on iOS device. Get status seems to work when the app loads.

I'm trying to dynamically load lower or higher quality photos based on the network status. It would also be nice to see if network status is 3G or 4G, so 3G would load low quality and 4G would load standard quality. Is that functionality going to be available at some point? Thanks for all your hard work guys!

from capacitor-plugins.

dmitry-stepanenko avatar dmitry-stepanenko commented on April 28, 2024

@kevinclarkadstech you can use ionic-native plugin for network instead, it has similar issue on IOS, but you can trigger the updates by toggling your app with any other.

from capacitor-plugins.

hanifnouhi avatar hanifnouhi commented on April 28, 2024

from capacitor-plugins.

jcesarmobile avatar jcesarmobile commented on April 28, 2024

moving the plugin to capacitor-plugins repo since network plugin has been moved here

please, for people having problems with the Android implementation, report a new issue, this one is about iOS.

I can still reproduce with the new implementation, but looks like a bug on the simulator more than a bug in the code

from capacitor-plugins.

micheal-w-wells avatar micheal-w-wells commented on April 28, 2024

The output of (await Network.getStatus()).connected) is often out of sync with the simulator in turning wifi on and off. The events also are unreliable on ios.

💊   Capacitor Doctor  💊 

Latest Dependencies:

  @capacitor/cli: 3.2.0
  @capacitor/core: 3.2.0
  @capacitor/android: 3.2.0
  @capacitor/ios: 3.2.0

Installed Dependencies:

  @capacitor/cli: 3.1.2
  @capacitor/core: 3.1.2
  @capacitor/android: 3.1.2
  @capacitor/ios: 3.1.2

[success] iOS looking great! 👌
[success] Android looking great! 👌```


from capacitor-plugins.

mohammad2002-1381 avatar mohammad2002-1381 commented on April 28, 2024

i just added

"plugins": {
    "CapacitorHttp": {
      "enabled": true
    }
  }

into capacitor.config.json file and internet connection issue was solved

from capacitor-plugins.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.