Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.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
Javascript UIWebview中的NSString_Javascript_Iphone_Objective C_Uiwebview_Uiwebviewdelegate - Fatal编程技术网

Javascript UIWebview中的NSString

Javascript UIWebview中的NSString,javascript,iphone,objective-c,uiwebview,uiwebviewdelegate,Javascript,Iphone,Objective C,Uiwebview,Uiwebviewdelegate,我的项目中有一个NSString和一个webView(Objective-C for iPhone),我在webView中调用了index.html,并在其中插入了我的脚本(javascript) 如何在脚本和viceversa中将NSString作为var传递 这是一个字符串,但我不太理解。将字符串发送到web视图: [webView stringByEvaluatingJavaScriptFromString:@"YOUR_JS_CODE_GOES_HERE"]; 将字符串从web视图发

我的项目中有一个
NSString
和一个webView(Objective-C for iPhone),我在webView中调用了
index.html
,并在其中插入了我的脚本(javascript)

如何在脚本和viceversa中将NSString作为var传递


这是一个字符串,但我不太理解。

将字符串发送到web视图:

[webView stringByEvaluatingJavaScriptFromString:@"YOUR_JS_CODE_GOES_HERE"];

将字符串从web视图发送到Obj-C:

声明实现UIWebViewDelegate协议(在.h文件中):

在Objective-C(在.m文件中)中也有:


将NSString传递到UIWebView(用作javascript字符串)时,需要确保转义换行符以及单引号/双引号:

NSString *html = @"<div id='my-div'>Hello there</div>";

html = [html stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
html = [html stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
html = [html stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
html = [html stringByReplacingOccurrencesOfString:@"\r" withString:@""];

NSString *javaScript = [NSString stringWithFormat:@"injectSomeHtml('%@');", html];
[_webView stringByEvaluatingJavaScriptFromString:javaScript];
NSString*html=@“你好”;
html=[html stringByReplacingOccurrencesOfString:@“\'”和字符串:@“\\\'”];
html=[html StringByReplacingOfString:@“\”和字符串:@“\\\”];
html=[html StringByReplacingOfString:@“\n”带字符串:@“\\n”];
html=[html stringByReplacingOccurrencesOfString:@“\r”带字符串:@”“;
NSString*javaScript=[NSString stringWithFormat:@“injectSomeHtml('%@');”,html];
[\u webView stringByEvaluatingJavaScriptFromString:javaScript];

@Michael Kessler很好地描述了反向过程

我已将UIWebView和UIWebViewDelegate添加到标记中(而不是xcode和html),反之亦然?:-)
// right after creating the web view
webView.delegate = self;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSString *url = [[request URL] absoluteString];

    static NSString *urlPrefix = @"myApp://";

    if ([url hasPrefix:urlPrefix]) {
        NSString *paramsString = [url substringFromIndex:[urlPrefix length]];
        NSArray *paramsArray = [paramsString componentsSeparatedByString:@"&"];
        int paramsAmount = [paramsArray count];

        for (int i = 0; i < paramsAmount; i++) {
            NSArray *keyValuePair = [[paramsArray objectAtIndex:i] componentsSeparatedByString:@"="];
            NSString *key = [keyValuePair objectAtIndex:0];
            NSString *value = nil;
            if ([keyValuePair count] > 1) {
                value = [keyValuePair objectAtIndex:1];
            }

            if (key && [key length] > 0) {
                if (value && [value length] > 0) {
                    if ([key isEqualToString:@"param"]) {
                        // Use the index...
                    }
                }
            }
        }

        return NO;
    }
    else {
        return YES;
    }
}
location.href = 'myApp://param=10';
NSString *html = @"<div id='my-div'>Hello there</div>";

html = [html stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"];
html = [html stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];
html = [html stringByReplacingOccurrencesOfString:@"\n" withString:@"\\n"];
html = [html stringByReplacingOccurrencesOfString:@"\r" withString:@""];

NSString *javaScript = [NSString stringWithFormat:@"injectSomeHtml('%@');", html];
[_webView stringByEvaluatingJavaScriptFromString:javaScript];