Get iPhone Device Version

He is a quick way to separate iPhone 4 / iPhone 3 code snippets.

float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 3.0) {
	// add your code for iphone 4 (retina display!)
} else {
	// add your code for iphone 3/3Gs
}

iPhone Touch Icon – Create your own Icon Set

Since the introduction to the iPhone 4, we now have some additional icons we need to create to support the Hi-Res graphics of the iPhones Retinal Display. Altogether we now have a total of 6 app icons, each with a different pixel size in order to support all devices they are as follows:

  • iPhone 4 Hi-Res App Icon ( 114 x 114 )
  • iPhone App Icon ( 57 x 57 )
  • iPad App Icon ( 72 x 72 )
  • iphone 4 Hi-Res Spotlight Icon ( 58 x 58 )
  • iPhone Spotlight Icon ( 29 x 29 )
  • iPad Spotlight Icon ( 50 x 50 )

After recently developing a few iPhone apps, I found it to be a redundant task to create 6 icons every time I make a new app. To make this process faster, I’ve created a PHP script that takes the highest resolution icon and converts it down to the 6 icons required.
You can find this script at my company website at http://empoc.com/ios-icons/
It only takes a few seconds to create all of the icons and when the script has finished running you will be able to download them all in a nicely zipped up file. I’ve also got instructions there that will show you what to do with the icons once you’ve downloaded them.
Please have a look and let me know what you think…

Alternatively you can visit make app icon to create the whole set of icons http://makeappicon.com/

Why Google Chrome?

There are some features that most ppl hasnt noticed in google Chrome. For my opinion Google Chrome will be the no1 choise of all net freaks.

Incognito mode (private browsing)

For times when you want to browse in stealth mode, for example, to plan surprises like gifts or birthdays, Google Chrome offers the incognito browsing mode. Here’s how the incognito mode works:

Webpages that you open and files downloaded while you are incognito aren’t recorded in your browsing and download histories.
All new cookies are deleted after you close all incognito windows that you’ve opened.
Changes made to your Google Chrome bookmarks and general settings while in incognito mode are always saved.
Keyboard shortcut: Ctrl+Shift+N

Advanced Search in Address Bar

Chrome uses a search engine as your type a url. So the address bar can be used for search and for inserting a url. Autocomplete is very helpful even in very specific searches.

Also Check out this screen. In options

There are default Search options but also Other search engines from Sites you use mostly (this is done for you automatically)

Quick Search

Type your search. As you type you can see the number of occurrences (current occurrences is highlighted in orange).
Check out that the search string is highlighted in the scrollbar also.

Keyboard shortcut: CTRL + F

Speed

see some past test i did with tracemonkey here

Wiscp on iPhone

First make sure you have ssh and bsd subsystem installed from installer. If you cant find the source reboot or turn off wifi and back on.

Next get WinSCP and find your iPhone’s ip by going to (on your iphone) settings > Wi-Fi > “Your selected WiFi” > Look under IP Address- Connect to your iPhone using WinSCP (user: root password: alpine) Change File Protocol to SCP.

Once OpenSSH is installed your phone will be vunerable to all users in a specific wifi subnet. Make sure to Use the WinSCP terminal window an use this command to change password:

[sourcecode]
passwd root
type in password, press return
type it in again, press return
[/sourcecode]

To avoid frequent logouts remember to change the Setting -> General -> Auto-Lock (5 minutes or Never)

Capture Save/Load/Remove Image in documents directory

NSFileManager offers a convenient way to write images to and load them from the documents directory.
If you’re frequently doing that in your project, I suggest to wrap up NSFileManager support in three simple methods:

Save/Load/Remove Image Methods

[sourcecode lang=”c”]
//saving an image

– (void)saveImage:(UIImage*)image:(NSString*)imageName {

NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.

NSFileManager *fileManager = [NSFileManager defaultManager];

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

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@”%@.png”, imageName]];

[fileManager createFileAtPath:fullPath contents:imageData attributes:nil];

NSLog(@”image saved”);

}

//removing an image

– (void)removeImage:(NSString*)fileName {

NSFileManager *fileManager = [NSFileManager defaultManager];

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

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@”%@.png”, fileName]];

[fileManager removeItemAtPath: fullPath error:NULL];

NSLog(@”image removed”);

}

//loading an image

– (UIImage*)loadImage:(NSString*)imageName {

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

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@”%@.png”, imageName]];

return [UIImage imageWithContentsOfFile:fullPath];

}
[/sourcecode]

Now, you can easily save an image like:

[sourcecode lang=”c”][self saveImage: myUIImage: @”myUIImageName”];[/sourcecode]

or load it like:

[sourcecode lang=”c”]myUIImage = [self loadImage: @”myUIImageName”];[/sourcecode]

or remove it like:

[sourcecode lang=”c”][self removeImage: @”myUIImageName”];[/sourcecode]

Capture an image

[sourcecode lang=”c”]
– (UIImage*) getGLScreenshot {
NSInteger myDataLength = 320 * 480 * 4;

// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, 320, 480, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

// gl renders “upside down” so swap top to bottom into new array.
// there’s gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y <480; y++) { for(int x = 0; x <320 * 4; x++) { buffer2[(479 - y) * 320 * 4 + x] = buffer[y * 4 * 320 + x]; } } // make data provider with data. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL); // prep the ingredients int bitsPerComponent = 8; int bitsPerPixel = 32; int bytesPerRow = 4 * 320; CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB(); CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault; CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault; // make the cgimage CGImageRef imageRef = CGImageCreate(320, 480, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent); // then make the uiimage from that UIImage *myImage = [UIImage imageWithCGImage:imageRef]; return myImage; } - (void)saveGLScreenshotToPhotosAlbum { UIImageWriteToSavedPhotosAlbum([self getGLScreenshot], nil, nil, nil); } [/sourcecode]

CLLocationManager delegate method is not called in iOS4

Weird bug-easy solution.
I did have this weird issue also when updating to SDK4.0. I couldn’t get CLLocationManager to work on simulator. This problem also existed in iPhone Simulator 3.1.3.

The solution is quite weird. Open the Airport (wifi) in your mac and the CLLocationManager will work as before fetching the Cupertino, or in my case fetching a wide area of my country.
I guess the Location is estimated by the internet ip address of your connection.

NSString Quickies


Yet another small useful collection of small code snippets for NSString.

Xcode Color Themes


This is a small collection of my personal preference of color themes for xcode programming environment. For those who are looking for a dark theme. You can chose from one of the following color themes. I’m currently using teliaz theme, it has been tested and works very smoothly.

To get

teliaz theme

run the following code in Terminal:
[sourcecode]
mkdir -p ~/Library/Application\ Support/Xcode/Color\ Themes; cd ~/Library/Application\ Support/Xcode/Color\ Themes; curl http://teliaz.com/files/apple/TeliazTheme.xccolortheme &> TeliazTheme.xccolortheme
[/sourcecode]

To get

Ego theme

run the following code in Terminal:
[sourcecode]
mkdir -p ~/Library/Application\ Support/Xcode/Color\ Themes; cd ~/Library/Application\ Support/Xcode/Color\ Themes; curl http://teliaz.com/files/apple/XCodeColorTheme.xccolortheme &> EgoTheme.xccolortheme
[/sourcecode]
“EGO” uses the font “DejaVu Sans Mono” at 12pt. Some of you may know it as “Panic Sans” from Panic’s Coda (which is a repackaged version of it). You can grab the font over at dejavu-fonts.org. If you already have Code installed, feel free to swap in Panic Sans in place of DejaVu Sans Mono.

To get

Humane Theme

run the following code in Terminal:
[sourcecode]
mkdir -p ~/Library/Application\ Support/Xcode/Color\ Themes; cd ~/Library/Application\ Support/Xcode/Color\ Themes; curl http://teliaz.com/files/apple/Humane.xccolortheme &> Humane.xccolortheme
[/sourcecode]

Now just restart Xcode, go to Preferences > Fonts & Colors, and select “one of these themes” from the color theme drop down. Hope you like it!! Happy coding…

Objective C – Basic Datatypes and NSLog


Have you been frustrated sometimes trying to debug an application and really making it worse, the output of your logs are not as desired. Complete chaos and nonsense.

Some of the concepts here will be familiar in c coding language, as objective c is just an extended c with object oriented approach. Take a quick look at this table that will surely clear things up.

Type Constants NSLog Short Explanation
char "a", "\n" %c
short int %hi, %hx, %ho %hi, %hx, %ho
unsigned short int %hi, %hx, %ho %hi,
%hx (hex notation),
%ho (octa notation)
int 12, -97, 0xFFE0, 0177 %i, %x, %o also %d can be used in objective-c
unsigned int 12u, 100U, 0xFFU %u, %x, %o as above
long int 12L, -200l, 0xfffL %lu, %lx, %lo as above
long long int 0xe5e5e5e5LL, 500LL %lli, %llx, %llo as above
long long unsigned int 0xe5e5e5e5ULL, 120ULL %llu, %llx, %llo as above
float 12.34f, 3.1e-5f %f, %e, %g %f (with 6 decimal place by default),
%e (with scientific notation), %g (personal preference most readable)
double 12.34, 3.1e-5 %f, %e, %g as above
long double 12.34l, 3.1e-5l %Lf, %Le, %Lg as above
id %p

NIBs or code? Why not both? Here’s nib2objc.


(Somehow this project seems to me so simple, that I’m sure someone has done this before. Anyway). This is my feeble attempt to bring an answer to the eternal dichotomy between those arguing about the relative benefits of creating user interfaces via Interface Builder or via pure Objective-C code: let me introduce nib2objc.