javascript - iOS 原生应用,与 Cordova 2.9.0 集成

标签 javascript ios cordova uiwebview json

我已经创建了一个 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在您的目标设置中:

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/

相关文章:

javascript - 如何将日历开始日期布局从星期日更改为星期一?

ios - Swift ios 从 pickerView 获取选定值

ios - 如果 UIView,如何提高屏幕截图的质量

ios - 如何在 Swift 中缩放卡片时翻转卡片

ios - 使用 Visual Studio Tools for Apache Cordova 创建 xcarchive 而不是 ipa?

plugins - Phonegap 添加插件失败 (errno 34) */plugin.xml

php - 来自 PHP MySQL 结果的 jSon 对象

javascript - 在 javascript 中将用户输入字符串分配为新变量名

javascript - 数据嵌套时 AngularJs 表排序

javascript - 单击按钮时的 Highcharts 加载效果