ios - 如何在后台禁用 Cordova webview?(仅限 iOS)
问题描述
我正在开发一个 iOS Cordova 应用程序,该应用程序在后台启动以处理位置事件。这工作正常,但我注意到当应用程序在后台启动时,webview 被初始化并且我的整个应用程序被渲染。我有处理后台启动的所有逻辑,所以我可以(希望)避免渲染 UI 和运行 Javascript 端。
我想我可以通过将MainViewController
创建包装在一个if
语句中来禁用 webview,但这似乎不起作用(我知道 webview 正在运行,因为它正在发送 HTTP 请求)。
这是我的AppDelegate.m
:
#import "AppDelegate.h"
#import "MainViewController.h"
#import "AppName-Swift.h"
@implementation AppDelegate {
LocationSyncManager* locationSyncManager;
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
bool isLocationLaunch = launchOptions[@"location"] == nil ? false : launchOptions[@"location"];
NSString* serverAddress = @"http://192.168.1.61:9090/api/notes/nearby";
locationSyncManager = [LocationSyncManager create: serverAddress isLocationLaunch: isLocationLaunch];
// Don't render the webview if the app is launched in the background by a location event
if(!isLocationLaunch) {
self.viewController = [[MainViewController alloc] init];
}
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
@end
解决方案
想通了问题:
isLocationLaunch
总是false
因为我检查位置启动标志错误。
此外,为了防止 Cordova 渲染/运行 javascript,我必须避免super didFinishLaunchingWithOptions
在位置启动期间被调用。
这有效:
#import "AppDelegate.h"
#import "MainViewController.h"
#import "AppName-Swift.h"
@implementation AppDelegate {
LocationSyncManager* locationSyncManager;
}
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
bool isLocationLaunch = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
NSString* serverAddress = @"http://192.168.1.61:9090/api/notes/nearby";
locationSyncManager = [LocationSyncManager create: serverAddress isLocationLaunch: isLocationLaunch];
if(!isLocationLaunch) {
self.viewController = [[MainViewController alloc] init];
return [super application:application didFinishLaunchingWithOptions:launchOptions];
}
return nil;
}
@end
推荐阅读
- validation - 相关数据验证列脚本不拒绝输入(Apps 脚本)
- python - 如何使用python解压缩多个文件
- python - Asyncio/Websocket:客户端不向服务器发送消息
- pandas - 有没有办法重新调整数据框以显示更好的时间序列数据集?
- php - 无法将时间戳存储到 MariaDB
- scala - Dataframe foreach 循环 - 提取结果的更好方法?
- reactjs - apache超集仪表板上的浮动组件/图表
- reactjs - 在基于 React 的 chrome 扩展中无法访问 Chrome 存储 API
- linux - 终端命令 ls > wc 和 ls | 有什么作用?厕所秀?
- sql - sql 在具有多个命名表的子查询中选择 max()