Eric Brotto
Eric Brotto

Reputation: 54281

Detect if iPhone device is attached to machine for debug purposes

Simply put I would like to print to the console when my device is attached and print to the device when it is not attached. I never use the simulator, but would like to have a sort of guard similar to the one below. Except rather then detecting whether or not I'm using the simulator I would like to automatically detect if the device is attached or not.

Anyway we could make this happen?

Thanks!

#if TARGET_IPHONE_SIMULATOR == 0
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
#endif

Upvotes: 0

Views: 236

Answers (2)

E. Rivera
E. Rivera

Reputation: 10938

In my case I would like to show UIAlertViews if not attached and print to NSLog otherwise. The question is if there's a way to add #if ... #endif directives for when attached/not attached for debug.

Upvotes: 0

Mugunth
Mugunth

Reputation: 14509

Print to console vs Print to device means? When you use the normal NSLog, it shows up on Console when device is connected and shows up on device log (which you can extract from Xcode organizer) when not connected.

Can you be a bit more specific on what you are trying to do?

Update

enter image description here

This is where you see the log.

On organizer, you have something called "Devices". Expand that, and select "Console". The console will be pretty messy since it contains logs of all apps and system built-in logs as well. You can filter them by your app name though.

Upvotes: 2

Related Questions