[英]How to autofill credit/debit-card details in inputfield in amazon.in in wkwebview in ios app?
我正在開發 iOS 應用程序,其中有 webview(wkwebview),將在 wkwebview 中加載網站,如 amazon.in 我有信用卡/借記卡詳細信息,如卡號、到期日期等。安全地存儲在我的 iOS 應用程序中,我想將信用卡/借記卡詳細信息填充到 amazon.in 支付頁面輸入字段中。
如果保存,iPhone 中的默認 safari 瀏覽器能夠自動填充信用卡/借記卡。
請讓我知道相同的解決方案。
這是亞馬遜支付頁面:
class ViewController: UIViewController {
let scriptSource = "document.getElementsByName('addCreditCardNumber')[0].value = '1111 1111 1111 1111'"
var webConfig: WKWebViewConfiguration {
get {
let webCfg = WKWebViewConfiguration()
let controller = WKUserContentController()
let pre = WKPreferences()
pre.javaScriptEnabled = true
webCfg.preferences = pre
let script = WKUserScript(source: scriptSource, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
controller.addUserScript(script)
webCfg.userContentController = controller
return webCfg;
}
}
lazy var webView: WKWebView = {
let webView = WKWebView(frame: .zero, configuration: self.webConfig)
webView.translatesAutoresizingMaskIntoConstraints = false
return webView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.addWKWebView()
}
private func addWKWebView(){
self.view.addSubview(webView)
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[webView]|", options: [], metrics: nil, views: ["webView": webView]))
self.view.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[webView]|", options: [], metrics: nil, views: ["webView": webView]))
webView.load(URLRequest(url: URL(string: "https://www.amazon.in")!))
webView.uiDelegate = self
webView.navigationDelegate = self
}}
您可以使用WKUserScript
為頁面添加自定義用戶腳本。 在其中您可以使用標准 JavaScript,例如document.getElementById("creditcard").defaultValue = "12345";
本教程應該讓您了解它: https://medium.com/capital-one-tech/javascript-manipulation-on-ios-using-webkit-2b1115e7e405 。 有關WKUserScript
的更多信息,請查看官方文檔( https://developer.apple.com/documentation/webkit/wkuserscript )
這是我用於 append 我在 webView 中的消息的內容:
public static func appendNewMessage(chatListWebView : UIWebView, appendTemplet : String) {
do {
let temp = "document.getElementById('message_div').innerHTML +=\"" + appendTemplet + "\""
print(temp)
chatListWebView.stringByEvaluatingJavaScript(from: "document.getElementById('message_div').innerHTML +=\"" + appendTemplet + "\"");
} catch {
// JLog.error(appendTemplet);
// ex.printStackTrace();
}
}
您可以根據您的情況更新它,對不起,我沒有時間編輯它
希望對你有幫助!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.