What's the best way to log debug info in an iphone app? - iphone

Is there some standard way or has anyone written something that allows you to log a message and have it be displayed either in a small scrolling section on the iphone screen or in a separate window in the iphone simulator?
Update:
For noobs like me and don't know, use the NSLog methods as decribed below and make sure you select Run->Console to display the console.
Would still like to know if anyone has written a simple logger that displays on the iphone itself....

I don't have enough 'reputation' to add a direct comment for your posting but: don't forget to go to XCode->Preferences->Debugging->On Start: Choose Show Console & Debugger
You can of course choose just the Console or whatever, but you'll probably want the Debugger to. To use that, just click to the left of the line you want to break at. You can also toggle from 'Activate' to 'Deactivate' so you if you know that there are a bunch of breakpoints you don't need to hit in the beginning of your application set the debugging to Deactive (in the debugging window) and then, before you hit the UI element in your app you want to debug, toggle that same button to Activate so your breakpoints become active. Otherwise, you could of course just click Continue until you got to your section.
Also, on the NSLog, if you start to accumulate a bunch of log statements, and you need to 'find' one in particular, it helps to do: NSLog(#"\n\n\nMy statement\n\n\n); which will give a bunch of line breaks. Also, for the uninitiated:
NSLog(#"My int: %d my BOOL: %d", myInt, myBOOL);
NSLog(#"My object of any NSObject: %#", anObjectOfAnyKind);
NSLog(#"My float: %f",myFloat);
Hope all of this is helpful and sorry if I got off track with the debugging bit ;)

The Objective-C (more correct, really) method is
NSLog(#"message");
But the standard C method will work also
printf("message");

Use NSLog(#"Log message");

If your have an application that is crashing then your can ask the users you the crash log. The crash log contains information about what the application was doing when it crashed and the stack trace.
iPhone app log files are also stored on your users computer, and are copied across everytime they sync their iPhone. ( Note that DEVICE_NAME will be the same name of your iPhone in iTunes, and each log file will begin with the name of the app. )
Mac OS X : /Library/Logs/CrashReporter/MobileDevice//
Windows XP: C:\Documents and Settings\Application Data\Apple computer\Logs\CrashReporter\
Windows Vista: C:\Users\AppData\Roaming\Apple computer\Logs\CrashReporter\MobileDevice\

For Swift, it's simply
print("log msg")

Related

Actions on Google - configure Simulator to display my test app

My test app is displayed only on the 'display' tab and not on the conversation menu on the left.
Expected:
Actual:
How can it be fixed?
A short term fix, until the bug in the simulator is resolved, is to change your surface. You can switch to speaker if you want to hear the response. Note that you can only swap surfaces when you are not in an active conversation, if you are in one, just click cancel, and then switch surfaces. If you do switch to speaker you will not see the display anymore, but it will say the question you are seeking. Please don't be discouraged, this was not a bug in your code, it is in the simulator.
it is only the first TTS (i.e Welcome! ... ) that you may not hear back the audio file. It is a bug and they are working to fix it. But as long as you see "Welcome!..." in the display card now, your action is working

How to simulate the very first start of an app in Xcode 6

I need to test some behaviors of the iOS 8 at the very first start of my app. Is it possible to simulate this in Xcode 6? If yes, then how?
Deleting the app will do it but note that certain pieces of information will be cached for a while like your permissions settings (notification, calendar, etc.). You can go to settings.app and reset settings to clear those out if that matters in your use case.
If you mean the FIRST start of the app, then what I did to achive this is, on start (viewDidLoad) check in the NSUserDefaults for example ,if the value "hasAlreadyStarted" exists (NSUserDefaults.objectForKey(..) ), if not, then its the first start of the app, and then i would set the value to true, so when you close the app, and open again, the value will exist.
Dareon I'm not sure what exactly you want to achieve. In Xcode 6 yes you can simulate your app from start. If you want to test behaviours I think you are looking for instruments. Right Click on the Xcode icon in your dock select option and choose instruments. You can add several instruments your phone or emulator support like connectivity or gps or memory to see exactly the behaviour of your app. Hope that helps
Well if by very start of the app cycle you mean before the app loads, there is a way.
In your ViewController call the ViewWillLoad function:
class ViewController
{
override func viewWillAppear(animated: Bool)
{
// your code
}
}
This event will be called before the view loads or appears.
Hope it helps :)
As Shanti K said in the comment, if you delete your application from the simulator and then run it again, you will be simulating the first run. To delete the application from the simulator, you mimic the same behavior on a device.
Click and hold on the icon, until they start shaking. Click the close X next to your application, and verify that you want to delete it if it asks. Then Shift + Command + H to simulate hitting the home button.

Resetting iPhone Application

I have an application that is currently named "xxxx". The app runs and works perfectly without any problems. However, I have reached a stumbling point. When I try to load the application onto my iPhone, the pictures on my picker view seemed to have disappeared for some strange reason. I've done multiple hours of debugging and everything checks out normal. I then decided I wanted to change the product name from "xxxx" to "xxxx Redo". As soon as I did that, the application loaded perfectly normal (with the pictures in the Picker View as it should be).
In conclusion, it comes to find out that there is no actual bug in the programming aspect of it. However, my question comes up as to why does it not load the correct way with the original product name but then loads the right way when the product name is changed?
The second part of my question is if anyone knows of a way to fix the original application so that way it displays correctly again with the original name?
I have tried the following:
Cleaning the app
Trashing the .app file and rebuilding
Deleting the app on the iPhone and rebuilding.
These things sometimes just happen, you can try the following extra steps:
clean build folder in XCode (press alt while clicking Product menu)
reboot the phone (poweroff/poweron)
I have seen cases where it made a difference.

Write to console synchronously

I need to debug a time-critical crash I have in my App and since NSLog does not immediately write to the console I am wondering if there is a way to synchronously write to the console from my code.
So for example is printf a better choice here or would I have to deal with the same problems.
You can add a breakpoint before the crashing line(s), though I think Xcode by default already hangs the proccess so that you can still access the debugger console ... If it doesn't, you may want to add some additional diagnostics by going to Product -> Edit Scheme .. (in the upper menu), such as Guard Edges, Zombie Objects, etc.
In addition, you can call NSLog from the main thread:
[self performSelectorOnMainThread:#selector(log:) withObject:#"String .." waitUntilDone:YES];
- (void)log:(NSString *)message {
NSLog(message);
}

Where is output of NSLog() written?

I am developing an iPhone app. Is output of NSLog written in a file in iPhone?
I am in situation that if I didn't write NSLog, my app would not work. I don't care it is written in my iMac. But I care if it is written in iPhone.
The NSLog() outputs text to the console, which can be read by the users on their devices by downloading some Console-app. It is suggested that you don't use NSLog() for anything else than error reporting in the finished products, but it's fine during testing.
Here is an approach you could do to make sure the NSLog's outputted are only being called in the finished app: http://joshhighland.com/blog/2010/04/10/dont-use-nslog-anymore/
I'm not sure what are you trying to do, but when you call NSLog, the output is shown in the lower right window. For example if I do:
NSLog http://img576.imageshack.us/img576/791/screenshot20111209at827.png
The output will be:
Output http://img857.imageshack.us/img857/3128/screenshot20111209at828.png
Also, make sure to select the console view in Xcode
Console View http://img3.imageshack.us/img3/791/screenshot20111209at827.png
If you want to show some text in the app directly, I suggest using UILabel or UITextView :)
See ya,
NiCK
Quite to the contrary, the log is written in the debugger, like so:
You can access the debugger by clicking on the middle button in the right-most tab bar on the navigation bar of Xcode (the tab bar is titled "View"). Your logs will looks something like this:
2011-12-09 Example[55409:40b] Log message
...giving the date, application, and some other stuff before the log message.