1

Hello Guys I'll hope that you can help me.

I have a Wkwebview which opens a website "example.com". This website includes a Javascript file with many functions (the Javascript is not located in the local store of the app, it's recommanded by a link). In this website is also a button to locate you. If I press this button the Javascript function "getLocation()" will be called, see below. This function shows you the long- and latitude!

var x = document.getElementById("location");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } 
}

function showPosition(position) {
    x.innerHTML = "Latitude: " + position.coords.latitude + 
    "<br>Longitude: " + position.coords.longitude;
}

I'm able to show the navigator.geolocation.getCurrentPosition with longitude and latitude in the native app. The Problem is that the WKwebview asked me always for permission to locate me (Note the app has already access to locate me it's always the WebView who asked)

ask for permission

My question is how can i transfer the data from the App to the Javascript without a permissionrequest. I read something about WKWebViewConfiguration or about evaluateJavaScript(_:completionHandler:) or about NSBundle and NSURLRequest but what is the right way or is there a better way? please help me I do not know how to continue

1 Answers1

0

By Using evaluateJavaScript() only you can run any JavaScript in a WKWebView and get the result in Swift. This can be any JavaScript, which means you can dig right into a page and pull out any information that you need.

Here's an example:

webView.evaluateJavaScript("document.getElementById('someElement').innerText") { (result, error) in
    if error != nil {
        print(result)
    }
}

Note : Kindly pass correct javascript to get the values or else you may return nil

  • By Using evaluateJavaScript() only you can run any JavaScript in a WKWebView and get the result in Swift. This is problem, because in a second step the javascript include these data in a database. But I'll try it. – user8987919 Dec 29 '17 at 13:47