简体   繁体   English

加载应用程序时的离子调用功能

[英]Ionic call function when app is loaded

I would like to check every time the app is loaded if the device has an internet connection. 我想在每次加载应用程序时检查设备是否可以连接互联网。 This function has nothing to do with any of my Ionic Views/pages. 此功能与我的任何“离子视图” /页面无关。 I don't need to make this check on a specific page. 我不需要在特定页面上进行此检查。 I need it to run every time the app is actually loaded on the screen. 每当应用实际在屏幕上加载时,我都需要运行它。

For example, if I hit the app icon from my home screen and load it, the check function should run. 例如,如果我从主屏幕上点击了应用程序图标并加载了它,则检查功能应运行。 Then I decide to open my emails to check on something. 然后,我决定打开我的电子邮件以检查某些内容。 At the end, I just re-open my app which is running in the background (Not terminated), the app should check again for an internet connection. 最后,我只是重新打开在后台运行的应用程序(未终止),该应用程序应再次检查互联网连接。

Im not asking how I'm I gonna write the function to check, but where shall I place that segment of code to run only when the app is loaded regardless the view/page of the app. 我不是在问我如何编写要检查的功能,而是我应该将这段代码放置在什么位置,以便仅在加载应用程序时运行,而不管应用程序的视图/页面如何。

For the first check, when you start your app, add your check code inside $ionicPlatform.ready method (app.js file). 对于第一次检查,在启动应用程序时,请将检查代码添加到$ ionicPlatform.ready方法(app.js文件)中。

For the check when you return to app from other activity, create inside .run method (app.js file) the $ionicPlatform.on method. 对于从其他活动返回到应用程序时的检查,请在.run方法(app.js文件)内部创建$ ionicPlatform.on方法。

Something like this (at app.js file): 这样的事情(在app.js文件中):

angular.module('starter', ['ionic', 'starter.controllers', 'starter.services'])

.run(function($ionicPlatform) {

  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)

    // PUT YOUR CHECK CODE HERE, TRIGGERED WHEN THE APP RUN FOR THE FIRST TIME

    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);

    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });

  $ionicPlatform.on('resume', function() {
   // PUT YOUR CHECK CODE HERE TOO. TRIGGERED WHEN YOU RETURN TO APP FROM OTHER ACTIVITY.
  });
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ionic 4 android 调用 function 后恢复时应用程序冻结 - Ionic 4 android app freeze when resume after call function 调用函数更新App时Ionic Force App Update崩溃 - Ionic Force App Update crashes when calling the function to update the App 每次应用程序以离子形式启动时,每次执行一个函数 - execute a function each time, when app started in ionic 页面加载时调用 onCreateOptionsMenu - Call onCreateOptionsMenu when page is loaded 从离子应用程序调用本机android应用程序 - Call a native android app from ionic app dataSnapshot function 不工作 - 调用 function 时应用程序崩溃 - dataSnapshot function not working - App crashing when function call 当我模拟Ionic App它会抛出TypeError:无法在undefined上调用方法 - When I emulate Ionic App it throws TypeError: Can't call method on undefined 动态加载的图像不会在 Ionic 应用程序中呈现 - Dynamically Loaded Images Won’t Render in Ionic App 如何在科尔多瓦离子应用程序中调用onActivityResult - how to call onActivityResult in cordova ionic app 活动加载后如何调用函数 - How to call a function after activity is loaded
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM