Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect back to app after successful login in a UIWebView

I am using a UIViewController with a UIWebView. In this UIWebView the user has to login and after a successful login it should redirect the user back to the app. But in my case, it stays in the UIWebView and doesn't redirect me back to the app.

I tried to use this code in the API Delegate:

func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool {
    DispatchQueue.main.async {
        if (url.scheme == "go-back"){
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            let controller = storyboard.instantiateViewController(withIdentifier: "WebView") as! ViewController
            self.window?.rootViewController!.present(controller, animated: true, completion: { () -> Void in
            })
        }
    }
    return true
}

and created a URL scheme, but it is not working.

like image 900
A.Daro Avatar asked Dec 03 '25 23:12

A.Daro


1 Answers

Step1 :

Make use of WKWebView rather than UIWebView in your ViewController.

Step2 :

Make your ViewController be the delegate of WKWebView

    webView.navigationDelegate = self
    webView.uiDelegate = self

Step3 :

Finally implement WKNavigationDelegate in your ViewController

    extension ViewController : WKNavigationDelegate{

        func webView(
            _ webView: WKWebView,
            decidePolicyFor navigationAction: WKNavigationAction,
            decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

            guard let url = navigationAction.request.url else {
                decisionHandler(.allow)
                return
            }

            //now u and ur server team can decide on what url will they redirect and what will be url string on login success
            //lets say u and ur server team decides url to be https://some_base_url/login/success

           if url.absoluteString.contains("/login/success") {
                // this means login successful
                decisionHandler(.cancel)
                _ = self.navigationController?.popViewController(animated: false)
            }
            else {
                decisionHandler(.allow)
            }
        }
}

EDIT:

As OP has issue with his code am updating answer to solve the issue

import UIKit
import WebKit

class ViewController: UIViewController, WKUIDelegate, WKNavigationDelegate{

    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://")
        let myRequest = URLRequest(url: myURL!)
        webView.load(myRequest)
    }

    func webView(
        _ webView: WKWebView,
        decidePolicyFor navigationAction: WKNavigationAction,
        decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {

        guard let url = navigationAction.request.url else {
            decisionHandler(.allow)
            return
        }


        if url.absoluteString.contains("/login/success") {
            // this means login successful
            decisionHandler(.cancel)
            _ = self.navigationController?.popViewController(animated: false)
        }
        else {
            decisionHandler(.allow)
        }
    }
}
like image 168
Sandeep Bhandari Avatar answered Dec 05 '25 19:12

Sandeep Bhandari