Generating Log Files For iOS Device

Recently, I got a task where I had to generate log files for the iOS devices. After a lot of research, I finally found the solution. While searching for how to generate log files, I noticed that there were many forums on this particular topic which remained unanswered. So, here’s the solution.
 
Objective-C
  1. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);  
  2. NSString *documentsDirectory = [paths objectAtIndex:0];  
  3. NSString *fileName =[NSString stringWithFormat:@"%@.txt",[NSDate date]];  
  4. NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];  
  5. freopen([logFilePath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);  
Swift
  1. var paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)  
  2. let documentsDirectory = paths[0]  
  3. let fileName = "\(Date()).txt"  
  4. let logFilePath = (documentsDirectory as NSString).appendingPathComponent(fileName)  
  5. freopen(logFilePath.cString(using: String.Encoding.ascii)!, "a+", stderr)  
Just add this block of code in the application: didFinishLaunchingWithOptions method in the App Delegate file. This will create a plain text file in the app document directory on iPhone which will log all the console log events. You need to import this file from iTunes to see all console events.
 
Before your files can appear in the Files app, you must indicate that your app supports Open in Place and File Sharing Enabled. These options are configured using keys in your Info.plist file. The first key is UIFileSharingEnabled, which enables iTunes sharing of files in your Documents folder. The second key is LSSupportsOpeningDocumentsInPlace, which grants the local file provider access to files in your Documents folder. Add these keys to your Info.plist and set their values to YES.
  • UIFileSharingEnabled - Application supports iTunes file sharing
  • LSSupportsOpeningDocumentsInPlace - Supports opening documents in place
Generating Log Files For iOS Device 
 
Run your app again to make these changes take effect.
 
To get the log files, Launch iTunes, after your device has connected select Apps - select your App - in Document you will get your file. You can then save it to your disk.
 
After you add LSSupportsOpeningDocumentsInPlace in the Info.plist, your log files should now appear in the Files app on your iPhone device.


Similar Articles