Programming

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: , , , , ,

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: , , , ,

Things Apple Left Out (toolchain)

Posted by Dallas on July 10, 2008
iPhone, Programming, Toolchain / No Comments

Lately there has been a lot of talk about the Apple iPhone SDK.

Well some of us while working on the SDK are still also working on the Original SDK, AKA the Toolchain.

Some might ask, “why if there is now the SDK would you still work on the toolchain?”

Two reasons:

  1. Maintaining existing toolchain applications
  2. The SDK is VERY restrictive on what you can and can’t do, and if you break one of those rules then Apple won’t let you into the AppStore and you can not distribute your application.

So I have been updating some of my applications recently and I keep running into the items that Apple forgot to add to the iPhone toolchain SDK.

For example XML processing was forgoten, as well as several parts of NSFileManager (like copying).

All of these I have had to fight with not having.

Well tonight I found a new one…

Seems Apple also forgot to include a LOT of the details contained in NSUserDefaults, not to mention that the few items that they did include are not set for the user root.

User root not having any defaults I can kind of understand, as with the later firmwares applications are not run as root anymore, but instead the user mobile.

While making some sense, it does not make total sense as some programs are still ran as root (such as daemons) (which just happens to be what I am dealing with).

So after jumping through the hoops of not having all the normal User Defaults while setting up the part of my application that runs as the user mobile to be able to have international/locale date support, I am now at a road block with getting the daemon part of my application which runs as root, to have the same support.

From what I can see even in the normal Cocoa SDK there is no way to get another users defaults (even if you have permission, ie: you are root). Maybe I am missing something as this is the first time I am dealing with international/localization programming.

If you happen to know how I can do this, please let me know…

Tags: , ,

Reached A New Development Milestone

Posted by Dallas on June 24, 2008
iPhone, Programming / No Comments

I have reached a new milestone in my work as a programmer.

What is that milestone you ask…

My software is being cracked and distributed on the net by way of warez sites.

This isn’t really a good thing, since all the people that steal it aren’t paying for it, and therefore taking money from me, but at the same time it is kind of cool.

OK, that’s long enough… STOP STEALING MY STUFF!!!!

:-)

Tags: