NSURLConnection and Basic HTTP Authentication in iOS
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
NSURLConnection was the old Foundation API for HTTP networking on iOS, and it could handle HTTP Basic Authentication through delegate challenge methods. Today it is legacy technology, and new code should use URLSession. Still, if you are maintaining older Objective-C code, the important part is understanding the authentication challenge flow and how to provide a NSURLCredential safely.
What Basic Authentication Is
HTTP Basic Authentication sends a username and password with the request, usually after the server challenges the client with a 401 Unauthorized response. The credentials are only base64-encoded, not encrypted, so Basic Auth should be used over HTTPS, not plain HTTP.
That rule matters more than the API details. If the connection is not protected by TLS, Basic Auth is not an acceptable design.
Legacy NSURLConnection Flow
With NSURLConnection, authentication is typically handled through delegate callbacks. The connection receives a challenge, and your delegate either responds with credentials or cancels the challenge.
A typical Objective-C example looks like this:
This is the legacy pattern many older projects still use.
Why This Is Legacy Code Now
Apple has long recommended URLSession for modern networking. NSURLConnection remains relevant mostly for maintenance work, debugging old code, or understanding older tutorials.
The core networking concept did not change:
- make a request
- receive an authentication challenge
- respond with credentials
- continue or cancel
Only the API surface changed.
Modern Equivalent With URLSession
If you are writing new code, use URLSession and its challenge delegate methods.
That is the API direction you should prefer unless the codebase is stuck on legacy patterns.
Alternative: Put The Header On The Request
For some controlled internal systems, you may see code that prebuilds the Authorization header.
This works, but it is less flexible than challenge handling and still requires HTTPS. It also pushes credential construction closer to application logic, which may not be what you want.
Common Pitfalls
- Using Basic Auth over plain HTTP instead of HTTPS.
- Continuing to add new
NSURLConnectioncode instead of usingURLSession. - Responding to every challenge the same way without checking the authentication method.
- Hardcoding credentials in source code for production systems.
- Confusing base64 encoding with encryption.
Summary
- '
NSURLConnectioncan handle Basic Auth through authentication challenge delegates.' - The legacy solution is to provide a
NSURLCredentialwhen the server issues an HTTP Basic challenge. - Modern iOS networking should use
URLSessioninstead. - Basic Authentication should be sent only over HTTPS.
- When maintaining legacy code, focus on the challenge flow and migrate to
URLSessionwhen possible.
Related reading
- NSURLRequest setting the HTTP header
- NSURLResponse - How to get status code?
- NSURLSession How to increase time out for URL requests?
- NSURLSession/NSURLConnection HTTP load failed on iOS 9
- NSURLSession HTTP load failed kCFStreamErrorDomainSSL, -9813 ; Self signing certificate
- OAuth with Verification in .NET
- NSURLConnection and sendAsynchronousRequestqueuecompletionHandler - does the completion block run in the main thread
- NSURLSession Threads Tracking multiple background downloads

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.