简体   繁体   English

将JavaScript URL命令发送到UIWebView

[英]Sending JavaScript URL Commands to UIWebView

So I was looking to create a UIBarButton, via IBOutlet, and send a JavaScript code to the WebView through this button. 因此,我希望通过IBOutlet创建UIBarButton,并通过此按钮将JavaScript代码发送到WebView。 This is what I have so far but it keeps returning an error: 到目前为止,这是我所拥有的,但始终会返回错误:

ViewController.m ViewController.m

- (IBAction)emailCodePhone:(id)sender {
    NSURL *url = [NSURL URLWithString:@"javascript:location.href="mailto:someone@something.com?body="+document.getElementById("code").value;"];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webViewPhone loadRequest:requestObj];
}

ViewController.m Error ViewController.m错误 错误截图


javascript:location.href="mailto:someone@something.com?body="+document.getElementById("code").value;

^ This being the JavaScript code I want sent to the UIWebView so whatever's located in the Textarea (who's ID is code ), will be emailed to someone@something.com . ^这是我要发送到UIWebView的JavaScript代码,因此Textarea中的任何内容(ID为code的内容 )都将通过电子邮件发送至someone@something.com

Please also specify if theres an easier way to do this. 还请指定是否有更简单的方法来执行此操作。

The error is being thrown because your string is ending prematurely at the first quotation mark inside it. 因为您的字符串过早地在字符串中的第一个引号引起了,所以引发了错误。 You need to "escape" the quotations within your string with a backslash: 您需要使用反斜杠“转义”字符串中的引号:

NSURL *url = [NSURL URLWithString:@"javascript:location.href=\\"mailto:...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM