saltedge / saltedge-ios-swift

MIT License
10 stars 13 forks source link

Redirect #27

Closed baha-che closed 4 years ago

baha-che commented 5 years ago

Hello, I use 3.1.3 in my app IOS It's not an issue, I follow all the instruction and I follow the example but my problem is : after the webview display in the last step I can't redirect to my app, I even try to print the response in the SEWebViewDelegate but there's no response until it reached "Done" there's no response. Thank you

import UIKit import SaltEdge import WebKit import PKHUD

class SaltEdgeVC: UIViewController {

@IBOutlet weak var webView: SEWebView!
private var provider: SEProvider?
private lazy var attempt = SEAttempt(returnTo: "http://httpbin.org/")
private lazy var consent = SEConsent(scopes: ["account_details", "transactions_details"])

override func viewDidLoad() {
    super.viewDidLoad()

    let url = URL (string: "\(SaltEdgeService.instance.connectURL)")
    let requestObj = URLRequest(url: url!)
    webView.load(requestObj)
    webView.stateDelegate = self

}

@IBAction func backBtnWasPressed(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

func requestToken(connection: SEConnection? = nil, refresh: Bool = false) {
    HUD.show(.labeledProgress(title: "Requesting Token", subtitle: nil))
    if let connection = connection {
        if refresh {
            // Set javascriptCallbackType to "iframe" to receive callback with connection_id and connection_secret
            // see https://docs.saltedge.com/guides/connect
            let params = SERefreshSessionsParams(
                attempt: attempt,
                javascriptCallbackType: "iframe"
            )
            SERequestManager.shared.refreshSession(with: connection.secret, params: params) { [weak self] response in
                self?.handleConnectSessionResponse(response)
            }
        } else {
            // Set javascriptCallbackType to "iframe" to receive callback with connection_id and connection_secret
            // see https://docs.saltedge.com/guides/connect
            let params = SEReconnectSessionsParams(
                attempt: attempt,
                javascriptCallbackType: "iframe",
                consent: consent
            )
            SERequestManager.shared.reconnectSession(with: connection.secret, params: params) { [weak self] response in
                self?.handleConnectSessionResponse(response)
            }
        }
    } else {
        createSession()
    }
}

private func handleConnectSessionResponse(_ response: SEResult<SEResponse<SEConnectSessionResponse>>) {
    switch response {
    case .success(let value):
        HUD.hide(animated: true)
        if let url = URL(string: value.data.connectUrl) {
            let request = URLRequest(url: url)
            webView.load(request)
            webView.isHidden = false
        }
    case .failure(let error):
        HUD.flash(.labeledError(title: "Error", subtitle: error.localizedDescription), delay: 3.0)
    }
}

private func handleLeadSessionResponse(_ response: SEResult<SEResponse<SELeadSessionResponse>>) {
    switch response {
    case .success(let value):
        HUD.hide(animated: true)
        if let url = URL(string: value.data.redirectUrl) {
            let request = URLRequest(url: url)
            webView.load(request)
            webView.isHidden = false
        }
    case .failure(let error):
        HUD.flash(.labeledError(title: "Error", subtitle: error.localizedDescription), delay: 3.0)
    }
}

private func createSession() {
    guard let provider = provider else { return }

    if SERequestManager.shared.isPartner {
        let leadSessionParams = SELeadSessionParams(
            consent: consent,
            providerCode: provider.code,
            attempt: attempt,
            javascriptCallbackType: "iframe"
        )

        // Check if SERequestManager will call handleConnectSessionResponse
        SERequestManager.shared.createLeadSession(params: leadSessionParams) { [weak self] response in
            self?.handleLeadSessionResponse(response)
        }
    } else {
        let connectSessionsParams = SEConnectSessionsParams(
            attempt: attempt,
            providerCode: provider.code,
            javascriptCallbackType: "iframe",
            consent: consent
        )

        // Check if SERequestManager will call handleConnectSessionResponse
        SERequestManager.shared.createConnectSession(params: connectSessionsParams) { [weak self] response in
            self?.handleConnectSessionResponse(response)
        }
    }
}

} extension SaltEdgeVC: SEWebViewDelegate { func webView(_ webView: SEWebView, didReceiveCallbackWithResponse response: SEConnectResponse) { print("Response :", response) print("Response Stage:", response.stage) switch response.stage { case .success: // Connection successfully connected let bankListVC = self.storyboard?.instantiateViewController(withIdentifier: "BankListVC") self.present(bankListVC!, animated: true, completion: nil) print("All good") case .fetching: // Connection is fetching. You can safe connection secret if it is present. guard let connectionSecret = response.secret else { return }

        if var connections = UserDefaultsHelper.connections {
            if !connections.contains(connectionSecret) {
                connections.append(connectionSecret)
                UserDefaultsHelper.connections = connections
            }
        } else {
            UserDefaultsHelper.connections = [connectionSecret]
        }
    case .error:
        // Handle error
        HUD.flash(.labeledError(title: "Cannot Fetch Connection", subtitle: nil), delay: 3.0)
    }
}

func webView(_ webView: SEWebView, didReceiveCallbackWithError error: Error) {
    alert(message: error.localizedDescription, title: Spare.ValidationMessage.warning)
}

func webView(_ webView: SEWebView, didHandleRequestUrl url: URL) {
    if url.absoluteString == attempt.returnTo {
        webView.isHidden = true
    }
}

}

baller784 commented 5 years ago

@baha-che Hello. We need more context about your issue. Could you please explain, what you mean about "last step" (which stage is it)? Please provide some screenshots, logs (You can send them to our support: "support@saltedge.com" with this issue link).