简体   繁体   English

Swift等待UIWebView中的JavaScript页面加载

[英]Swift waiting for page load from javascript in UIWebView

I am attempting to log into a page using a UIWebView. 我正在尝试使用UIWebView登录页面。 Due to how complicated the infrastructure is, I'm loading the login page and manually submitting the form (inserting javascript). 由于基础架构的复杂程度,我正在加载登录页面并手动提交表单(插入javascript)。 Once the home page is loaded, I want to redirect to another page. 加载主页后,我想重定向到另一个页面。 The only problem is that there is nothing I can do to have that redirect wait until the home page is loaded (Because I didn't load the view I submitted a form). 唯一的问题是,我无法做任何让重定向等到加载主页的操作(因为我没有加载我提交的表单的视图)。 Is there a way I can get it to wait? 有办法让我等待吗?

My code currently 我的代码目前

webView.stringByEvaluatingJavaScriptFromString(jScript3) //submit the form

let gradeURL = NSURL(string: "https://redirectpage")!
let gradeRequest = NSMutableURLRequest(URL:gradeURL)
webView.loadRequest(gradeRequest)

Thank you! 谢谢!

I think u may want get something from the redirect page. 我认为您可能想从重定向页面获取一些信息。

First set the Delegate and then use UIwebViewDelegate funcion 首先设置代表,然后使用UIwebViewDelegate函数

func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    //do nothing except 4 the redirectURL
    if request.URL?.absoluteString.hasPrefix(redirectURL) == false  {
        return true
    }
    //detailly handle the query
    if let _query = request.URL?.query where _query.condition {...}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM