Nativescript Webview callback uri

Is it possible to send a message from an external URL to a web view in nativescript and get the values ​​from the postback? This is a uri redirected oauth2 stream where the user displays an external link to the website in their own webview and retrieves the value of the tokens from the postback URL. Any suggestion or pointer to tut or blog? All major players provide support for this, and it is very often used for oauth.

+4
source share
4 answers

You can look urlPropertyfor changes. For example.

Given that you have a view that looks like this:

<Page loaded="loaded">
     <WebView id="myWebView" src="{{ url }}" />
</Page>

WebView URL, :

var webViewModule = require('ui/web-view');

function loaded(args) {
    var page = args.object;
    var webView = page.getViewById('myWebView');
    webView.on(webViewModule.WebView.urlProperty, function (changeArgs) {
        console.dir(changeArgs); 
        // Do something with the URL here.
        // E.g. extract the token and hide the WebView.
    });
}
+5

main-page.js, , args.url

var vmModule = require("./main-view-model");
var webViewModule = require('ui/web-view');
function pageLoaded(args) {

    var page = args.object;
    page.bindingContext = vmModule.mainViewModel;
    var webView = page.getViewById('myWebView');
    debugger;
    //webView.url = 
    webView.on(webViewModule.WebView.loadFinishedEvent, function (args) {
        alert(JSON.stringify(args.url));
    });
    webView.src = vmModule.mainViewModel.url;

}
exports.pageLoaded = pageLoaded;

<Page xmlns="http://www.nativescript.org/tns.xsd" loaded="pageLoaded">
    <GridLayout>
        <WebView id="myWebView" />
    </GridLayout>
</Page>

, . , .

+7

, . .

YOUR_WEB_VIEW_OBJECT.on(webViewModule.WebView.loadFinishedEvent, function (args) {
    args.object.android.getSettings().setJavaScriptEnabled(true);

    args.object.android.evaluateJavascript('(function() { console.log("LOGS"); return "MESSAGE"; })();', new android.webkit.ValueCallback({
        onReceiveValue: function (result) {
            console.log(result);
        }
    }));
});

Android. iOS, API, {N}.

+1

IOS :

args.object.ios.stringByEvaluatingJavaScriptFromString('YOUR_JAVASCRIPT_CODE');
0

Source: https://habr.com/ru/post/1605867/


All Articles