How do I load an HTTP URL with App Transport Security enabled in iOS 9?


How to Load an HTTP URL with App Transport Security Enabled in iOS 9 📲💻
So, you're a developer working on an iOS app and you want to load an HTTP URL with App Transport Security (ATS) enabled in iOS 9. By default, ATS enforces secure connections using HTTPS, which is great for user privacy and security. However, there may be situations where you need to load HTTP URLs, either for local development or for connection to a web service on your local machine. This blog post will guide you through the process and provide easy solutions to this common problem. Let's dive in! 🚀
Understanding App Transport Security (ATS) 🕵️♂️🔒
First, let's clarify what App Transport Security (ATS) is. ATS is a feature introduced in iOS 9 that enforces secure network connections. It ensures that your app uses secure communication protocols, such as HTTPS, by default. ATS improves the privacy and security of user data and prevents certain security vulnerabilities. However, it can cause issues when trying to load HTTP URLs.
The Challenge: Loading HTTP URLs with ATS Enabled 😓🔐
In your specific case, you mentioned that you have a local development environment where you need to connect to a web service running on your laptop. This web service is using HTTP instead of HTTPS. When you try to load the HTTP URL in your iOS app, ATS automatically replaces it with an HTTPS URL, which is not what you want.
The Solution: Disabling ATS for Specific URLs 🤔💡
To load HTTP URLs with ATS enabled in iOS 9 or later, you can make use of the NSAppTransportSecurity
key in your app's Info.plist file. Here's how you can do it:
Open your Xcode project and locate the Info.plist file.
Add a new entry by clicking on the "+" button. Name it
NSAppTransportSecurity
and make it a dictionary type.Inside
NSAppTransportSecurity
, add a sub-entry namedNSAllowsArbitraryLoads
and set its value toYES
.
Here's what your Info.plist file should look like:
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
By setting NSAllowsArbitraryLoads
to YES
, you are instructing iOS that it is allowed to load both HTTP and HTTPS URLs. This will disable ATS for all URLs in your app, not just the specific ones you want to load via HTTP.
✔️ Note: Disabling ATS should only be done for local development or debugging purposes. It is important to ensure that your app uses secure connections (HTTPS) in a production environment to protect user data.
Secure Alternative: Using HTTPS for Local Development 🌐🔐
While disabling ATS might be necessary for local development, it's essential to use secure connections whenever possible. Consider setting up HTTPS in your local development environment to align with best practices and improve the overall security of your app.
You can generate a self-signed SSL certificate and configure your web service to use HTTPS. By doing this, you'll be able to access your web service using a secure connection in your app.
Engage and Share! 📢📲✅
We hope this guide helps you load HTTP URLs with App Transport Security enabled in iOS 9. Remember to disable ATS only for local development and use secure connections (HTTPS) for your production environment.
If you found this blog post helpful, please share it with your fellow iOS developers! And don't forget to let us know in the comments if you have any questions or other topics you'd like us to cover in future blog posts. Happy coding! 💻🙌
References:
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.
