1

.Net 8 Maui App, running/debugging on Mac OS.

What I am trying to do:

I am trying to get data from a web view’s local storage. I am expecting serialized json to be returned by localStorage.getItem(‘dps’) but instead when trying to get the string data after performing a ToString() on the NSObject what it gives me is \0\0….

Steps to reproduce:

xaml markup

<WebView x:Name="webview" HeightRequest="300" WidthRequest="500" Source="https://app.hosted.onprem">
</WebView>  

button to initiate retrieval of data from localStorage

<Button x:Name="btnLoggedIn" WidthRequest="150" Clicked="btnLoggedIn_Clicked"  Text="Validate" />

code behind

private async void btnLoggedIn_Clicked(object sender, EventArgs e)
{
    try
    {
        /*
        //Tried to use generic approach of:
        //webview.EvaluateJavaScriptAsync("localStorage.getItem('dps');");
        //which always returned null.
        */

        var macosResults = await (webview.Handler.PlatformView as WebKit.WKWebView).EvaluateJavaScriptAsync("localStorage.getItem('dps');");

        var stringResults = macosResults.ToString();
    }
    catch (Exception ex)
    {
        
    }

}

If I inspect the NSObject in the debugger I see the value I need and can copy it from there but if I try assigning the value programmatically, it is garbage, \0\0…

results

What am I missing/doing wrong?

I am ensuring that I am properly waiting for the call to complete and have verified the data exists in local storage and can see it and access it in the debugger.

1
  • Have you tried converting the object to string before sending it, e.g. EvaluateJavaScriptAsync("JSON.stringify(localStorage.getItem('dps'));") ? Commented Jun 11 at 6:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.