gpt4 book ai didi

ios - 如何将文本从 SafariViewController 快速获取到我们的应用程序?

转载 作者:行者123 更新时间:2023-11-29 05:15:54 25 4
gpt4 key购买 nike

在我的应用程序中,我们使用 Segpay 进行支付,没有合适的 SDK 可以集成,然后我们从支持的 URL 中创建 URL 并在 SafariViewController 中打开它。

当用户成功完成付款时,我们需要将用户带入主页,如何从 SafariViewController 知道状态为成功。

我在 SafariViewController 上收到了来自后端的响应,如下所示,在 webview 页面上:enter image description here

最佳答案

您可以使用 WKWebview 代替 safari,如下所示。

创建 WKWebView 的属性

private var webView: WKWebView!

按照 viewDidLoad 方法初始化 webview...

let jScript = "var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"
let wkUScript = WKUserScript(source: jScript, injectionTime: .atDocumentStart, forMainFrameOnly: true)
let wkUController = WKUserContentController()
wkUController.addUserScript(wkUScript)

let webConfiguration = WKWebViewConfiguration()
webConfiguration.userContentController = wkUController

webView = WKWebView(frame: view.bounds, configuration: webConfiguration)
webView.navigationDelegate = self
view.addSubview(webView)
let url = URL(string: "https://...") //set url to load in webview
let req = URLRequest(url: url)
webView.load(req)

实现WKWebView的以下委托(delegate)方法。

func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.getElementById('response_success').textContent", completionHandler: { (html, error) in

if let content = html as? String {
print(content)

if content.lowercased().contains("ok") { //here you can place the status coming from server for comparison

//handle success response
}
}
})
webView.evaluateJavaScript("document.getElementById('response_fail').textContent", completionHandler: { (html, error) in

if let content = html as? String {
print(content)

if content.lowercased().contains("fail") { //here you can place the status coming from server for comparison

//handle fail response
}
}
})
}

Note: You need to ask your backend team to post data in webview using tag with id response_success, if response is success & response_fail, if response is failed.(You can change it as per your requirements)

关于ios - 如何将文本从 SafariViewController 快速获取到我们的应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59189771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com