Monday, July 27, 2015

Using CFNETWORK_DIAGNOSTICS for network log for iOS

CFNetwork has built-in support to log network activity for iOS device by setting the environment variable CFNETWORK_DIAGNOSTICS.

The CFNETWORK_DIAGNOSTICS can be set to the following values:

    1: Enables internal CFNetwork event and url logging

    2: Adds information about http header and how CFNetwork decides to make and re-use TCP sockets

    3: Adds decrypted bytes in and out of network connection.

Following the below steps to use this feature
1. Enable the feature with one of the following options

   option 1: call setenv method in xcode project's main method
   int main(int argc, char * argv[]) {
       setenv("CFNETWORK_DIAGNOSTICS", "3", 1);
          @autoreleasepool {
               return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
       }
  }

  option 2: set the environment var in project scheme as 


2. build and run the project on device, make the server request to be tested

3. from xcode device tab, export the application container. Then show the package content of the downloaded container from Finder, and the log file will be in the folder of
/AppData/Library/Logs/CrashReporter.

  


2 comments: