Swift-从网络(webview,safari等)获取响应



我想在我的应用程序中打开一个url并登录。之后,它会重定向到某个地方,并给我一个响应(200或500(我想得到回复

有可能吗?我看到有几个选项:webview,使用SafariServices的safariviewcontroller,甚至SFAuthenticationSession(但它不推荐使用,新的是iOS 12…(

你推荐什么?

致以最诚挚的问候!!

您可以使用WKWebView及其委托函数来归档这种类型的行为。

如果您使用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. 
}
}

最新更新