Skip to content

Instantly share code, notes, and snippets.

@micahnyc
Created December 21, 2016 16:51
Show Gist options
  • Save micahnyc/0b426bdbb3b4a95bb3792b439e2897e3 to your computer and use it in GitHub Desktop.
Save micahnyc/0b426bdbb3b4a95bb3792b439e2897e3 to your computer and use it in GitHub Desktop.
webview js xhr post
var startScript = "var http = new XMLHttpRequest();"
startScript += "var url = 'http://192.168.1.43:8080/liveControl';var params = 'action=LivePreviewCtl&control=true';http.open('POST', url, true);"
startScript += "http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');"
startScript += "http.onreadystatechange = function() {"
startScript += "http.onreadystatechange = function() {"
startScript += "if(http.readyState == 4 && http.status == 204) {"
startScript += "console.log(http.responseText);"
startScript += "} else { console.log('error with preview', http.status); }"
startScript += "http.send(params);"
liveStreamWebView.evaluateJavaScript(startScript) { (reult, error) in
print("started preview")
DispatchQueue.main.asyncAfter(deadline: .now() + 0.25, execute: {
let request = URLRequest(url: LiveRestApi.livePreviewUrl)
self.liveStreamWebView.load(request)
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment