检测应用程序重新聚焦事件 [英] Detect application refocus event
问题描述
每次用户开始使用该应用程序时我都需要知道。
I need to know each time the user is starting to use the application.
案例1(确定):应用程序未启动。
用户从头开始申请。
我添加了一个应用程序引导程序上的监听器,我知道它什么时候开始。
I add a listener on the app's bootstrap, and I am aware when it starts.
案例2(TODO):该应用程序已经启动,但它不再处于活动状态
用户从任务栏重新加载应用程序。
我想要知道应用程序何时从任务栏到达前台(如alt + tab)。
I want to know when the application comes from the taskbar to the foreground (like a alt+tab).
抓住这个很棘手,因为应用仍在运行,我不知道要听哪个事件。事实上,我甚至不知道如何命名这种行为。
This is tricky to catch, because the app is still running and I don't know which event to listen to. In fact, I don't even know how to name this behaviour.
推荐答案
ios-app开发者接受了帮助。他的回答非常适合我,因为它似乎很干净且可重复使用。所以我将在这里制作它:
An ios-app developper accepted to help me. His answer suits me very well, as it seem clean et reusable. So I will produce it here :
可以通过 applicationWillEnterForeground 事件捕获app come to foreground事件。
Phonegap / Cordova允许通过Cordova类调用javascript函数。 webView 对象有一个专用方法来启动js脚本。
The "app come to foreground" event can be catched through the applicationWillEnterForeground event. Phonegap/Cordova allows to call javascript functions through the Cordova classes. The webView object has a dedicated method to launch js scripts.
所以我打开了文件Projet / Classes / Cordova / AppDelegate.m:
So I opened the file Projet/Classes/Cordova/AppDelegate.m :
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(@"applicationWillEnterForeground: %@", self.viewController.webView);
[self.viewController.webView stringByEvaluatingJavaScriptFromString:@"notifyForegroundEvent();"];
}
我在根目录的某处添加了notifyForegroundEvent()方法我的js文件:
And I added the notifyForegroundEvent() method somewhere in the root of my js files :
var notifyForegroundEvent = function() {
console.log('notifyForegroundEvent()');
// Do something here
}
Etvoilà
这篇关于检测应用程序重新聚焦事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!