如何在IOS APP中通过swift/javascript登录网站
Posted
技术标签:
【中文标题】如何在IOS APP中通过swift/javascript登录网站【英文标题】:How do I log into a website in an IOS APP through swift/javascript 【发布时间】:2017-09-16 05:46:56 【问题描述】:我正在尝试创建一个用户通过我的 UI 登录的应用程序。我有要从中提取数据的 textLabel,我想获取该数据并使用它在线登录网站并在登录后获取数据。我现在拥有的是一个 WKWebView,它在加载的大部分时间都是不可见的我要登录的网站,然后填写登录表格并单击按钮 - 全部通过 evaluatejavascript。我现在唯一的问题是我正在尝试想出一些能够检查用户是否错误登录的东西。我要做的是等到 javascript 执行,然后检查 webView 并查看 url 是否已更改为登录页面。但是,这只有在用户没有输入正确密码失败的情况下才有效。如果他们得到一次错误的密码,那么之后的所有条目都表示密码不正确。我使用观察值来检查页面是否已完成加载,但我需要帮助来实现这一点。
@IBAction func loginBtnPressed(_ sender: Any)
if(!validate(usernameText) || !validate(passwordText))
self.validationLabel.text = "One or more fields are empty."
UIView.animate(withDuration: 0.25, animations:
self.validationLabel.isHidden = false
)
return;
let oldUrl = webView.url?.absoluteString;
self.validationLabel.isHidden = true
webView.evaluateJavaScript("document.getElementById('fieldAccount').value = \(usernameText!.text!)", completionHandler: (result,err) in print(result ?? "No Result"); print (err ?? "No Error") )
webView.evaluateJavaScript("document.getElementById('fieldPassword').value = '\(passwordText!.text!)'", completionHandler: (result,err) in print(result ?? "No Result"); print (err ?? "No Error") )
webView.evaluateJavaScript("document.getElementById('btn-enter').click()", completionHandler: (result,err) in
print(result ?? "No Result"); print (err ?? "No Error") )
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(6), execute:
// Put your code which should be executed with a delay here
if(self.webView.url?.absoluteString.contains("termGrades"))!
print("We're in bois: ")
self.view = self.webView
else
print("Did not get to grades page..")
self.validationLabel.text = "Invalid Username/Password"
UIView.animate(withDuration: 0.25, animations:
self.validationLabel.isHidden = false
)
let portalURL = URL(string: "*website*")
let request = URLRequest(url: portalURL!)
self.webView.load(request)
return;
)
我是一个新的 ios 开发人员,但我不想放弃这一点,如果您有任何帮助,或者关于等待 javascript 单击按钮和页面完全加载的更好方法的建议,请分享
编辑: 我想我找到了解决方案,但由于某种原因,现在必须单击登录按钮两次才能处理任何内容...
@IBAction func loginBtnPressed(_ sender: Any)
if(!validate(usernameText) || !validate(passwordText))
self.validationLabel.text = "One or more fields are empty."
UIView.animate(withDuration: 0.25, animations:
self.validationLabel.isHidden = false
)
return;
self.validationLabel.isHidden = true
webView.evaluateJavaScript("document.getElementById('fieldAccount').value = \(usernameText!.text!.description)", completionHandler: (result,err) in print(result ?? "No Result"); print (err ?? "No Error") )
webView.evaluateJavaScript("document.getElementById('fieldPassword').value = '\(passwordText!.text!)'", completionHandler: (result,err) in print(result ?? "No Result"); print (err ?? "No Error") )
webView.evaluateJavaScript("document.getElementById('btn-enter').click()", completionHandler: (result,err) in
print(result ?? "No Result"); print (err ?? "No Error") )
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?)
if keyPath == "loading"
if(!webView.isLoading)
print("Finished navigating to url \(webView.url!)");
if(webView.url! == URL(string: "**"))
statusLabel.text = "Done."
else if(webView.url! == URL(string: "**"))
self.validationLabel.text = "Invalid Username/Password"
UIView.animate(withDuration: 0.25, animations:
self.validationLabel.isHidden = false
)
else if(webView.url! == URL(string: "**"))
self.view = webView
【问题讨论】:
【参考方案1】:func webView(webView: WKWebView!, didFinishNavigation navigation: WKNavigation!)
print("Finished navigating to url \(webView.url)");
确保将 WKWebView 的委托设置为您的视图控制器
【讨论】:
以上是关于如何在IOS APP中通过swift/javascript登录网站的主要内容,如果未能解决你的问题,请参考以下文章
在 iOS 中通过 GCDAsyncsocket 读取 xml 文件
如何在 iOS 中通过 UIActivityViewController 导出 PHAsset 视频