Зареждам уебсайт в UIWebView
, Този уебсайт включва бутон и текстово поле. В самото приложение запазих низ. Сега бих искал да вмъкна този низ в textField на уебсайта (който е бил зареден в UIWebView
). Някой знае ли как мога да направя това в Swift? Благодаря много за отговорите.
Отговори:
0 за отговор № 1Можеш да използваш stringByEvaluatingJavaScriptFromString
.
Ако приемем, че уебсайтът ви е такъв:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Hello world</title>
</head>
<body>
<label for="name">Name:</label>
<input type="text" name="name" value="" id="name">
</body>
</html>
Искате да зададете стойността в полето за име на някаква стойност, въведена от потребителя в приложението ви:
override func viewDidLoad() {
webView.delegate = self
}
func webViewDidFinishLoad(webView: UIWebView) {
// This is the value you want to pass into the website, it can be something that
// the user entered into your app. Here, I use a hard coded value for convenience
let name = "John Smith"
// Make sure the element"s id in getElementById matches your HTML code
var js = "var textfield = document.getElementById("name");n"
js += "textfield.value = "" + name + "";"
webView.stringByEvaluatingJavaScriptFromString(js)
}