Swift 4 - set value to webKit using evaluateJavaScript - swift

I am trying to set a value from my project in Swift to a website using evaluateJavaScript but when I set the value he set success but he only placeholder not Really Value. How can I fix it?
See this image to understand what I mean:
#objc func handleStart() {
webView.evaluateJavaScript("document.getElementsByName('username')[0].value='\(textTagOne.text!)'", completionHandler: nil)
}
//This is HTML //

Related

How to store wkwebview textfield data in userdefaults and set again that data in webview field when needed [duplicate]

This question already has answers here:
Get text from input text field in WKWebView
(2 answers)
Closed 3 months ago.
I am trying new things in swift. I have developed a WKWebview iOS app in swift. I want to to store wkwebview textfield data in userdefaults in iOS and set again that data into webview textfield when needed.
I want to store address of the user in the userdefaulfts and set that address again in other screens in webview when needed.
To get textField data from web view, use evaluateJavaScript function of wkWebview and write simple javascript getElementsByClassName or getElementsById('textfield_id').
Sample:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.getElementsByClassName('input')[0].value") {(result, error) in
guard error == nil else {
print(error!)
return
}
print(result)
}
}
Then store the result into UserDefaults as required.
To populate value, write javascript code as below:
webview.evaluateJavaScript("document.getElementById('result').value = 'Hello Result';", completionHandler: { (res, error) -> Void in
//Here you can check if the execution was successful
})

URL keeps returning nil [duplicate]

I'm trying to create an URL variable from a string value. I don't understand why the resulting URL is nil
I have set up a new Xcode macOS project, placed a simple button on the View, created an action for that button and implemented the following code. The resulting url is nil.
I tried the same in Swift playground and there it worked...
#IBAction func buttonClicked(_ sender: Any) {
let urlAsString = "http://www.google.de/"
let url = URL(string: urlAsString)
if url != nil {
// Do work...
}
}
urlAsString is "http://www.google.de/" but
url is nil
debugger
You've found a bug in the debugger!
[This bug is slated to be fixed in Xcode 12.5.]
It's easy to reproduce it:
We have paused at a breakpoint inside the condition. So obviously url is not nil or we wouldn't be here at all.
Another way to prove this is to po url in the console (see right-bottom of this screen shot):
Nevertheless, url shows as nil both in the tooltip and in the variables list. So the debugger is just lying to you: url is not nil. Don't worry, be happy. Your code is working fine.
EDIT The bug has something to do with the Swift Foundation overlay. If you change the declaration of url to this:
let url = NSURL(string: urlAsString)
...then everything works as expected.
And see also https://stackoverflow.com/a/58156592/341994

Convert String to URL (Why is resulting variable nil)

I'm trying to create an URL variable from a string value. I don't understand why the resulting URL is nil
I have set up a new Xcode macOS project, placed a simple button on the View, created an action for that button and implemented the following code. The resulting url is nil.
I tried the same in Swift playground and there it worked...
#IBAction func buttonClicked(_ sender: Any) {
let urlAsString = "http://www.google.de/"
let url = URL(string: urlAsString)
if url != nil {
// Do work...
}
}
urlAsString is "http://www.google.de/" but
url is nil
debugger
You've found a bug in the debugger!
[This bug is slated to be fixed in Xcode 12.5.]
It's easy to reproduce it:
We have paused at a breakpoint inside the condition. So obviously url is not nil or we wouldn't be here at all.
Another way to prove this is to po url in the console (see right-bottom of this screen shot):
Nevertheless, url shows as nil both in the tooltip and in the variables list. So the debugger is just lying to you: url is not nil. Don't worry, be happy. Your code is working fine.
EDIT The bug has something to do with the Swift Foundation overlay. If you change the declaration of url to this:
let url = NSURL(string: urlAsString)
...then everything works as expected.
And see also https://stackoverflow.com/a/58156592/341994

Could not store value in safari browser localstorage using swift

I am using WebKit to store the email in the safari browser local storage. However, I am getting an error of
thread 3: Fatal error: Unexpectedly found nil while unwrapping an
Optional value
One way to store value in localstorage is using UserDefaults but this way the value is not stored in safari local storage thus i used the below way
import SafariServices
import WebKit
class SafariExtensionViewController: SFSafariExtensionViewController {
#IBOutlet weak var webView: WKWebView!
static let shared = SafariExtensionViewController()
override func viewDidLoad() {
self.preferredContentSize = NSSize(width: 300, height: 250)
message.stringValue = ""
emailMessage.stringValue = ""
passwordMessage.stringValue = ""
}
#IBAction func userLogin(_ sender: Any) {
let providedEmailAddress = email.stringValue
self.webView.evaluateJavaScript("localStorage.setItem(\"email\", \"value\")") { (result, error) in
self.webView.reload()
}
}
}
my question is, how do i store the value in safari browser localstorage from swift so i can access that from javascript as well.
You didn't connect webView outlet (that is marked as Implicitly Unwrapped Optional) from storyboard so you got this error.
You can use CoreData, UserDefaults, FileManager, even simple SQLite to store what you want.
EDIT:
A Safari app extension can read and modify your webpage content. Also it allows you to access data in your browser.
Complete the following steps to make Safari App Extension:
Build a Safari App Extension
Inject a script into a webpage
Pass message between your Safari app extension and injected script

learning swift: expressions are not allowed at the top level

I'm learning Swift. I met a problem cannot be solved.
import UIKit
func helloword(str:String) {
print(str)
}
helloword("say")
I use helloword("say") but Xcode tell me the error:
expressions are not allowed at the top level
You can't simply call this method anywhere in the file. It must be called in a control flow. I mean call it inside a function.
For example, call your function from your viewDidLoad method like so :
override func viewDidLoad() {
self.helloword("say") // here self is the View Controller itself
}