payments snippet

 avatar
unknown
swift
2 years ago
3.0 kB
9
Indexable
    // dla UIViewControllera bądź UIWebViewControllera:

    private var bgTaskId: UIBackgroundTaskIdentifier?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // załadowanie web contentu dla podanego url
        guard let url = URL(string: paymentURL ?? "") else { return }
        let urlRequest = URLRequest(url: url)
        webView.load(urlRequest)
        
        // dodanie notyfikacji na wejście w 'background' i 'foreground'
        addDidEnterBackgroundNotification()
        addWillEnterForegroundNotification()
    }

    deinit {
        // usunięcie notyfikacji na wejście w 'background' i 'foreground' (by po wyjściu dalej nie spadały notyfikacje)
        removeWillEnterForegroundNotification()
        removeDidEnterBackgroundNotification()
        if let bgTaskId = bgTaskId, bgTaskId != .invalid {
            // zakończenie bg taska (by po wyjściu dalej się nie wykonywał)
            UIApplication.shared.endBackgroundTask(bgTaskId)
        }
    }

    private func addWillEnterForegroundNotification() {
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(appWillMoveToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
    }

    private func removeWillEnterForegroundNotification() {
        NotificationCenter.default.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
    }

    private func addDidEnterBackgroundNotification() {
        let notificationCenter = NotificationCenter.default
        notificationCenter.addObserver(self, selector: #selector(appDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
    }

    private func removeDidEnterBackgroundNotification() {
        NotificationCenter.default.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
    }
    
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if let url = navigationAction.request.url, url.description.contains("<<Twoja domena>>") {
            didFinishByUser = false // nasze custom flagi, które zaimplementowaliśmy. Czy payment wykonał się sam bądź czy był zakończony przez usera
            didFinishPayment?(false)
            return decisionHandler(.cancel)
        }
        return decisionHandler(.allow)
    }

    @objc func appWillMoveToForeground() {
        if let bgTaskId = bgTaskId, bgTaskId != .invalid {
            UIApplication.shared.endBackgroundTask(bgTaskId)
        }
    }

    @objc func appDidEnterBackground() {
        // rozpocznij background task + expiration handler na jego przerwanie
        bgTaskId = UIApplication.shared.beginBackgroundTask(expirationHandler: { [weak self] in
            guard let bgTaskId = self?.bgTaskId else { return }
            UIApplication.shared.endBackgroundTask(bgTaskId)
        })
    }
Editor is loading...