iosswiftapp-transport-security

iOS Loading images from HTTP


Is it possible to download an image from any website?

My problem is App Transport Security, because it is impossible to use domain exceptions for the whole internet, and unsecure ( and probably forbidden by Apple ) to bypass ATS.

HTTPS works fine, but what if user wants to save an image from HTTP?


Solution

  • It is not forbidden, but at some point, Apple may ask you to provide justification for why you are lowering security of your app by using an ATS exception.

    To allow HTTP connections to a single domain, which sounds like what you need, simply add the following to your Info.plist

    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>yourdomain.com</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>
    

    The key is to limit your exceptions to as narrow of a scope as possible. In this case, you are limiting to only allow insecure (HTTP) connections to the domain of the server that hosts your images.