swift - stringByEvaluatingJavaScriptFromString

标签 swift uiwebview

我编写应用程序,我需要加载相同的 HTML 页面。比需要采取的元素。

getElementById(\"orderData\")

let webView = UIWebView()       
let localfilePath = NSBundle.mainBundle().URLForResource("PrintPage", withExtension: "html") 

let myRequest = NSURLRequest(URL: localfilePath!)    
webView.loadRequest(myRequest)  

print(webView.stringByEvaluatingJavaScriptFromString("document"))

但只要打印文档就给我可选“”。 getElementById(\"orderData\") 也写可选""

附注我可以看到这个页面,加载没有问题。

------更新 P.S.S Deal not in OptionValue =) 当我尝试使用 JavaScript 时处理空字符串

最佳答案

在 Swift 中,如果你打印一个 Optional 值,那么它会使用 Optional"" 打印,如果你不想使用Optional 打印,那么你可以使用以下代码。

if let docString = webView.stringByEvaluatingJavaScriptFromString("document") {
     print(docString)
}

关于swift - stringByEvaluatingJavaScriptFromString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36865552/

相关文章:

ios - RealmSwift : Implementing one time login with MongoDB Realm in swift

swift - 将我的代码变成更好的类似 Swift 的 FizzBu​​zz 响应答案

swift - UISliders 和设置值

iphone - UIWebView加载本地内容延迟

swift - 为什么具有协议(protocol)一致性的扩展不能具有特定的访问级别?

ios - 错误 : generic parameter 'T' could not be inferred in swift

ios - 收到推送通知时如何重定向到 Webview 中的特定页面

html - Swift:无法在 webView 中加载 HTML

ios - UITableViewCell 内的 UIWebView,检测到链接但不可点击

ios - Objective-C 发送按键事件到 webview