Hayden Passmore
Hayden Passmore

Reputation: 1155

Adding html to a webview is appearing as blank iOS xamarin forms

I have a web view in a Xamarin.forms project and it is populated by some HTML that is loaded from a database.

The following is the XAML code that creates the web view. It may be worth noting that this is inside a GridView.

<WebView Grid.Row="2" x:Name="policyView" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" />

And this is the cs code that loads and sets the web view's source so it can display the HTML.

     public async void LoadPolicy(policy_procedure policy)
    {
        busyIndicator.IsVisible = true;
        busyMessage.Text = "Loading Policy";

        var htmlSource = new HtmlWebViewSource();

        htmlSource.Html = await ViewModelLocator.ConvertPolicy(policy);// returns the html as a string

        policyView.Source = htmlSource;

        busyIndicator.IsVisible = false;
    }

This code is working fine for Android and is displaying the HTML content without any problem. However, when running on iOS I am just getting a blank screen and I am unsure why this is happening. I have tried setting the horizontal options to fillAndExpand likewise with vertical options. My Question is how do I make the HTML code appear on iOS?

Thanks.

Upvotes: 1

Views: 1556

Answers (2)

Narendra Sharma
Narendra Sharma

Reputation: 682

The code below I tested in for iOS. You can adjust it according to your requirement.

 <ContentPage.Content>
                    <StackLayout BackgroundColor="Teal" TranslationY="64">
                 <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*"/>
                             <RowDefinition Height="Auto"/>
                                          </Grid.RowDefinitions>
                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition Width="*"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <Label Grid.Row="0" Grid.Column="0" BackgroundColor="Silver" Text="tets test" HeightRequest="30" 
        HorizontalOptions="FillAndExpand"/>
                        <WebView Grid.Row="1" Grid.Column="1" BackgroundColor="Red" x:Name="webView"
        VerticalOptions="FillAndExpand"  HeightRequest="300"
        HorizontalOptions="FillAndExpand" /> 
                    </Grid>
                       </StackLayout>
                </ContentPage.Content>

webview iOS screenshot

Upvotes: 0

Narendra Sharma
Narendra Sharma

Reputation: 682

This is happening because you have not given AppTransportSecurity(ATS). Go to to your info.plist and add the permission.

<key>NSAppTransportSecurity</key>  
<dict> 
   <key>NSAllowsArbitraryLoads</key><true/>  
</dict> 

for reference: https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

Upvotes: 1

Related Questions