首页 > 解决方案 > WKWebView 中的 Apple Pay JS

问题描述

我正在研究是否可以在 WKWebView 中嵌入 Apple Pay JS/Web Payments API。我们有一个嵌入在 WKWebView 中的 Web 应用程序,我们希望通过 JavaScript 实现 Apple Pay,而无需桥接。

我整理了一个非常基本的ViewController.swift示例如下,使用Apple Pay on the Web Demo

import UIKit
import WebKit


class ViewController: UIViewController, WKUIDelegate {

    var webView: WKWebView!

    override func loadView() {
        let webConfiguration = WKWebViewConfiguration()
        webView = WKWebView(frame: .zero, configuration: webConfiguration)
        webView.uiDelegate = self
        view = webView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        let myURL = URL(string:"https://applepaydemo.apple.com")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }
}

呈现了 Apple Pay 按钮,但也显示了不支持设备的错误消息(见下文),并且无法单击该按钮。

在此处输入图像描述

我认为这意味着在这种情况下没有支持。我再次尝试使用SFSafariViewController并显示按钮并在单击时启动 Apple Pay。简而言之 - 这一切都完美无缺。

import UIKit
import SafariServices

class ViewController: UIViewController, SFSafariViewControllerDelegate {
    override func viewDidAppear(_ animated: Bool) {
        let safariVC = SFSafariViewController(url: URL(string:"https://applepaydemo.apple.com")!)
        self.present(safariVC, animated: false, completion: nil)
    }
}

我想知道的是我如何为 WkWebView 实现这个是否存在问题,或者是否存在错误。看起来按钮出现或按钮不起作用都是错误。显然在一个层面上它认为 WkWebView 是兼容的,但在另一个层面上却不是。这似乎是一个需要报告的错误,但我对 Swift 很陌生,所以它可能是一个实现问题。

谢谢!

标签: iosswiftwkwebviewapplepaysfsafariviewcontroller

解决方案


不幸的是,您只能将 ApplePay 与 SFSafariViewController 一起使用;这意味着即使出现 ApplePay 按钮,交易流程也不会让您继续,因为 WkWebview 不支持 ApplePay。


推荐阅读