我已经创建了一个 native iOS 应用程序 (Xcode 5.1),我想通过 btn、cordova(Cordova 2.9.0) web View (否则为 CDVViewController)打开。我已经成功了, WebView 有效,它向我显示了网页,但是当我嵌入 cordova.js(在网页内)时, CDV命令队列.m
- (void)fetchCommandsFromJs
{
// Grab all the queued commands from the JS side.
NSString* queuedCommandsJSON = [_viewController.webView stringByEvaluatingJavaScriptFromString:
@"cordova.require('cordova/exec').nativeFetchMessages()"];
NSLog(@"---- %@",queuedCommandsJSON);
[self enqueCommandBatch:queuedCommandsJSON];
if ([queuedCommandsJSON length] > 0) {
CDV_EXEC_LOG(@"Exec: Retrieved new exec messages by request.");
}
}
调用上面的函数并执行'cordova.require('cordova/exec').nativeFetchMessages()', 这个函数返回
[["Device748313476","Device","getDeviceInfo",[]],["NetworkStatus748313477","NetworkStatus","getConnectionInfo",[]]]
然后它将这个值传递给
- (void)executePending
{
// Make us re-entrant-safe.
if (_currentlyExecuting) {
return;
}
@try {
_currentlyExecuting = YES;
for (NSUInteger i = 0; i < [_queue count]; ++i) {
// Parse the returned JSON array.
NSLog(@"%@",[_queue objectAtIndex:i]);
**NSArray* commandBatch = [[_queue objectAtIndex:i] JSONObject];**
// Iterate over and execute all of the commands.
for (NSArray* jsonEntry in commandBatch) {
CDVInvokedUrlCommand* command = [CDVInvokedUrlCommand commandFromJson:jsonEntry];
CDV_EXEC_LOG(@"Exec(%@): Calling %@.%@", command.callbackId, command.className, command.methodName);
if (![self execute:command]) {
#ifdef DEBUG
NSString* commandJson = [jsonEntry JSONString];
static NSUInteger maxLogLength = 1024;
NSString* commandString = ([commandJson length] > maxLogLength) ?
[NSString stringWithFormat:@"%@[...]", [commandJson substringToIndex:maxLogLength]] :
commandJson;
DLog(@"FAILED pluginJSON = %@", commandString);
#endif
}
}
}
[_queue removeAllObjects];
} @finally
{
_currentlyExecuting = NO;
}
}
我的应用程序崩溃是因为在这条线上
NSArray* commandBatch = [[_queue objectAtIndex:i] JSONObject];
无法将值识别为 json 对象,它给了我这个错误消息
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString JSONObject]: unrecognized selector sent to instance
非常感谢。
最佳答案
最近在新的 Xcode 中构建旧的 cordova 应用程序时遇到了同样的情况。
你应该检查Other linker flags
在您的目标设置中:
对于 debug
构建配置你可以使用-ObjC
旗帜。 ( What does the -ObjC linker flag do? , Why do I get a runtime exception of "selector not recognized" when linking against an Objective-C static library that contains categories? )
如果阅读之前的链接后,您仍想在 release
中使用此标志— 就去做吧。
否则,您应该添加 -force_load ${BUILT_PRODUCTS_DIR}/libCordova.a
到发布链接器标志。
为了检查/编辑事件构建配置,请转到 Product
> Scheme
> Edit scheme
(Cmd <
)。
关于javascript - iOS 原生应用,与 Cordova 2.9.0 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22806269/