Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/476.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用NSString变量从PhoneGap中的Objective C编写JavaScript_Javascript_Ios_Objective C_Cordova - Fatal编程技术网

使用NSString变量从PhoneGap中的Objective C编写JavaScript

使用NSString变量从PhoneGap中的Objective C编写JavaScript,javascript,ios,objective-c,cordova,Javascript,Ios,Objective C,Cordova,在PhoneGap应用程序中,我试图从.m Objective C文件返回一行JavaScript。我可以这样做,一个变量没有问题,但另一个变量会默默地失败。NSString变量*text的声明方式似乎有些矛盾,但我对Objective C不太熟悉,无法判断代码的错误。请参阅下面的代码 - (void)umSwipe_receivedSwipe:(NSNotification *)notification { NSLog(@"umSwipe_receivedSwipe called");

在PhoneGap应用程序中,我试图从.m Objective C文件返回一行JavaScript。我可以这样做,一个变量没有问题,但另一个变量会默默地失败。NSString变量*text的声明方式似乎有些矛盾,但我对Objective C不太熟悉,无法判断代码的错误。请参阅下面的代码

- (void)umSwipe_receivedSwipe:(NSNotification *)notification {
    NSLog(@"umSwipe_receivedSwipe called");
    NSData *data = [notification object];
    NSString *text = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

    // FAIL: This fails silently
    NSString* orig = [NSString stringWithFormat:@"alert('%@')", text];
    [super writeJavascript:orig];

    // This works great
    NSString* someVar = @"B1234221012341234^LAST/FIRST^151710100000000099000000?;1234221012341234=15171010000000099?";
    NSString* concat = [NSString stringWithFormat:@"alert('%@')", someVar];
    [super writeJavascript:concat];
}
作为背景,这是为了利用iPad上的

为什么当一个变量包含相同的字符数据时,另一个变量不工作?如何将此变量合并到一个字符串中,并在iOS PhoneGap应用程序中发送给JavaScript

顺便说一句,如果答案中有正确的代码,而不仅仅是指向某个东西的链接,我更可能欣赏和理解它

谢谢


编辑:所以我发现这个错误是一个JavaScript错误。仍然不确定到底是什么问题,但是NSString变量中必须包含某种不可见的字符,这会破坏JS体验。可能是我记录时没有显示的换行符…?

这是换行符。我更改了此代码:

NSString* orig = [NSString stringWithFormat:@"alert('%@')", text];
为此:

NSString *nolinebreaks = [[text componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] componentsJoinedByString:@" "];
NSString* orig = [NSString stringWithFormat:@"alert('%@')", nolinebreaks];
…一切都很好

FWIW,为了查看所有JS console.log脚本以及记录到xcode控制台的JS错误,我在JS代码的顶部添加了以下脚本:

<script type="text/javascript">
    // Make the console debugger work properly
    console = new Object();
    console.log = function(log) {
      var iframe = document.createElement("IFRAME");
      iframe.setAttribute("src", "ios-log:#iOS#" + log);
      document.documentElement.appendChild(iframe);
      iframe.parentNode.removeChild(iframe);
      iframe = null;    
    }
    console.debug = console.log;
    console.info = console.log;
    console.warn = console.log;
    console.error = console.log;
    debug.log = console.log;

    window.onerror = function(message, url, lineNumber)
    {
        console.log("[Javascript Error]: "+message+" in "+url+" at line "+lineNumber);
    }

</script>

//使控制台调试器正常工作
console=新对象();
console.log=函数(log){
var iframe=document.createElement(“iframe”);
setAttribute(“src”,“ios日志:#ios#“+log”);
document.documentElement.appendChild(iframe);
iframe.parentNode.removeChild(iframe);
iframe=null;
}
console.debug=console.log;
console.info=console.log;
console.warn=console.log;
console.error=console.log;
debug.log=console.log;
window.onerror=函数(消息、url、行号)
{
log(“[Javascript错误]:”+message+”在“+url+”的“+lineNumber”行中);
}

NSData*data=[notification object];`?它崩溃:2013-09-17 13:00:59.254 HelloWorld[490:707]-[NSConcreteModableData getCharacters:range:]:发送到实例0x271780的无法识别的选择器2013-09-17 13:00:59.256 HelloWorld[490:707]***由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:'-[NSConcreteModableData getCharacters:range:]:未识别的选择器发送到实例0x271780'如果我输入NSLog(文本),我将获得以下信息:B123422210112341234^最后一个/第一个^151710100000000099000000?;1234221012341234=15171010000000099?…尽管使用了真实的CC数据。当然,我不会在这里发表。