Question: Is it possible to log to a file on the iPhone (or get at the console output somehow) to read it later, or perhaps directly from the phone?

I need to debug an app that is using a cable-connected accessory device, so it cannot be connected to XCode at the same time.

Answer: 
You can make use of [PonyDebugger](https://github.com/square/PonyDebugger). 

I've been using it a lot to debug networking and CoreData resources, but it also allows logging to the console with PDLog.

> 
> PonyDebugger is a remote debugging toolset. It is a client library and gateway server combination that uses Chrome Developer Tools on your browser to debug your application's network traffic and managed object contexts.
> 
> 
> 

Some recommendations:

* It works better in Safari than Chrome.
* Look into repo issues for making it work in your OSX version if needed. I have it working on El Capitan.
* Automatic connection didn't work for me, try using local IP gateway address instead:
	+ e.g. `[debugger connectToURL:[NSURL URLWithString:@"ws://192.168.0.12:9000/device"]];`
It is really a pity that Apple doesn't provide WiFi debugging for Xcode, but for your question, the answer is YES, you can definitely redirect NSLog to a log file, you can try this approach:

```
// this will redirect all NSLog in your project, add it to PCH file.
#define NSLog(args...) writeLog(__PRETTY_FUNCTION__, __LINE__, args)

void writeLog(const char *function, int lineNumber, NSString *format, ...) {

// basic log content.
va_list ap;
va_start (ap, format);
NSString *msg = [[NSString alloc] initWithFormat:format arguments:ap];   
va_end (ap);

// add function name, and line number.
NSString *log = [NSString stringWithFormat:@"%s line %d $ %@", function, lineNumber, msg];

// add your own code to write `log` into a text file.
....
}

```

Note: if you are using some libraries which also write logs to Console, this macro won't redirect those logs to your log file, it will only work with `NSLog` in your own source files.