Xamarin forms: IOS won't load webview - xamarin

I have a webview on a page that opens with data from a previous page. It works on Android and with IOS the screen remains white. I have already searched a bit and found out that it can be due to the events. I use the events : OnAppearing, Navigating and Navigated.
<WebView x:Name="webview"
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Navigating="webOnNavigating"
Navigated="webOnEndNavigating"
IsVisible="False"/>
public WebviewPage(String link, String username, String password)
{
InitializeComponent();
colorLogOutButton(link);
linkForApi = link;
compLink = link + "/Login1.aspx" + "?ID=" + GUID + "&Username=" + username + "&Password=" + password;
webview.Source = compLink;
}

You have to add an exception to the Apple Transport Security (ATS) in order to display HTTP content in your application. You can do that modifying your .plist and adding the NSAllowsArbitraryLoadsInWebContent key. Something like that:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoadsInWebContent</key>
<true/>
</dict>

Related

Webview Session Storage lost when navigate between Xamarin pages

I have a Xamarin.Forms App with Two Pages, one with a WebView and other with just text.
Session Storage lost data when I navigate between pages.
//APage.xaml
<WebView x:Name="webView" WidthRequest="1000" HeightRequest="1000" Navigated="webView_Navigated">
<WebView.Source>
<UrlWebViewSource />
</WebView.Source>
</WebView>
//BPage.xaml
<Span Text="Hi B Page"/>
//APage.xaml.cs
...
public BPage()
{
InitializeComponent();
(webView.Source as UrlWebViewSource).Url = "mytestsite.com";
}
private async void webView_Navigated(object sender, WebNavigatedEventArgs e)
{
var webView = sender as WebView;
var token = await webView.EvaluateJavaScriptAsync("window.sessionStorage.getItem('token')");
System.Console.WriteLine(token); //Every time I Go to BPage and back, it is null.
}
My website return a "Token not found in sessionstorage" error.
//Every time I Go to B Page and back, it is null.
When you navigate from B to A, page B will be released and every time you navigate from A to B, you are navigating to a new PageB(not the same PageB every time).
Each webView you use is under different Page B and that's why you get null.
Here is the document about Navigation.
You can use Xamarin.Essentials: Preferences to save token in apps.
Save:
Preferences.Set("my_key", "my_value");
Read:
var myValue = Preferences.Get("my_key", "default_value");

xamarin.auth stuck on success when isUsingNativeUI true

Im using xamarin.auth for facebook, google and twitter login. Its not closing browser after get success, if i close browser manually IsAuthenticated returning false. If i use isUsingNativeUI false it works fine but very ugly new page with uncontrolled color and title. Is where a way to change color and title on there? I can use these 2 ways too but need to fix.
var authenticator = new OAuth2Authenticator(
clientId,
Constants.FacebookScope,
new Uri(Constants.FacebookAuthorizeUrl),
new Uri(Constants.FacebookAccessTokenUrl),
null, isUsingNativeUI: true);
authenticator.Completed += OnAuthCompleted;
authenticator.Error += OnAuthError;
AuthenticationState.Authenticator = authenticator;
var presenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
presenter.Login(authenticator);
Ugly navigation bar and title
I just solved my issue with correct redirect url. 111111 means your client Id, add fb on start, ://authorize to the end of it.
public static string FacebookiOSRedirectUrl = "fb1111111111111://authorize";
public static string FacebookAndroidRedirectUrl = "fb1111111111111://authorize";
On ios it say unknown or wrong redirect url, you can fix it with add this on your plist.
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>Xamarin.Auth facebook PAuth</string>
<key>CFBundleURLSchemes</key>
<array>
<string>fb111111111111</string>
</array>
<key>CFBundleTypeRole</key>
<string>Viewer</string>
</dict>
</array>

How can I modify a WebView in Xamarin.Forms to open links in a browser on the device?

