Friday, August 6, 2010

Copy-Paste Code: Opening up Settings.plist

So you've bundled a Settings.plist, and now you want to know why you can't edit it? It's because it's not in a writable directory. Let's put that baby where she belongs:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

settingsPlistPath = [[documentsDirectory stringByAppendingPathComponent:@"Settings.plist"] copy];



NSFileManager *fileManager = [[NSFileManager alloc] init];



if (![fileManager fileExistsAtPath:settingsPlistPath]) {

    NSString *bundledSettings = [[NSBundle mainBundle] pathForResource:@"Settings" ofType:@"plist"];

    [fileManager copyItemAtPath:bundledSettings toPath:settingsPlistPath error:NULL];

}



self.settingsDictionary = [[NSMutableDictionary alloc] initWithContentsOfFile:settingsPlistPath];

A Quick Way to Syntax Highlight Code

How to make code pretty before posting: it's easier than you think

Some people would prefer spending the time to set up a script that will highlight their code for them. There definitely advantages to this approach. Not wanting to spend the time and resources to set something like this up (as simple as it seemed to me to be), I opted for a google search: syntax highlight objective-c code online. There are plenty of options out there, but http://quickhighlighter.com/ is the one I liked the best. It has one drawback: you have to copy and paste two bits of HTML into your blog instead of one (it separates the css).

There are plenty of options to choose from, and you can even copy and paste the highlighted text into blogger, but (at least with Google Chrome) you loose the pretty gray box the code comes in.

Monday, August 2, 2010

Finding Memory Leaks - the easy way

From the iPhone Development Guide, "Debugging Applications"


Finding Memory Leaks

If you fix a leak and your program starts crashing, your code is probably trying to use an already-freed object or memory buffer. To learn more about memory leaks, see "Finding Memory Leaks".

You can use the NSZombieEnabled facility to find the code that accesses freed objects. When you turn on NSZombieEnabled, your application logs accesses to deallocated memory, as shown here:

2008-10-03 18:10:39.933 HelloWorld[1026:20b] *** -[GSFont ascender]: message sent to deallocated instance 0x126550

To activate the NSZombieEnabled facility in your application:

  1. Choose Project > Edit Active Executable to open the executable Info window.
  2. Click Arguments.
  3. Click the add (+) button in the “Variables to be set in the environment” section.
  4. Enter NSZombieEnabled in the Name column and YES in the Value column.
  5. Make sure that the checkmark for the NSZombieEnabled entry is selected.
For more information about configuring executable environments, see “Configuring Executable Environments” in Xcode Project Management Guide.

iPhone 3.1 SDK introduces the Leaks instrument, which you can use in the Instruments application to easily find memory leaks. For more information, see Instruments User Guide.

© 2010 Apple Inc. All Rights Reserved. (Last updated: 2010-05-28)

Saturday, July 31, 2010

Downgrading iPhone OS

Yes, sometimes you need to. Like if you have an iPhone 3G and you install OS 4 on it. And you don't like how unresponsive the OS got. Well, here's an excellent how-to. Don't forget the step with RecBoot - it's worth downgrading your phone just to see the message RecBoot gives you when you're done!

Saturday, July 24, 2010

SQLite: Easy as 1 2 3 ...4 (Practically Painless)

Yes, SQLite is an excellent SQL solution for the iPhone.

So, how to add support to a project?

I'm assuming that you have an SQLite database already. If you don't, try SQLite Database Browser. It isn't perfect, but it works well enough, and you can use it to create and edit tables. I also assume that you know how to use what SQLite retrieves in your iPhone project. For a more complete guide, you might try this tutorial, but I like to keep my posts focussed. Eventually I'll take the time to learn the pros and cons of using Core Data - I've been led to believe it can do some of the same things that SQLite can do (it can even write to a file in SQLite format) - but until I understand it better I'll stick with what I know: SQL.

Adding the SQLite Framework to your project 

It's as easy as 1 2 3 ...4...

  1. Right-click on Frameworks
  2. Click Add -> Existing Frameworks
  3. In the drop-down, select Dylibs
  4. Select libsqlite3.0.dlib and click Add
Accessing your SQLite database

I'm using fmdb to access the database (it has an MIT license), rather than write my own object for accessing the db. I've copied over the project into a folder I aptly named "FMDB". From there on it should be calm seas! FMDB even comes with a "fmdb.m" file that can be used to see the code in action.

