UIDevice Properties

March 2, 2011 § Leave a comment

Currently these are the most popular common UIDevice properties. It shows the device name, device identifier, device model, device system name, and device system version.

You can print these properties inside the Console for debugging.

NSLog(@”Device Name: %@”, [UIDevice currentDevice].name);
NSLog(@”UDID: %@”, [UIDevice currentDevice].uniqueIdentifier);
NSLog(@”Device Model: %@”, [UIDevice currentDevice].model);
NSLog(@”System Name: %@”, [UIDevice currentDevice].systemName);
NSLog(@”System Version: %@”, [UIDevice currentDevice].systemVersion);
NSLog(@”User Interface: %@”, [UIDevice currentDevice].userInterfaceIdiom);

If you would like to print it in a label, then you can assign an NSInteger object to the UIDevice property. Assign the UILabel text to the NSString object.

// Show Device Name
NSString *nameString = [UIDevice currentDevice].name;
nameLabel.text = nameString;

You can also print Core Foundation object in Console.

CFShow([[UIDevice currentDevice] model]);
printf(“–\n”);

Advertisements

Constant in Objective-C

March 2, 2011 § Leave a comment

This is a very tricky task to handle constants in Objective-C. The most suggested approach is to define global, public or private constants. This will work only if you don’t expect to put the same constant inside a loop.

In the header file

// Constants.h
extern NSString * const strConstant;

You can include this file in each file that uses the constants or in the pre-compiled header for the project.

// Constants.m
NSString * const
strConstant
= @"Constant";

Constants.m should be added to your application/framework’s target to link it to product.

It is suggested that the advantage of using string constants instead of #define‘d constants is that you can test for equality using pointer comparison (stringInstance == strConstant) which is much faster than string comparison ([stringInstance isEqualToString:strConstant]).

// Constants.h
#define MY_CONSTANT @"my_constant"

Or even better
// Constants.h
extern NSString * const MY_CONSTANT;
// Constants.m
NSString * const MY_CONSTANT = @"my_constant";

If you need a non global constant, you should use static keyword. Static constant is not visible outside the file.

// Constants.m
static NSString * const CONSTANT = @"my_constant";

Another quick dirty way of declaring global constants is to put constant declaration into pch file instead of .h or .m files.

To declare integer instead of string in Objective-C, you will use NSInteger. This is Apple preferred way of declaring Objective-C integer.

NSInteger const counter = 0;

However if you are going to use this method to declare NSInteger and use it inside a loop, then you are in trouble.

Warning:

Assignment of read-only variable ‘counter’

If you need to use a constant inside a IF statement and iterate the constant inside a class instance or method, you would have to use static keyword.

You can add static int outside @implement.

static int counter = 0;

– (void)showTimer {
counter += 1;
}

Adding Text Field inside UIAlertView

February 18, 2011 § Leave a comment

Adding form text field into an alert window on iOS is a popular UI pattern. It’s a very handy feature to achieve good user experience by asking a simple input without any click on the same screen. The best use is iPhone app to ask for user password and to log into iTunes app store for app purchase.

To add a form text field inside UIAlertView is tricky. Technically it is easy. However Apple reserves the right to use Private API for its own native app design. Developer would have to work more in order to achieve the same feature.

If you use these Objective-C methods to insert a Text Field inside UIAlert, your app will get rejected by Apple review team.

The following non-public APIs are included in your application:
addTextFieldWithValue:label:
textFieldAtIndex:

“3.3.1 Applications may only use Documented APIs in the manner prescribed by Apple and must not use or call any private APIs.”

I’ll share how to add Text Field without using these private API methods.

Improve Window 7 Performance on Mac OSX

June 26, 2010 § Leave a comment

It is importance to develop multiple SDK platforms that are required in both Win OS on PC and Mac OSX on Mac. Mac allows virtual machine to install and support Win OS.

Window 7:

The single most helpful thing to make your Windows 7 computer run at peak speed is to give it enough system memory (or RAM, short for random access memory). Your computer uses two types of memory: hard disk space and RAM. Windows 7 can run with as little as 512MB of RAM and an 800MHz CPU but it is at low performance.

ReadyBoost
Microsoft introduced ReadyBoost as part of Windows Vista. Essentially, it allowed users to allocate all or part of any single USB flash drive (UFD) or a Secure Digital (SD, SDHC, or mini-SD) memory card as an extension to cache memory available in system RAM.
• Vista limited ReadyBoost space to a maximum of 4GB (32-bit operating systems limitation) for both 32- and 64-bit versions. In Windows 7, 64-bit versions can allocate up to 128GB for a ReadyBoost cache. 
• Vista limited eligible memory devices to UFDs and SD cards; Windows 7 works with those devices plus Compact Flash (CF), any MemoryStick (MS, MS Duo, MS Pro, etc). Vista limited ReadyBoost to a single memory device; In Windows 7 ReadyBoost cache on multiple memory devices at the same time.
To allocate space on a memory device for ReadyBoost, right-click the drive icon in Windows Explorer, and then click the ReadyBoost tab in its Properties dialog box. The Properties dialog box on the left comes from an NTFS-formatted UFD (NTFS or exFAT are required to create a ReadyBoost cache file greater than 4GB in size), while the box on the right is from a FAT-32 formatted UFD.

Use case:

Mac OSX: 10.6.3 Snow Leopard
Virtual Machine: Parallel 5
Win OS: Window 7

The hdd value is higher than 5.9, so Windows thinks you have a SSD

With your virtual machine running:

1. go to Virtual Machine > Configure > Optimization
2. disable the Tune Windows for speed option

Start Window 7 and go to the start menu and search for services. Once services is open find Superfetch and click start.

In the Optimization menu and disable Adaptive Hypervisor as well.

At this point I manage to setup Window 7 ReadyBoost to accept USB Flash to adjust the RAM. However, ReadyBoost can’t create the cache. This is where it ends at this time.

Error
Cache Creation Failed
Unable to create the cache. There are no more endpoints available from the endpoint mapper.

UITableView Hello World

May 24, 2010 § 1 Comment

Usually writing a tutorial blog takes a lot of time and effort to document every step and explain all details. I have seen many HelloWorld app that show very little on how iPhone app and SDK work. I include the best HelloWorld tutorial and example I found.

These are the most important building blocks of iPhone App. However usually developers don’t start with new app and may be working on existing app. Sometimes developers never get to build new View Controller and/or view. You won’t remember all the details in each step. It’s nice to reference this kind of Hello World example.

It’s nice to see how a navigation-based app with minimal functionality and show how the navigation bar and button work.

UITableView Hello World

http://icodeblog.com/2008/07/26/iphone-programming-tutorial-hello-world/

Add View Controller and View and Back Button to HelloWorld

http://icodeblog.com/2008/08/03/iphone-programming-tutorial-transitioning-between-views/

Note: Remember setText is deprecated in OS 3.2 and warning will appear in this Hello World example.

Populating UITableView with NSArray

http://icodeblog.com/2008/08/08/iphone-programming-tutorial-populating-uitableview-with-an-nsarray/

I am including code snippet to show how to gain access to UIApplication delegate to return the NSArray object in UITable row.

In RootViewController.m

// Customize the number of rows in the table view.
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

// Gain access to Application delegate
HelloNavigationAppDelegate *appDelegate = (HelloNavigationAppDelegate *)[[UIApplication sharedApplication] delegate];
// Return no of rows based on no. of objects in array
return appDelegate.fruits.count;

}

// Customize the appearance of table view cells.
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”Cell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
}

// Gain access to Application delegate
HelloNavigationAppDelegate *appDelegate = (HelloNavigationAppDelegate *)[[UIApplication sharedApplication] delegate];
Fruit *f = (Fruit *)[appDelegate.fruits objectAtIndex:indexPath.row];
cell.textLabel.text = f.name;

// Configure the cell.
return cell;
}

I have rewritten the UITableView Hello World sample in iPhone SDK 3.2 and replaced the deprecated API that no longer support UITableView. You can download the latest version here

UITableView deprecated setText in iPhone SDK 3.0

May 24, 2010 § Leave a comment

By default Navigation-based app use UITableView.

In RootViewController.m, you would declare the no. of row in tableView: method. Here you increase the row from 0 to 1 to display one row.

// Customize the number of rows in the table view.
– (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return 1; // Modify from 0 to 1
}

cell.text and setText: are deprecated in iPhone SDK 3.2. It would give you warning but error in red.

So don’t use:

[cell setText:@”Next View”];

cell.text = @”Next View”;

Use cell.textLabel.text = @”Next View”;

// Customize the appearance of table view cells.
– (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

static NSString *CellIdentifier = @”Cell”;

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}

// Added
cell.textLabel.text = @”Next View”;

// Configure the cell.
return cell;
}

View entire error in iPhone SDK

May 24, 2010 § Leave a comment

iPhone SDK 3.2 doesn’t show entire error or warning within Xcode. In order to print (copy and paste) the error, you would have to take extra steps.

  • open the build window (Command-Shift-B)
  • At the bottom of the build progress view there are four buttons (from left to right) are a checkmark, a warning symbol, some dotted lines, and a popup menu for more options.
  • Third symbol, the one with the dotted lines, has a dark grey background. Toggle it to see the new view that is exposed when this button is pressed has the full text output from the build process (compiler, linker, shell script output, etc…)
  • You can use the Command-= and Command-Shift-+ to move through the warnings and errors.