iPhone Programming Tricks and Traps

January 31, 2010 by rudifa

Basic Animation : set the animation properties (like duration) before setting the properties to be animated (this is where the animation is cast in stone).

Example : this fadeOut animation will last 1.5 s, as desired:

-(void) fadeOutMyImage {
 [UIView beginAnimations:nil context:nil];
 [UIView setAnimationDuration:1.5];
 myImageView.alpha = 0.0;
 [UIView commitAnimations];
}

Example : this fadeOut animation will last 0.2 s (the default), not 1.5 s as desired:

-(void) fadeOutMyImage {
 [UIView beginAnimations:nil context:nil];
 myImageView.alpha = 0.0;
 [UIView setAnimationDuration:1.5]; // WRONG, no effect
 [UIView commitAnimations];
}

How to run graphviz on the Mac

January 10, 2010 by rudifa

Install graphviz for Mac from graphviz.org.

To check the installation, open a Terminal and type : dot -h


$ dot -h
dot: option -h unrecognized

Usage: dot [-Vv?] [-(GNE)name=val] [-(KTlso)<val>] <dot files>
...

OK, so dot was installed and added to your $PATH.

Next, create a dot source file and launch dot : dot -T png -O my1st.dot

For example :

digraph d {
 A [label="Hello"]
 B [label="World"]
 C [label="Everyone"]
 A -> { B C }
}

This will create a new file my1st.dot.png that looks like this :

For more about graphviz, look at the gallery and other resources on the graphviz website, or google for more.

OpenTerminalHere with wd in title

December 18, 2009 by rudifa

How to open a Mac Terminal application from Finder, in its current directory?

Get the app OpenTerminalHere by Marc Liyanage and add its icon in the Finder toolbar. Click on the icon to open a Terminal.

How  to make that Terminal display the working directory in its window title?

Modify the OpenTerminalHere like this :

  1. in Finder, right-click on OpenTerminalHere.app and select Show Package Contents
  2. drill down to OpenTerminalHere.app/Contents/Resources/Scripts and click on main.scpt – this opens the AppleScript editor
  3. just before the line “end process_item” at the end of script insert this line: tell application “Terminal” to set custom title of window 1 to the_path
  4. Compile and Run – it should open a Terminal that displays the path in its title

Bonus: when you right-click on the Terminal’s Dashboard icon, each terminal listed displays the working directory, end of guessing which is which.

How  to make that Terminal display the working directory in its window title? — take two –

As Marc Liyanage, the author of OpenTerminalHere pointed out to me, my change to his script sets the window title at the time of opening the Terminal, but it does not react to subsequent changes of working directory. He suggested putting a suitable ecape sequence into ~/.bashrc. After some man reading and experimenting I settled to the following solution :

  1. Leave the OpenTerminalHere unchanged
  2. Add to ~/.profile

export PROMPT_COMMAND='echo -ne "\033]0; ${PWD}\007"'

This gets executed on every command, and keeps the window title in sync with any change of the working directory.


Internationalization of my iPhone app

December 12, 2009 by rudifa

How does the user change the language of the application?

It looks like he can only change the language of the entire iPhone (Settings -> General -> International -> Language).

After the change, an app internationalized for that language will respond in it.

How does the user change the language of the application? – take two -

It turns out that an application can give the user a choice among languages that it supports,

say “en”, “fr”, “de”, “jp”.

When the user selects one of these codes (in the app’s own Select Language menu), the app should do

 [[NSUserDefaults standardUserDefaults] setObject:[NSArray arrayWithObjects:languageCode2LetterString, nil] forKey:@"AppleLanguages"];

and instruct the user to stop and to restart the application.

The application will now come up with the newly selected language.

How do I set up an Xcode project for internationalization ?

  • replace strings in the program like
label.text = @"Hello world.";

by

label.text = NSLocalizedString(@"Hello world.",
@"This is a comment about my greeting.");
  • add directories en.lproj, fr.lproj, de.lproj e.t.c.
  • run : genstrings -o en.lproj Classes/*.m
  • add the generated en.lproj/Localizable.strings to Xcode project resources (save as UTF-16!)
  • copy en.lproj/Localizable.strings to fr.lproj/Localizable.strings and edit it to provide translations
  • add fr.lproj/Localizable.strings to project Resources
  • build
  • test

OK, there is more to it but this is a start.

Profiling an iPhone application with Shark

September 16, 2009 by rudifa

Here is a quick checklist

  1. Build app and launch on the device
  2. Launch Shark (perhaps from Spotlight)
  3. From the Shark menu, select Sampling->Network/iPhone Profiling
  4. In the Shark window, select the radio button “Control network profiling of shared computers”.
  5. Select your iPhone in the list and optionally configure the profiling session
  6. Check “Use:” next to your iPhone (the checkbox may take several seconds to respond)
  7. Choose the app running on your iPhone from the “Target” drop down.
  8. Press Start to begin profiling, launch the app activity that you wish to profile, press Stop to end profiling
  9. Be patient while Shark transfers the data  to the Mac (this takes longer than the profiling)
  10. In the Time Profile window that Shark opens, select View : Tree (Top-Down)

Tree view provides a very good insight into the percentage of time spent in functions/methods.

Look for those that seem unjustifiably high.

Save the session data for future reference, try to speed up the code and redo the profiling.

Happy hunting!


Installing iPhone dev tools on the MacBookPro

August 20, 2009 by rudifa

Installed Firefox and its Google toolbar – OK

Configured the trackpad to respond to taps.

Mac wants to update 13 items, including the OS, to 10.5.8 – OK

Use the Mac Migration Assistant to copy over the user account and files from the old Mac – OK

Install iPhone SDK 3.0 for Leopard – download 30 min, install 10 min – OK, builds an iPhone  project and runs it in Simulator

Try to run the iPhone project on the device – fails because the device is at 3.0.1 – must add a symlink as explained in iPhone OS 3.0.1 Advisory :

ln -s /Developer/Platforms/iPhoneOS.platform/DeviceSupport/3.0\ \(7A341\) /Developer/Platforms/iPhoneOS.platform/DeviceSupport/3.0.1

Now the device is recognized by the Xcode Organizer, and the run on device succeeds – OK

Next, install OpenTerminalHere and add it to the Finder toolbar – opens the terminal in Finder’s current directory – OK

Install git from git-1.6.4-intel-leopard.dmg – OK

Open terminal in one of migrated iPhone project directories and try

$ git status

OK

Install gitx – the git GUI for Mac OS X – from gitx v. 0.6.3 – OK

“After you have started it once, you can install the command-line tool through the menu (GitX->Enable Terminal Usage…). This will install a “gitx” binary in /usr/local/bin.”

It did. Now I can invoke gitx from terminal in a project directory – OK.

Is ProjectLocker responding to git ?

Try,  in a project directory that was not up-to-date with the PL repository :

$ git push origin master

OK, the update worked.

Install clang from Clang Static Analyzer 0.2.1.5 : it unarchives into the directory checker-0.215.

Move this directory into /Applications/Utilities and make symlinks :

$ ln -s /Applications/Utilities/checker-0.215 /Applications/Utilities/checker

$ ln -s /Applications/Utilities/checker/scan-build /usr/local/bin/scan-build

$ ln -s /Applications/Utilities/checker/scan-build /usr/local/bin/scan-view

Try

$ scan-build

OK, responds with the help message.

Git, Xcode and ProjectLocker

May 19, 2009 by rudifa

Here is a checklist for setting up git for Xcode projects on a Mac.

1- install

Install Git from git-osx-installer

Install Gitx from gitx

Launch Gitx and invoke Enable Terminal Usage form Preferences

Read up the git basics

2- set up git for an Xcode project :

cd <projectdir>

git init

This creates the subdirectory .git

git status

Create file .gitignore :

#see http://shanesbrain.net/2008/7/9/using-xcode-with-git
# xcode noise
build/*
*.xcclassmodel/*
*.pbxuser
*.mode1v3
*.mode2v3
*.perspectivev3

# old skool
.svn

# osx noise
.DS_Store
profile

Create file .gitattributes :

#see http://shanesbrain.net/2008/7/9/using-xcode-with-git

*.pbxproj -crlf -diff -merge

We could continue in Terminal using git commands (add, commit)

but we can now launch Gitx from Terminal :

gitx

This opens the GUI view on files to be added and comitted – see the gitx documentation for details.

Use gitx to add all Xcode files in the project and to commit them.

3- create a remote repository at ProjectLocker

Sign up at ProjectLocker for a free account (git)

Generate a public key with

ssh-keygen -t dsa

and paste it to ProjectLocker when requested.

ProjectLocker will provide a Git Location similar to git-your_user_name@free2.projectlocker.com:your_first_project_name.git

Back in Terminal in the Xcode project directory, tell git about the remote location, then pull the (for now empty) remote repository, and finally push the local repository :

git remote add origin git-your_user_name@free2.projectlocker.com:your_first_project_name.git

git pull origin master

git push origin master

Now the remote repository should contain a copy of the local repository.

4- add projects to the ProjectLocker (PL) repository

You need to create a PL project for each local git project that you want to replicate to the PL.

You log in to PL and Add Project … very simple.

Just don’t forget to add yourself as User to the newly created project, otherwise you won’t have the access to it from your local terminal via git.


Linux .so files

March 31, 2009 by rudifa

Here is a tutorial

Linux .so files

March 31, 2009 by rudifa

Here is a tutorial

http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html

Backing up

March 5, 2009 by rudifa

Archive a directory
tar -zcvf 20090204.tar.gz .git

Unarchive it
tar -zxvf 20090204.tar.gz

Should find a way to email it to a safe location automatically