Startseite Debugging Network Requests in iOS with Swift

Debugging Network Requests in iOS with Swift

Debugging Network Requests in iOS with Swift

In this article, we are going to discuss an essential part of every mobile application that interacts with a backend server - network requests. Specifically, we’ll focus on how to debug network requests in an iOS application using Swift.

While developing an iOS application, it’s very common to encounter issues with network requests such as invalid request formats, issues with authorization or unexpected server responses. Being able to effectively debug these network requests can save you a lot of time and effort.

Let’s start with the most straightforward way of debugging - logging.


One of the most common ways to debug network requests is to log essential parts of the requests and responses to the console. This includes the request’s URL, method, headers, and body, as well as the response’s status code and body.

Swift’s os_log API can be used for this purpose. It provides a powerful, flexible way to efficiently log diagnostic messages from your app.

Here is a simple example of how you can use os_log to log network request and response details:

os_log("Starting request to endpoint: %@", log: self.log, type: .info, endPoint.path)
os_log("Built request: %@", log: self.log, type: .debug, request.debugDescription)
os_log("Request httpBody: %@", log: self.log, type: .debug, String(data: request.httpBody ?? Data(), encoding: .utf8) ?? "Not decodable to string")
os_log("Request headers: %@", log: self.log, type: .debug, request.allHTTPHeaderFields?.description ?? "None")
os_log("Request URL: %@", log: self.log, type: .debug, request.url?.absoluteString ?? "None")
os_log("Received data: %@", log: self.log, type: .debug, String(data: data, encoding: .utf8) ?? "Not decodable to string")
os_log("HTTP error: %d", log: self.log, type: .error, httpResponse.statusCode)

However, it’s important to be careful with what you log. Do not log sensitive data like passwords, tokens, or personally identifiable information (PII). This is particularly relevant when working with JWT tokens or similar, which often carry sensitive data.

Anonymizing Sensitive Data

For instance, you might want to log the identity token used for signing in with Apple. However, this token contains sensitive information and should not be logged in its entirety.

You could instead log a part of the token, for example the first few and last few characters, and replace the rest with placeholders:

let anonymizedToken = String(identityToken.prefix(8)) + "..." + String(identityToken.suffix(8))
os_log("Verifying identity token: %@", log: self.log, type: .info, anonymizedToken)

This will give you enough information to identify individual tokens in the logs, without revealing sensitive information.

Checking the Server Logs

While the client logs can give you a lot of information about what’s happening on the device, sometimes the problem lies on the server side. If you have access to the server logs, you should also check them for any errors or warnings.

Here’s a simple example of what you might find in the server logs:

2023-06-30 21:01:47,838 - quartapp - INFO - Request to /verifyIdentityToken...
2023-06-30 21:01:47,838 - quartapp - DEBUG - Request data: None
2023-06-30 21:01:47,838 - quartapp - ERROR - Missing request body

In this case, the server log shows that the request body was missing from the request. This gives you a clear indication of what went wrong and where to look in your client code to fix the issue.

In conclusion, debugging network requests might seem daunting at first, but with the right tools and practices, it becomes a manageable task. Remember to use logging effectively and always be mindful of security and privacy considerations when handling sensitive data. Happy debugging!

Note: The code snippets used in this article are based on Swift 5 and the standard Swift and Foundation libraries available as of Xcode 13. Some details might differ in newer or older versions of Swift and Xcode.

Dieser Eintrag ist vom Autor unter CC BY 4.0 lizensiert.