.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…
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.
EvaluateJavaScriptAsync("JSON.stringify(localStorage.getItem('dps'));")?