简体   繁体   中英

iOS app debug without cable

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.

You can make use of 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:
    • eg [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.

As far as I know, this is not possible.

You can eventually use a free and excellent solution like bugfender which will allow you to get the data logged by NSLog remotely. This could help you.

Kevin Xi's approach is OK. You can also redirect NSLog output to the file using following code in AppDelegate's didFinishLaunchingWithOptions method:

NSArray *allPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [allPaths objectAtIndex:0];
NSString *pathForLog = [documentsDirectory stringByAppendingPathComponent:@"log.txt"];
freopen([pathForLog cStringUsingEncoding:NSASCIIStringEncoding],"a+", stderr);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM