Capture and Sanitize Network Data

Capture and Sanitize Network Requests

Network requests in iOS are automatically captured by default.

Disable Network Capture

To disable the automatic network capture initialize the SDK with the networkCaptureEnabled set to false.

let configuration = Configuration(appID: "<APP_SLUG>", networkCaptureEnabled: false)
SDK.initialize(configuration: configuration)

Manual Network Capture

If you have disabled automatic network capture and would like to only capture specific requests, you can use our RequestBuilder object.

let requestBuilder = SDK.newRequestBuilder()
requestBuilder.headers = ["Header": "Value"]
requestBuilder.method = "GET"
requestBuilder.url = "https://www.example.com"
let responseBuilder = requestBuilder.capture()
responseBuilder.status = 200
responseBuilder.capture()

For the request builder, the headers, method and URL are required fields. For the response builder, only the headers are required.

Redacting Sensitive Information

The SDK accepts a requestSanitizer and responseSanitizer at initialization. The values provided must conform to the RequestSanitizer protocol (LRORequestSanitizer in Objective-C) and the ResponseSanitizer protocol (LROResponseSanitizer in Objective-C) respectively.

import Foundation
import LogRocket

class ExampleSanitizer: RequestSanitizer, ResponseSanitizer {
  public func sanitize(request: Request) -> Request? {
    if let url = request.url, url.contains("privateInformation") {
      // This will redact the entirety of the request
      // The matching response will be ignored as well
      return nil
    }
    
    // This will remove only privateHeader, but leave the rest of the headers in the recording
    request.headers?.removeValue(forKey: "privateHeader")

    return request
  }

  public func sanitize(response: Response) -> Response? {
    if response.status == 404 {
      // This will redact only the response body for responses with a 404 status code
      response.body = nil
    }

    return response
  }
}

// Add to configuration
let sanitizer = ExampleSanitizer()
let configuration = Configuration(appID: "<APP_SLUG>", requestSanitizer: sanitizer, responseSanitizer: sanitizer)
SDK.initialize(configuration: configuration)