Saturday, July 10, 2010

Installing Bazaar on Bluehost

I've decided to move my content conversioning system (CVS) off of my local system to I don't have to worry so much about my apartment burning down. I've got a bluehost server, so it seems like a good place to put it. Of course, that also means I have to set it up myself, since bluehost doesn't have a script to do it, and naturally I couldn't find a guide anywhere to do it - so I decided to write one myself.

I'm assuming in this guide that you have access to a brain, and a shell session to your bluehost box. I like to use putty myself, dirt simple. Yes, I'm lazy, so I didn't compile this information into a script - it'd be a great little project for someone!

Grabbing the Source
First thing's first: I'm going to need to install from source, because I don't have access to the CentOS's yum package installer. I grabed the latest stable release, bzr-2.1.2.tar.gz, from https://launchpad.net/bzr/+download. It's nice to know that every release the bzr team has put out is available there for download.

Installing Dependencies
On to the next step: dependencies. The Bazaar install instructions indicate a number of dependencies that I'll go through as needed. Bazaar uses python, and bluehost doesn't permit users to install python eggs in the default location, so there's some prep work required beforehand. You'll need to upload all the files to your bluehost server using ftp cPanel, or just use wget.

     Preparing your PATH (per bluehost instructions)

  1. Add the following to .bashrc (I had to revise "python" to "python2.4":
    1. export PYTHONPATH=$HOME/.local/lib/python2.4/site-packages:$PYTHONPATH
    2. export PYTHONPATH=$HOME/.local/lib64/python2.4/site-packages:$PYTHONPATH
    3. export PYTHONPATH=$HOME/lib/python:$PYTHONPATH
    4. export PYTHONPATH=$HOME/lib64/python:$PYTHONPATH
    5. export PATH=$HOME/.local/bin:$PATH
    6. export PYTHONPATH=$HOME/[path to bzrtools directory, e.g. $HOME/.bazaar/plugins]:$PYTHONPATH
  2. Log off
  3. Log back on
Dependencies: Installing Python Eggs on Bluehost
  1. Un-tar the egg
  2. cd into the egg directory
  3. python setup.py build
  4. python setup.py install --prefix=$HOME/.local
    Dependencies


  • Python >= 2.4
    • python -V yields Python 2.4.3, so I'm good there.
     Eggs (use python -c "import [egg name]" to test installation - no response is good)
Installing Bazaar
And now, finally, run make, then python setup.py install --home $HOME. Kind of anticlimactic, really.ls ~/

bzrTools
To instal bzrTools, simply grab the version that corresponds to your version of bzr, extract it, and copy over the bzrtools dir to the path you set up in the section titled preparing your PATH.

Monday, July 5, 2010

ZipKit: Compress and Extract Zip Files Programatically

My Problem:
Download and store a web page that could have multiple resources embedded into the html (e.g. links to pictures, css files, etc.). Why is this a problem? Because I have discovered that there is no way to save a web page programmatically in the iPhone SDK (at least using the 3.x OS). I could download and save each resource separately, but that means parsing the HTML to find each resource, download the resource, and then replace any absolute URLs with relative URLs in every file. A pretty gnarly task indeed.

The Circumstances:
Fortunately I have one thing on my side: I can dicate how I want the web page to exist. That's pretty huge: for example, I can require that it is packaged into a zip file, so I only need to download one file instead of hunting through the html to figure out what all needs to be downloaded.

The Solution:
A nifty library called ZipKit.

There are a couple of considerations that I will have to experiment with before I am satisfied with this solution:

  • How long does it take to unzip the types of zip files I am expecting?
  • If it does take a significant amount of time, how does that compare to the amount of time saved by downloading a compressed (read smaller) zip file?
  • Would a tar file work better, since it does not compress files - you have the full download time of the uncompressed files, but potentially a faster extraction speed (the OS doesn't have to run through a decompress operation)?
  • Can I read directly from the zip/tar file without extracting the files?
Of course, if someone has a suggestion for a better solution to my problem, I am all ears!

Thursday, May 27, 2010

Registering in the Apple Developer Program


Phase 1 - the preliminary harvesting of personal information

Enrolling in the Apple Developer Program seems like an excellent place to start, since it is the beginning. I'll be enrolling a Company for the purposes of this blog; enrolling an individual should be simpler. Starting here (in the column labeled New Apple Developer), choose to create a new account for an Apple Developer Program.

As apple explains on the enrollment page, you'll need a few things:


  • Legal Company/Organization Name.
  • The legal authority to bind your company to any legal agreements that may be presented to you during the enrollment process or your program year.
  • To provide us business documents including, but not limited to: Articles of Incorporation, Business License, etc. as part of our identity verification process.

We'll see how complete that information is once we get started.

Ok, since I'm enrolling a company, I choose the button labeled "Company". Whoops:
This isn't that big of a deal. It's annoying when it happens, and it does occur at an alarming frequency for a professional site! Still, I can solve it by simply going back and clicking "Company" again. I just hope it doesn't happen when I'm submitting the information for my app!

Ok, here's a checklist of fields that will need filling out:

Apple ID
  • Desired Apple ID
  • Password (you knew it was coming)
Security Information
  • Birthday (OK, that one came out of the blue. I'm registering for a company)
  • Security Question
  • Answer
Personal Information
  • First Name (Make sure to use a real name! No aliases or organization names, or the application may run into trouble. I guess Apple really wants to have a contact.)
  • Last Name
  • Email Address
  • Company / Organization
  • Country
  • Street Address
  • City
  • State
  • Postal Code
  • Phone

On to the second page! There's a few questions here that I think are more for Apple's benefit than anything else:
  • Which Apple platforms do you develop with? Select all that apply.
    • iPhone OS
    • Mac OS X
    • Safari
  • What is your primary market? (I won't bother to enter the choices here)
  • Check this box if you are currently enrolled in a college or university.
As soon as you click a platform more questions pop up - this page looked deceptively short. These questions are the same type as before, except they are mostly future contingent questions and therefore cannot be answered truthfully (as every philosophy student knows).

Now, after all that is done, Apple sends an email to the address you provided. Once you click on their link, you've officially registered as an Apple Developer! However, that's not what we were trying to do: we want to join the developer program, so that we can submit apps to the iStore. This is definitely not a one-step process - but at least phase 1 is complete. Congratulations - you (not your company) registered as a developer with Apple!


Phase 2 - gathering the details on your company

Back on page 1, this time we can choose the option in the "Existing Apple Developer" column that says we're already a developer with Apple and would like to enroll in a paid Apple Developer Program. Then we get to hit the button that says "Company" again. Now we can start filling in information on the company:

  • Company Name
  • Website
Contact Information
  • Country
  • Street Address
  • City/Town
  • State
  • Postal Code
  • Phone
After we're through with that, we can get into the legal part of the registration process. Apple conveniently offers you a form to fill out for the legal contact of the company that is able to verify that you have the authority to bind the company to any legal agreements that are presented to you during your stay with Apple. Of course, if you are the legal contact for your company, there's an option just for you. The form fields follow; all of them are required:
  • First Name
  • Last Name
  • Title
  • Phone
  • Email
After that you can select the program you are applying for, verify your information, and submit! You will end up with a screen giving you your enrollment ID and next steps:

Next steps: Company verification process

  1. We will be verifying your company's identity. If necessary, we may ask for additional information in order to complete the review of your enrollment. We appreciate your patience as this process may take several days.
  2. We will be contacting the legal representative you provided to verify that you have the legal authority to bind your company to any agreements that may be presented to you as part of the enrollment process or during your membership.
  3. You can check the status of your enrollment at any time by visiting the Member Center.
  4. Once we have completed the review process, you will receive an email from Apple Developer Support with further instructions.
Phase 3 - purchasing
Part three to follow...

Tuesday, May 18, 2010

A New Blog Has Been Born

Purpose and Philosophical Perspective

There are thousands and thousands of blogs out there, and to many this is "just another blog". Many more will never know this blog exists. But it is the same when a new person is born, and that does not diminish the value of a new life in the least! This newborn blog may die alone and unappreciated, or live to a ripe and sagacious old age, but one thing is certain: it will be unique.

The purpose of this blog is to archive my knowledge of iPone apps and other programming adventures that I may have in the future. I would be honored if other iPhone developers would share their experiences, offer suggestions, and perhaps even praise on the topics that I write. Hopefully I'll be able to add articles on iPad and Android apps to this blog as well, but time measures the length of all endeavors in this life, and so I must submit to her.

Happy Reading!