Cocoa

The Complete Friday Q&A by Mike Ash

Posted by Dallas on January 27, 2011
Apple, Cocoa, Cocoa Touch, iOS, Mac, Objective-C, Programming, SDK / No Comments

Mike Ash has turned his famous Friday Question and Answer series into a book, available in either iBooks or Kindle versions.

Mike is an amazingly smart guy, who knows more about programming then you could ever imagine.

If you work with Objective-C you owe it to yourself to check out his book and his site.

Tags: , , , , ,

Log all accessed methods in Objective-C with DTrace

Posted by Dallas on February 07, 2009
Cocoa, Cocoa Touch, DTrace, iPhone, Mac, Objective-C, Programming / No Comments

I have recently been debugging some Objective-C and Cocoa/Cocoa Touch code.

Not being familiar with all the code and with a TON of methods being accessed in milliseconds, my attempts at stepping through the code just wasn’t working, as clicking ‘Step Into’ as fast as I could keep up and over an hour of stepping through I was still not even close to figuring out what I needed to know.

This lead me to the thought of logging every method that was accessed and then I could step look through it and skip around as needed.
However with 60 some odd files and say maybe 20 methods in each file, that would take a lot of NSLog’s.
I started looking around and was pointed to bbums blog about doing something similar.
However it still would take adding some code, plus I couldn’t get it working right with the iPhone.

This lead me to wonder about DTrace.
After a recent podcast from Scotty at the Mac Developer Network, featuring Colin Wheeler talking about DTrace, I have been very interested in learning more about DTrace.

The difficult thing I have been finding is that since DTrace was originally written for Sun, it is hard to find info related to MacOS and more so Cocoa.

I then found a video from Colin about using DTrace with Cocoa and XCode.
http://www.viddler.com/explore/Machx/videos/8

I wasn’t able to get what I wanted working, so I hit Colin up on Twitter and then talked to him a bit in email and he pointed me to the Golden Solution!

The solution is:
objc$target:::entry{}

So I took that and created a DTrace script.
You can grab it here:
http://kdbdallas.com/wp-content/uploads/2009/02/logallmethodsd.zip

To run it open Terminal and run:
sudo ./logAllMethods.d -p PID

Of course replacing ‘PID’ with the PID of your running program and remembering that for DTrace ‘sudo’ is required.

Also note that this works for iPhone programs running in the iPhone Simulator.
Just look for the PID of your iPhone app within Activity Monitor once it’s running in the simulator.

Be careful as depending on the program it can spit out a TON of data VERY quickly.

Running this against Adium for only a fraction of a second gave me a couple hundred lines of output.

The output looks like this:

tesseract:Desktop dallas$ sudo ./logAllMethods.d -p 4371
dtrace: script ‘./logAllMethods.d’ matched 71222 probes
CPU ID FUNCTION:NAME
0 59398 -retain:entry
0 34684 +retain:entry
0 32061 +idleAllMovies::entry
0 32062 -idle:entry
0 34707 -retain:entry
0 32914 -_usingVisualContext:entry
0 32878 -_resyncNaturalSize:entry
0 31935 -loadState:entry
0 31972 -naturalSize:entry

Hopefully this helps someone out there.
Just remember always use the info I post for good.
(or if its for evil make sure it profitable and then send me a cut)

Once again, special thanks to Colin Wheeler.

Tags: , , , , ,

White Indeterminate Progress Indicator (AKA: White NSProgressIndicator)

Posted by Dallas on December 28, 2008
Cocoa, Objective-C, Programming / 1 Comment

If you have ever wanted to use a Indeterminate Spinner NSProgressIndicator on a dark background you have probably been upset to find that there is no way to get a white spinner.
That is where WhiteIntermProgIndicator comes into play.
WhiteIntermProgIndicator, is based off of AMIndeterminateProgressIndicatorCell by Andreas at Harmless Cocoa.

Screenshot:
whiteintermprogindicator

Download: WhiteIntermProgIndicator.zip

Tags: , ,

Mac/iPhone: Show Available Usable Disk Space

Posted by Dallas on December 27, 2008
iPhone, Mac, Objective-C, Programming / 6 Comments

If you have an application that allows users to upload content to the app, you might want to show the user how much space is available for them to use.

This is especially true on the iPhone where it is not as easy for the user to quickly check if they have enough room to upload a given file.

You may also want to check if there is enough space before you start the upload, so you can inform them that the file wont fit.

That brings you to the task of figuring out how much free space there is, and more importantly how much free space is left that you as an unprivileged program can access.

The following code is based towards the iPhone, however all that would be needed to change it to be used on the Mac is the location you are checking for free space, and the setting of the label.

NOTE: The below “shown” code is old. The updated code is in the zip file at the bottom

#include <sys/param.h>
#include <sys/mount.h>
NSString *sizeType;
 
float availableDisk;
 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 
struct statfs tStats;
 
statfs([[paths lastObject] cString], &tStats);
 
availableDisk = (float)(tStats.f_bavail * tStats.f_bsize);
 
if (availableDisk > 1024)
{
	//Kilobytes
	availableDisk = availableDisk / 1024;
 
	sizeType = @" KB";
}
 
if (availableDisk > 1024)
{
	//Megabytes
	availableDisk = availableDisk / 1024;
 
	sizeType = @" MB";
}
 
if (availableDisk > 1024)
{
	//Gigabytes
	availableDisk = availableDisk / 1024;
 
	sizeType = @" GB";
}
 
diskSpaceLbl.text = [[@"Available Disk Space: " stringByAppendingFormat:@"%.2f", availableDisk] stringByAppendingString:sizeType];

This will give you something like the following:

availablediskspace

UPDATE:
I have gone a step further and made this into an easy to use class, that works out of the box with the iPhone and the Mac.

FSStats.zip

Tags: , , , ,

Cocoa: Adding an application to Login Items in Mac OS X Leopard

Posted by Dallas on September 30, 2008
Apple, Cocoa, Mac, Objective-C, Programming / No Comments

In Mac OS X 10.5 Apple added a new set of API’s for adding an application to the users login items in Cocoa.

In looking for the way to use this new API, I stumbled upon Justin Williams’ blog carpeaqua.com where Justin was nice enough to put together a code example project showing just how to use this new API.

You can grab the sample project at his public code repository http://secondgear-public.googlecode.com/svn/trunk/SGLaunchAtLogin/

Thanks again to Justin for making this available.

Hopefully this page helps with his Google position for people searching for this info.

Read his full post at: http://log.carpeaqua.com/post/27727810/adding-an-application-to-login-items-in-mac-os-x

Tags: , , , , ,