Set the DEBUG flag in XCode 4

Most of us use something like “#ifdef DEBUG” to log things in debug mode. In XCode 4, the way to set this flag is in the Build Settings, under LLVM GCC 4.2 – Preprocessing. (If you’re using another compiler, adjust accordingly). Screenshot:

You also need to make sure you are building in debug mode. That is a setting in the “scheme.” In the upper left of XCode 4.xx click the name of your app next to the “run” and “stop” buttons:

In the menu that comes up, click “edit scheme.” That will bring up the settings for the scheme you are building. There is a setting called “Build Configuration” that should be set to “Debug”….

Posted in Uncategorized | Leave a comment

When you add files to git before creating .gitignore

You just added filesfor git to track. Suddenly you remember there are 77 files used by your IDE to track where your mouse cursor last rested (or something equally baffling). These files are useless to you. Yes, you are probably using XCode. Here’s how to reset the repository so that it only tracks files that are NOT in .gitignore, WITHOUT manually removing the files one by one or writing an inscrutable bash script:

git rm -r --cached .
git add .
git commit -m "that was easier than I thought."

PS: This is intended for a newly set up project. I don’t know if you want to do it with something you’ve been tracking over many revisions.

Posted in Uncategorized | Leave a comment

Hide status bar on iOS app – moments of amnesia

In info.plist make an entry UIStatusBarHidden and check it for YES.
Or in code, do

[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
Posted in Uncategorized | Leave a comment

Firefox for web cams in 64 bit Linux

If you have pixelated, grainy or boxy video using 64 bit Flash, here’s a command to start Firefox that should do much better:

bash -c “LD_PRELOAD=/usr/lib32/libv4l/v4l1compat.so firefox”

Then configure a launcher to use the better settings.

Posted in Uncategorized | Leave a comment

XCode Template Macros Cheat Sheet

The default XCode templates are in /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Project Templates/
Substitute /MacOSX.Platform/ for the OSX templates.
Your own custom templates can be put in (notice tilde) ~/Library/Application Support/Shared/Xcode/ProjectTemplates/
Just create the directory if it’s not there, and copy/imitate the structure of the default templates (there are plists and such for configuration).

Now here’s a cheat sheet for renaming files and bits of code. This is also useful if, like me, you find yourself converting a project into a template after the fact.

Template macros for File names and text inside an Obj-C or other code file:
___PROJECTNAME___
___PROJECTNAMEASIDENTIFIER___
___FULLUSERNAME___
___DATE___
___YEAR___
___ORGANIZATIONNAME___

Variable names for info.plist and similar places:
${PRODUCT_NAME}
${PRODUCT_NAME:rfc1034identifier}
${EXECUTABLE_NAME}

Posted in Uncategorized | Leave a comment

XCode Renaming Detritus

This pertains to XCode 3.x., which I just rolled back to because XCode 4 has been disasterous for me.

Today I converted a project into a template project and that required changing all the names to something like ___PROJECTNAME__. More about that in another post. But I discovered that even when you’re careful, there are some hidden things that don’t get changed while editing manually or using XCode renaming.

The file that was a problem for me was ___PROJECTNAMEASIDENTIFIER___Prefix.pch . Everywhere I could see it was correctly named, and XCode could not find any reference to its old name, but GCC would throw a “can’t find oldprojectnamePrefix.pch” error (which just states “failed with error code 1″ if you’re not looking carefully).

So here was the solution for me. If you “show contents” on the XCode project file (in finder by right-clicking, or through command-line) there is a file called project.pbxproj. Open it and search for anything that’s left over from a project rename or conversion to template. Chances are it’s in here. I recommend closing XCode first (otherwise there’s a delay in XCode realizing that the file has changed) and doing a find and replace.

Posted in Uncategorized | Leave a comment

OH… architecture matters

The OS X migration assistant is OK, really. But it’s good to remember that when you upgrade from an i386 to an i686 (or any other architecture), some of your stuff will get transferred but not work. If you have a lot of *nix dev tools, some of them are probably present but broken. If you’re lucky they’ll echo ‘wrong architecture’ or something similar.

Posted in Uncategorized | Leave a comment

Building a list of resource filenames programmatically

Here is a simple way to index art and sound resources for your application. Use case: I find myself manually typing file names into a plist, so that the app can load certain images/sounds into memory.  Also, when I add files in the future, I don’t want to add them manually to the plist (or I might forget to add them and get unexpected behavior). So here is the init method for a class (a singleton in this case), that searches the app bundle for specific resources. The class is called SpriteLoader:

- (id)init {
  if ( self = [super init] ) {
    // get the application bundle, then put all the filenames from the desired subdirectory into an array (see note about subdirectories)
    NSArray *artFileNames = [[NSBundle mainBundle] pathsForResourcesOfType:@"png"  inDirectory:@"GeneralSpriteList"]; // returns NSArray of NSString objects
    // optional code: if NSBundle takes a long time to find files, you might want to only search once ever, once per launch, once per app update, etc.
    // NSUserDefaults * theDefaults = [NSUserDefaults standardUserDefaults];
    // [theDefaults setObject:artFileNames forKey:@"GeneralSpriteList"];</code>

    // now init our class dictionary. For this class, we want it to contain actual image files for the strings we collected, plus reasonably intelligible keys.
    sprites = [[NSMutableDictionary alloc] init];
    int index = [artFileNames count] - 1; // last index + 1
    if (index == 0) { DLog(@"WARNING: Nothing in sprite array! Crash in 3...2...1...Aaaaaa!") }
      while (index > 0) {
        index -- ;
        UIImage *thisSpriteImage = [UIImage imageWithContentsOfFile:[artFileNames objectAtIndex:index]];
        // now cut out the file url or path to get just the filename
        NSString *thisString = [[artFileNames objectAtIndex:index] lastPathComponent];
        // store the actual image in the "sprites" dictionary, with key of same name BUT without the file extension
        [sprites setObject:thisSpriteImage forKey:[thisString stringByDeletingPathExtension]];
      }
    }
  return self;
}

Here’s some debugger output to show it is working:

(gdb) po sprites
{
"weapons-01" = "<UIImage: 0x11be70>";
"weapons-02" = "<UIImage: 0x11be71>";
"weapons-03" = "<UIImage: 0x11be72>";
}

Careful, this dictionary could become violent if provoked.

Now whenever I need a texture file from [[SpriteLoader sharedSpriteLoader] sprites] , I will know that I have the most up-to-date collection of sprites :) There are lots of other things that can be done from here. For example, if you had a long animation, you could place all the frames in a directory and let NSBundle index them for you. Make sure the strings are sorted, save an array of sorted paths to defaults (perhaps only the first time the program loads), and use it to load the array that iterates through your animation frames.

