2019年11月19日 星期二

[morse]Ionic Native Network (網絡)

Network(網絡)


Installation(安裝)

ionic cordova plugin add cordova-plugin-network-information
npm install @ionic-native/network


Usage(使用方法)


app.module.ts

import { Network } from '@ionic-native/network/ngx';

@NgModule( {
    ...
    providers: [
        Network
    ],
} )


.ts
import { Network } from '@ionic-native/network/ngx';

constructor(private network: Network) { }

...

// watch network for a disconnection
let disconnectSubscription = this.network.onDisconnect().subscribe(() => {
  console.log('network was disconnected :-(');
});

// stop disconnect watch
disconnectSubscription.unsubscribe();


// watch network for a connection
let connectSubscription = this.network.onConnect().subscribe(() => {
  console.log('network connected!');
  // We just got a connection but we need to wait briefly
   // before we determine the connection type. Might need to wait.
  // prior to doing any api requests as well.
  setTimeout(() => {
    if (this.network.type === 'wifi') {
      console.log('we got a wifi connection, woohoo!');
    }
  }, 3000);
});

// stop connect watch
connectSubscription.unsubscribe();








From" https://ionicframework.com/docs/native/network









沒有留言:

張貼留言