3

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.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
A.Daro
  • 31
  • 1
  • 2
  • Is it going inside this code? – rishi Sep 18 '17 at 12:14
  • As far as I know, if you open outside app (let's say Safari) then it will return to your app if your app is registered under given url scheme and website runs that scheme. But if you have ViewController and content inside your app, you can just pop that controller and you will "return to app". – Adamsor Sep 18 '17 at 12:27
  • If you used UIWebview please use UIWebview delegate – IOS Singh Sep 18 '17 at 12:33
  • Check it out here, they have written a nice tutorial about this topic: https://blog.branch.io/how-to-setup-universal-links-to-deep-link-on-apple-ios/. Also have here nice trouble shooting checklist: https://stackoverflow.com/questions/32751225/ios9-universal-links-does-not-work – Bence Pattogato Sep 18 '17 at 12:33

2 Answers2

5

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)
        }
    }
}
Sandeep Bhandari
  • 19,999
  • 5
  • 45
  • 78
  • In which ViewController I have to add: Make your ViewController be the delegate of WKWebView webView.navigationDelegate = self webView.uiDelegate = self – A.Daro Sep 18 '17 at 13:15
  • View controller which has the web view now. Replace the webview with WKWebview and set the delegate – Sandeep Bhandari Sep 18 '17 at 13:21
  • after I have added Step 2: [webView.navigationDelegate = self webView.uiDelegate = self] I got the error message, that says: "Member self is a fuction; did you mean to call it? .. how can i replace it ? – A.Daro Sep 18 '17 at 13:26
  • Can I please post the updated code of your view controller – Sandeep Bhandari Sep 18 '17 at 13:41
0
//

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)
}

}

i got the error by using this line:

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

that says that use of unresolved identifier "self"

A.Daro
  • 31
  • 1
  • 2
  • This is not a answer. Please add it as question – Sandeep Bhandari Sep 18 '17 at 14:46
  • There is no issue in code there is an additional '}' If you are using xCode you should have seen it very clearly – Sandeep Bhandari Sep 18 '17 at 14:48
  • Please consider accepting the answer if it helped. BTW u are using string /login/success this is only for example in real life you might have different url string – Sandeep Bhandari Sep 18 '17 at 14:50
  • oh sorry, i will try to change it from answer to question.. I know that /login/success is an example .. but i got an error in the last line – A.Daro Sep 18 '17 at 15:05
  • @a-daro : Thats because your code has an additional '}' I have removed it use the code I posted above – Sandeep Bhandari Sep 18 '17 at 15:06
  • i have tried it.. it works .. but after clicking login it stays in the webView instead to redirect me back to the app – A.Daro Sep 18 '17 at 15:18
  • @a-daro : On login success what is the url server is redirecting to ? Have you replaced if url.absoluteString.contains("/login/success") { with actual url path ? Have u put a break point and check if control enters if block in WKWebView – Sandeep Bhandari Sep 18 '17 at 15:19