Getting ready for iOS 14 local network privacy restrictions

Apple added privacy control in iOS 14 that affects apps accessing the local network. When your app tries to access the user's local network, iOS will show a prompt asking the user to allow access. This should be tested on a real device.

Screenshot of iOS prompt to grant access to local network

We can customize the usage description in the Info.plist file using the key NSLocalNetworkUsageDescription.

Screenshot of Info.plist file in Xcode

If the user doesn't allow access, the app won't be able to connect to devices on local network. In our example, if the user tries to connect to a local HTTP server, we'll get an error The Internet connection appears to be offline.

Screenshot of Xcode console with internet connection error printed

If the user allows access, then the request should go through once permission is granted. For this we will need to create URLSession with waitsForConnectivity set to true in configuration and an appropriate timeout.

let sessionConfig = URLSessionConfiguration.default
sessionConfig.waitsForConnectivity = true
sessionConfig.timeoutIntervalForResource = 60

URLSession(configuration: sessionConfig)
    .dataTask(with: URLRequest(url: url)) { data, response, error in
    
    if let response = response as? HTTPURLResponse {
        print("Response code: \(response.statusCode)")
    }
    
    if let error = error {
        print(error.localizedDescription)
    }
    
}.resume()

The user can change the Local Network access permissions inside the device Settings for your app.

Screenshot of Local Network device setting set to true
Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Level up your Swift skills!$35

100+ tips to take your Swift code to the next level

Swift Gemsby Natalia Panferova

  • Advanced Swift techniques for experienced developers bypassing basic tutorials
  • Curated, actionable tips ready for immediate integration into any Swift project
  • Strategies to improve code quality, structure, and performance across all platforms

Level up your Swift skills!

100+ tips to take your Swift code to the next level

Swift Gems by Natalia Panferova book coverSwift Gems by Natalia Panferova book cover

Swift Gems

by Natalia Panferova

$35