如果存在UIApplicationLaunchOptionsLocationKey,则启动Cordova iOS应用程序 [英] Starting Cordova iOS app if UIApplicationLaunchOptionsLocationKey is present

查看:1447
本文介绍了如果存在UIApplicationLaunchOptionsLocationKey,则启动Cordova iOS应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cordova应用程式记录使用者的行程。它在后台工作正常,我想让它自动重新启动,如果它被杀死了某种原因(用户刷它,手机重新启动等)。

I have a Cordova app that records user's trips. It works fine in the background, and I'd like to get it automatically restarted if it's killed for some reason (user swipes it away, phone restarts, etc.).

我正在监控一个区域,当用户进入/退出时,应用程序会部分重新启动。 部分地在这里意味着应用程序只在后台运行 - Cordova加载我的应用程序的插件,但它从不加载WebView。

I'm monitoring a region, and the app partially restarts when the user enters/exits from that. 'Partially' here means the app only runs in the background - Cordova loads my app's plugins, but it never loads the WebView.

对于这个部分重新启动,我在听从UIApplicationDidFinishLaunchingNotification与UIApplicationLaunchOptionsLocationKey从我的插件的pluginInitialize方法和开始位置服务立即保持运行。

For this partial restart, I'm listening for UIApplicationDidFinishLaunchingNotification with UIApplicationLaunchOptionsLocationKey from my plugin's pluginInitialize method and starting location services right away to stay running.

为什么WebView没有加载?

Why doesn't the WebView load? Is there something I need to do to trigger the next stage of initialization when started by iOS?

EDIT 2016/3/7

EDIT 2016/3/7

我做了一些研究和调试。显然,iOS在后台启动我的应用程序;这是我自动启动这种方式,而手动启动,从日志行:

I did some research and debugging. Apparently, iOS starts my app in the background; here's what I get when auto-started this way vs. a manual start, from the logging line:

        NSLog(@"Launched in: bg:%d active:%d inactive:%d", state == UIApplicationStateBackground, state == UIApplicationStateActive, state == UIApplicationStateInactive);

        Launched in: bg:1 active:0 inactive:0
        Launched in: bg:0 active:0 inactive:1

我验证了这两种情况下都得到的代码(来自cordova-ios'v.3.9.2(Cordova v.5.4.1)CDVViewController:

I verified this code was being reached in both cases (from cordova-ios' v.3.9.2 (Cordova v.5.4.1) CDVViewController:

        NSURLRequest* appReq = [NSURLRequest requestWithURL:appURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
        [self.webView loadRequest:appReq];

我在手动情况下看到此加载完成,但不是自动启动情况, line:

I see this load finish in the manual case, but not the auto-start case, with the log line:

        Finished load of: file:///var/mobile/Containers/Bundle/Application/[device id]/[app name].app/www/index.html

为什么在自动启动时?

推荐答案

我最终使用苹果支持票;他们的回答基本上是开始时无法加载网络视图那么:

I ended up using an Apple support ticket; their answer is basically "you can't load a web view when started that way":


如果我理解正确,你期望你的应用程序明显地启动到前台,当你得到一个位置更新。
这不是它的工作原理。您的应用程序将启动到后台。如果你看到UIApplicationLaunchOptionsLocationKey,只有你的应用程序代理和位置管理器的委托类将被初始化。

If I understand correctly, you are expecting your app to visibly launch to the foreground when you get a location update. that is not how it works at all. Your app will be launched into the background. If you see the UIApplicationLaunchOptionsLocationKey, only your app delegate and the location manager’s delegate class will be initialized. No views will be initialized whatsoever.

应用程序在后台启动后,需要静默处理几秒钟的数据,然后返回系统。在此阶段无法加载网络视图或任何类型的用户界面。

Once an app is launched in the background, it is expected to process the data for a few seconds silently and then yield back to the system. It is not possible to load a web view, or any kind of UI at this stage.

唯一可能的用户互动是使用消息给用户启动应用程序。

The only user interaction that would be possible is to schedule a local notification with a message to the user to launch the app. If the user taps the notification, your app will be fully launched.

只有这样,您才能加载自己的网络视图。

Only then you can load your web view.

这篇关于如果存在UIApplicationLaunchOptionsLocationKey,则启动Cordova iOS应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