1

I am currently loading web views using HTML file. And web functions are handled in js file. This is not a problem.

But my problem is the backButton. I check if there is a page to go back to. So we used document.referrer to show webviews using existing servers.

But it doesn't work because it's used inside iOS while creating the iOS hybridApp.

So I checked if the history.back() worked. It works. There's a page to go back to.

There are pages that need to be returned, but document.referrer not work.

Move from main page to detail page in html include js file

location.href = './Detail.html'

Back button function in the header of Common.js file

    alert(document.referrer) // return empty
    if (document.referrer) {
      history.back();
    } else {
      location.href = "./Main.html"; // only can do
    }

The two html files(Main,Detail) use a common header. The function is in the Common.js file. And I'm using Swift5

Main.html

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover, shrink-to-fit=no">
<meta name="referrer" content="always">

Detail.html

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover, shrink-to-fit=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="referrer" content="always">

How can we solve this problem?

Thanks in advance.

hong developer
  • 13,291
  • 4
  • 38
  • 68

2 Answers2

1

I found another solution. This is from Navigating in WKWebView. This allows you to function like document.referrer.

However, since this is a function of WKWebView, communication between js and ios is required.

  1. Modify the function of the existing js.

Back button function in the header of Common.js file

var data = { 
           type : "backCheck",
           callback : "backCheckCallback"
       }
webkit.messageHandlers.sendMessageToIos.postMessage(data);
  1. receive Message in Ios
@available(iOS 8.0, *)
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
    if message.name == "sendMessageToIos" {
        let getMessage = message.body as! NSDictionary
        guard getMessage["type"] != nil else {
           return
        }
   let type : String = getMessage["type"] as! String
   let callbackName : String = getMessage["callback"] as! String
   if type == "backCheck" {
       let backCheck = WKWebView.canGoBack
       self.WKWebView.evaluateJavaScript("\(callbackName)(\(backCheck))", completionHandler: { (any, err) -> Void in
                            print(any ?? "no any")
                            print(err ?? "no Error")
                        })
        }
  1. Configure callback function in js file
function backCheckCallback(backCheck){
  if(backCheck) {
    history.back();
  } else {
    location.href = "./Main.html";
  }
}
hong developer
  • 13,291
  • 4
  • 38
  • 68
0

Now my problem has been solved, but it's not a fundamental solution. In my case, I solved the problem by attaching parameters for push when I received the push message and moved the page.

Swift

webUrl =  "Web/Detail?pram=pram" // current Url
webUrl = "\(webUrl!)&push=Y"
let splitData = webUrl.split(separator: "?")
var myReq : URLRequest!
let mainUrl : String = String(splitData[0])
let mainParameter : String = String(splitData[1])
let url1 = Bundle.main.url(forResource: mainUrl, withExtension: "html")!
let url2 = URL(string: "?" + mainParameter, relativeTo: url1)!
myReq = URLRequest(url: url2)
WKWebView.load(myReq)

JS

if (getParameter("push") != "Y")) {
  history.back();
} else {
  location.href = "./Main.html"; // only can do
}

This solves the problem, but I think there will be a better. Please write me a better solution in the answer.

hong developer
  • 13,291
  • 4
  • 38
  • 68