By default links (<href..>) Xamarin.Forms WebViews open inside the WebView. Especially in iOS where there's no native backbutton that behavior is unconveniend when opening an Url from the internet.
How do I get Xamarin.Forms to let a browser on the device open the links instead, so that it works in at least Android, iOS and UWP?
There is no built-in property or anything that lets you do this.
However, the WebView does have a Navigating event handler. You should be able to hook into that, redirect the user to whatever you want and then cancel the original event. Something like this:
public void WebView_Navigating(object sender, WebNavigatingEventArgs args)
{
if (args.Url.StartsWith("file://"))
{
return;
}
Device.OpenUri(new Uri(args.Url));
args.Cancel = true;
}
To hook it up from code:
var webView = new WebView();
webView.Navigating += WebView_Navigating;
from XAML:
<WebView Navigating="WebView_Navigating" />
More info:
https://learn.microsoft.com/en-us/dotnet/api/xamarin.forms.webview.navigating?view=xamarin-forms

Xamarin Binding Title of Navigation page

i´ve got an little problem. i got like a searchpage, and if you click on an item from the search, you will get so an other page using the navigationpage. i want the title of this page set to the clicked item. here´s what i got:
private async void PatListe_ItemTapped(object sender, ItemTappedEventArgs e)
{
if (e.Item is Patient pat)
{
PatNN = pat.NName.ToString();
PatVN = pat.VName.ToString();
PatInt = pat.Fallnummer;
}
PatVN_NN = PatNN + " " + PatVN;
await Navigation.PushAsync(new PatientView());
}
so this gives me the "name" I want for the title. with debugging and stopping-points, i noticed that this is working. now there are 2 mistakes i guess, one is on the other site in .cs and on in the .xaml file. here´s the one from my .cs-file:
private string PatNameTitle = SuchErgebnisse.PatVN_NN;
while debugging, i recognised that this does not get any value, its null. but: i´ve got the same with the PatInt and this is working...
but even if this is working, there´s an other mistake - in my xaml file:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="INESVitaAppTest.Views.PatientenView" BackgroundColor="#235d2a" Title="{Binding PatNameTitle}">
this gives me nothing even when i m using a string, like " PatNameTitle = "foo"; "
thanks a lot !
damn, i got it. There was no initialisation of the PatNameTitle....here is what i added:
Title = PatNameTitle;
in an method that get´s called when i open this page.. ant then i deleted the Title="{Binding..}"-stuff, now it s orking :) sorry

Windows phone app, how to use narrator read page title automatically?

I'm developing windows universal app(XAML,C#) on phone, and am enabling accessibility for Narrator. Does anybody know how to get narrator automatically to read page title when a page is opened?
I tried setting automationproperties.name in page but didn't work:
<Page
x:Class="xxxxxx"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
AutomationProperties.Name="Page title to be read"
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
The features of the Narrator for UWP are applied when you select a control in a list, or editing a textbox. If you want to read content when the app is opened you should use the SpeechSynthesizer API, which is really easy to implement:
1.- In XAML add a Media Element
<MediaElement x:Name="MediaElement"/>
2.-Then in the code behind of the page:
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
ReadTitle();
}
private async void ReadTitle()
{
var voice = SpeechSynthesizer.AllVoices.First();
SpeechSynthesizer reader = new SpeechSynthesizer() { Voice = voice };
var text= this.GetValue(AutomationProperties.NameProperty) as String;
await reader.SynthesizeTextToStreamAsync(text);
MediaElement.SetSource(stream, stream.ContentType);
MediaElement.Play();
}
You can read everything you want passing the string to the reader.
You need to make view-able by narrator. I don't believe you can declare the Name property within the Page Class. Try something like this within the content of your Page:
<HyperlinkButton
NavigateUri="www.bing.com"
AutomationProperties.AutomationID="bing url" //Not Required to work
AutomationProperties.Name="Go to the Bing Homepage"//Narrator will read this
<StackPanel>
<TextBlock Text="Bing Dot Com" />
<TextBlock Text="www.bing.com" />
<StackPanel>
</HyperlinkButton>
https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.automation.peers.accessibilityview
EDIT: You may also need to programmatically set focus on the item for this to work

Resources