Long time reader, first time asker.
I have an app that sends JSON messages when a condition occurs. I'm trying to get the webpage to refresh when it receives the JSON message.
I'm very new to web development and used this great tutorial to get the Get Post going:
https://www.youtube.com/watch?v=aTj0ZLha1zE
I used https://stackoverflow.com/users/88204/james-lawruk great answer here to refresh the webpage when the local file "data.json" is changed. How to refresh a page whenever my json data file changes
What I can't figure out is how to trigger the refresh from swift. I'm probably missing something fundamental here but i'm not sure where to look.
My swift code:
@IBAction func OnPostTapped(_ sender: Any) {
let parameters = ["username": "@you", "tweet": "HelloWorld"]
guard let url = URL(string: "http://localhost:8888/data.json") else { return }
var request = URLRequest(url:url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
guard let httpBody = try? JSONSerialization.data(withJSONObject: parameters, options: []) else {return }
request.httpBody = httpBody
let session = URLSession.shared
session.dataTask(with: request) { (data, response, error) in
if let response = response {
print (response)
}
if let data = data {
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print (json)
}catch {
print (error)
}
}
}.resume()
}
Edit
I should have clarified, the app doesn't have a UIWebView, it just sends JSON to a webpage. I'm trying to get the webpage to refresh on the user's browser.