【问题标题】:How to autofill credit/debit-card details in inputfield in amazon.in in wkwebview in ios app?如何在 ios 应用程序的 wkwebview 中的 amazon.in 的输入字段中自动填充信用卡/借记卡详细信息?
【发布时间】:2020-02-15 03:37:51
【问题描述】:

我正在开发有 webview(wkwebview) 的 iOS 应用程序,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
   }}

【问题讨论】:

  • 请附上代码、资料,有助于更好地回答
  • 已更新。我已经尝试过这段代码,但没有工作
  • 您从哪里获得“addCreditCardNumber”-Name?这里没有显示它的源代码。
  • @CennoxX 我们可以通过 google chrome 检查获得这个元素名称

标签: javascript ios swift webkit wkwebview


【解决方案1】:

您可以为带有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)

【讨论】:

  • 我尝试在亚马逊注入 document.getElementsByName('addCreditCardNumber')[0].value = '1111 1111 1111 1111' 但不起作用
  • 如果你想要一个通用的答案,像你一样问,如果你对特定页面有问题,你必须根据那个问你的问题并添加源代码。
  • 更新问题。我已经尝试了上面的代码,但没有工作
【解决方案2】:

这是我用来在 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();
    }
    }

你可以根据你的情况更新它,对不起,我没有时间编辑它

希望对你有所帮助!

【讨论】:

  • Webkit Javascript 注入在亚马逊上不起作用,如果您知道任何 javascript 或任何其他解决方案,请告诉我。
猜你喜欢
  • 2022-01-09
  • 2020-10-12
  • 2019-05-11
  • 2017-04-25
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 2011-07-25
相关资源
最近更新 更多