【问题标题】:Xamarin Forms-(IOS) System.Net.WebException has been thrownXamarin Forms-(IOS) System.Net.WebException 已被抛出
【发布时间】:2017-06-02 22:25:18
【问题描述】:

我是 Xamarin Forms 的新手,我正在尝试将 JSONdata 以及来自网络的图像检索到 ListView 但是,我读到了 ATS https://developer.xamarin.com/guides/ios/platform_features/introduction_to_ios9/ats/ 和我试图在info.plist 中实现它,下面是屏幕截图,但我仍然得到System.Net.WebException。

我的形象

Image Screenshot

这是我得到的错误:

   $exception   {System.Net.WebException: 
    The resource could not be loaded because 
the App Transport Security policy …}    
System.Net.WebException

这是我的班级页面 OnlineStreaming.xaml.cs

public partial class OnlineStreaming : ContentPage
    {
        private HttpClient _client = new HttpClient();
        public ObservableCollection<Adverts> adverts;
        private const string Url = "http://eamobiledirectory.com/cooperp/Mobile/Mobileapi.aspx?Action=Featured&Country=Uganda";
        private const string BaseImageUrl = "http://eamobiledirectory.com/cooperp/Images/app_images/";

        public OnlineStreaming()
        {
            InitializeComponent();
        }




        protected override async void OnAppearing()
        {

            var content = await _client.GetStringAsync(Url);
            var adv = JsonConvert.DeserializeObject<List<Adverts>>(content);

            adverts = new ObservableCollection<Adverts>(adv);

            foreach (var item in adverts)
            {
                //some coding



                adverts.Add(new Adverts
                {

                    company_name = item.company_name
                                       ,
                    office_photo = BaseImageUrl + item.office_photo
                });

                //Debug.WriteLine("Output  is here  :" +  item.company_name);


            }



            listViewTest.ItemsSource = adverts;




        }
    }

我的 Xaml 页面:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage 
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    Title="Online Streaming"
    x:Class="BBSTV.OnlineStreaming">



    <ListView  x:Name="listViewTest">
    <ListView.ItemTemplate>
    <DataTemplate>
                <ViewCell>
                    <StackLayout>
                    <Image  Aspect="Fill"  Source="{Binding office_photo}"/>
                        <Label  TextColor="Gray" Text="{Binding company_name}"/>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</ContentPage>

请帮帮我,为什么这是在 IOS 上。

【问题讨论】:

    标签: c# ios xamarin.ios xamarin.forms


    【解决方案1】:

    如果您的应用需要加载和显示来自非安全的网页内容 网站,将以下内容添加到您应用的 Info.plist 文件中以允许 Web Apple Transport Security (ATS) 时正确加载的页面 应用程序的其余部分仍启用保护:

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

    您需要将 NSAllowsArbitraryLoadsInWebContent 设置为 true 。 当前您设置为 false。

    清理项目并再次运行

    【讨论】:

    • 我添加了 NSAllowsArbitraryLoadsInWebContent 加上 NSAllowsArbitraryLoads ,我将所有设置为 true,但它仍然带来相同的异常,请帮助我我哪里错了。
    • 删除所有与 ATS 相关的密钥并使用建议的答案。
    • 是的@KKRocks,我删除了所有这些,我使用了你的解决方案,但仍然是同样的错误,我很困惑:(
    • 我在 Xcode 和 Xamarin 中使用了相同的包名称,这是否意味着我也在 Xcode 中编辑了 info.plist file ?或
    • 如何在模拟器中删除应用程序
    猜你喜欢
    • 2019-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 2021-12-30
    相关资源
    最近更新 更多