如果存在 UIApplicationLaunchOptionsLocationKey,则启动 Cordova iOS 应用程序

Starting Cordova iOS app if UIApplicationLaunchOptionsLocationKey is present

我有一个记录用户行程的 Cordova 应用程序。它在后台运行良好,如果它由于某种原因被杀死(用户将其滑开,phone 重新启动等),我希望它自动重新启动。

我正在监控一个区域,当用户 enters/exits 离开该区域时应用会部分重​​启。 'Partially' 此处表示该应用程序仅在后台运行 - Cordova 加载我的应用程序的插件,但它从不加载 WebView。

对于此部分重启,我正在使用插件的 pluginInitialize 方法中的 UIApplicationLaunchOptionsLocationKey 侦听 UIApplicationDidFinishLaunchingNotification,并立即启动位置服务以保持 运行。

为什么 WebView 没有加载?当 iOS 启动时,我需要做些什么来触发下一阶段的初始化吗?

编辑 2016/3/7

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

        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:

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

我看到这个加载在手动情况下完成,但在自动启动情况下没有,日志行:

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

为什么这个加载在自动启动时会失败?

我最终使用了 Apple 支持票;他们的回答基本上是 "you can't load a web view when started that way":

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.