Swift-从网络获取响应(网络视图,野生动物园等)

时间:2018-08-01 08:31:19

标签: swift ios9

我想在我的应用程序中打开一个URL并进行登录。之后,它将重定向到某个地方,并给我一个响应(200或500)。 我想得到这个答复。

有可能吗?我已经看到有几种选择:webview,使用SafariServices的safariviewcontroller甚至是SFAuthenticationSession(但已弃用,新的​​是iOS 12 ...)

您推荐什么?

最亲切的问候!

2 个答案:

答案 0 :(得分:0)

您可以使用WKWebView及其委托函数来存档这种行为。

答案 1 :(得分:0)

如果您使用的是 UIWebview ,请使用JavaScript技术在该Web视图中加载数据。像这样

    public func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {

    if let requestUrl = request.url, requestUrl.absoluteString.hasPrefix("check with your url prefix") {
        let absoluteStr = requestUrl.absoluteString.replacingOccurrences(of: "use to make url", with: "")

        if let absoluteUrl = URL(string: absoluteStr) {
            // do what you want with your url now.
        }
    }
}

UIWebView的完成和错误委托

    public func webViewDidFinishLoad(_ webView: UIWebView) {

    if !webView.isLoading {
       //webview loading complete
    }
}

public func webView(_ webView: UIWebView, didFailLoadWithError error: Error)
{
    // Call failure callback except for the error of WebKitErrorDomain
    if ((error as NSError).domain != webKitErrorDomain) {
       // delegate error where do you want. 
    }
}