Viggo Lundén
Viggo Lundén

Reputation: 788

Downloading an image from an http website in Xamarin with C#

I know how to download an image from a website with the https protocol:

static UIImage FromUrl (string uri)
{
    using (var url = new NSUrl (uri))
    using (var data = NSData.FromUrl (url))
        return UIImage.LoadFromData (data);
}

but when uri isn't an https website data becomes null. Is there a workaround for this issue? Thanks in advance.

Upvotes: 2

Views: 672

Answers (1)

Kurt Raschke
Kurt Raschke

Reputation: 980

If it's specifically an issue with sites being served over insecure HTTP, then it's probably due to App Transport Security, which by default blocks connections to non-HTTPS sites. You can either whitelist specific sites which you need to reach over HTTP, or disable ATS entirely if necessary, using settings in Info.plist:

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

Upvotes: 4

Related Questions