What we’ve done here is intended for collections of art and sound resources, but can be extended to support pre-indexed atlas sprites, or perhaps composite sound files.

By the way, I’m really sorry about how the code formats in my WP editor. I will try to fix this in the future.

Now… NOTE ABOUT SUBDIRECTORIES: If you use XCode, the groups you see in the IDE do not correspond to directories in the app bundle. The solution is to drag in a folder, and check the radio button stating: “Create Folder References for any added folders“. Once done, this will cause a blue, not yellow, folder to manifest itself in XCode. Files in that folder will be in a subdirectory of same name in your app bundle! If you don’t do this, the files will be at the top of the bundle and you won’t be able to use the technique described in this post.

EDIT: And further note about folders… It is worth noting that NSBundle, UIImage imageNamed: , and others might not find your files once you take control of directory structure. It’s useful if you’re indexing all the images as in the post above, but it would be annoying if you frequently use:
UIImage *theImage = [UIImage imageNamed:@"myImage.png"];
because UIImage would most likely return nil. In this case, you have to be more specific:
UIImage *theImage = [UIImage imageNamed:@"MyGreatDirectory/myImage.png"];
for UIImage to return the file you want. Here’s some GDB output with further examples of what does and does not work once you take control of the directory structure:

// doesn't work
(gdb) po [[NSBundle mainBundle] pathForResource:@"weapons-03" ofType:@"png"]
Can't print the description of a NIL object.
// works
(gdb) po [[NSBundle mainBundle] pathForResource:@"GeneralSpriteList/weapons-03" ofType:@"png"]
/var/mobile/Applications/06CDAEF9-040B-4AF1-9676-C13AEC1C96AB/MyViolentApp.app/GeneralSpriteList/weapons-03.png
// doesn't work
(gdb) po [UIImage imageNamed:@"weapons-03.png"]
Can't print the description of a NIL object.
// works
(gdb) po [UIImage imageNamed:@"GeneralSpriteList/weapons-03.png"]
<UIImage: 0x11be72>
(gdb) ...
Posted in Uncategorized | Leave a comment

The application-identifier entitlement is not formatted correctly

This is not well documented by Apple, as far as I am aware. If you get an error stating “The application-identifier entitlement is not formatted correctly” when uploading an app to Apple, here is the solution.

  1. Delete your old Entitlements.plist if you have one.
  2. Create a new file of type “code signing,” “entitlement.”
  3. Clean and build.

There are two new “identifier” fields in the entitlement certificate that were not present before. It has nothing to do with the “get-task-allow” field you’re used to fiddling with for ad-hoc builds. May the upload gods bless your new binary.

Posted in Uncategorized | 2 Comments

XCode Screenshot Location

Because I can never find this when I need it, here is the location where XCode stores screenshots:

~/Library/Application Support/Developer/Shared/Xcode/Screenshots

To take screenshots for the iPhone, iPad, or iPod Touch, go to Window –> Organizer and click on the device within Organizer. You’ll see a tab titled “screenshots.”

You can also drag and drop the screenshots from the Organizer to Desktop or a folder of your choice.

Posted in Uncategorized | Leave a comment