Monday, July 25, 2011

Customize UISlider color


Instead of having 3 UISlider with 3 UILabels telling red, green, blue, we have 3 UISlider with custom colors. Everybody would agree that this is nicer…
To do this, just customize your UISlider this way :
[redSlider setMinimumTrackImage:[[UIImage imageNamed:@"redSlider.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
Then you just need the images. Taken (and edited) from the UICatalog Apple Template :

Thursday, July 14, 2011

How to Prevent iPhone from Sleeping

The automatic sleep timer is one of the ways the iPhone saves power. If the screen isn’t touched for a certain amount of time, it dims the screen and eventually turns it off. Although you should leave the timer on, there are times when this is not what you want (games are a good example).
To disable the timer, set the idleTimerDisabled property to YES,


[[UIApplication sharedApplication] setIdleTimerDisabled:YES];

Date Formatter Examples

NSDateFormatter

To save you some time, here are several simple examples for displaying date information:

// <strong>Output ->  Date: 10/29/08</strong>
NSDate *today = [NSDate dateWithTimeIntervalSinceNow:0];
NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
[dateFormat setDateStyle:NSDateFormatterShortStyle];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"Date: %@", dateString);
 

Notice above how the style of the output is set using NSDateFormatterShortStyle. There are additional canned formats as well such as NSDateFormatterFullStyle and NSDateFormatterNoStyle.

// <strong>Output ->  Date: 10/29/2008 08:29PM</strong>    
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MM/dd/yyyy hh:mma"];
NSString *dateString = [dateFormat stringFromDate:today];
NSLog(@"date: %@", dateString);
[dateFormat release];
 
 

Format Strings

I want to show an additional example that uses the ICU (International Components for Unicode) library for format strings.

Here is a short list of sample formats using ICU:




The format specifiers are quite straightforward, Y = year, M = month, etc. Changing the number of specifiers for a field, changes the output. For example, MMMM generates the full month name “November”, MMM results in “Nov” and MM outputs “11″.
What follows is an example to create the following date string:

Saturday November 8, 2008:

NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@&quot;EEEE MMMM d, YYYY&quot;];
NSString *dateString = [dateFormat stringFromDate:today];
[dateFormat release];
 
Here’s another example showing the current time:
9:20 AM, PST:

NSDate *today = [NSDate date]; 
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"h:mm a, zzz"];
NSString *dateString = [dateFormat stringFromDate:today];
[dateFormat release];
 

Date from String

 
While working on an iPhone application recently, I needed to convert a date read from an XML stream that was in the following format: 20081122 to a nicely formatted string for display on the device: Saturday November 22, 2008.


How to get there from here is now obvious, however, when I first encountered this dilemma the solution wasn’t apparent. The reason being, there is significant depth in the Cocoa frameworks and half the battle in becoming proficient as an iPhone developer is to have an opportunity to explore the range of APIs. Albeit the solution was right under my nose the whole time, my first pass was to take a more traditional route of trying to parse the string and rebuild a more “traditional” date format which I could use to create a date object. So, skipping all that, here’s the proper solution…
If you’ve ever worked with dates in Cocoa, chances are you are familiar with the stringFromDate method of the NSDateFormatter. For example, the code below will convert the current date to a string that looks like this: Wednesday November 26, 2008


NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
NSString *dateString = [dateFormat stringFromDate:date];
[dateFormat release];
 
The trick is two-fold, the date format string to specify desired output, and the method stringFromDate to convert the date object to an NSString.
I’m sure you can see where I’m going with this…the solution I was looking for to convert a date (stored as a string) that was in a pre-defined format (i.e. 20081122) to a date object is as simple as using the method dateFromString. The primary difference is that the format string needs to represent the current format of the date that is to be read (versus the desired output format).
The code below converts a string that represents a date to an NSString object, with the output as follows: Saturday November 22, 2008:

NSString *dateStr = @"20081122";
 
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
 
// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];

Setting Locale 

By default, the NSDateFormatter will use the locale set on the device, so no code specific locale changes are required if you want your app to display in the current locale.

However, if you need to display a date in a locale other than the current setting on the device, here’s how to do it:


// Create date object  
NSDate *date = [NSDate date];
 
// Create date formatter
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
 
// Set the locale as needed in the formatter (this example uses Japanese)
[dateFormat setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"ja_JP"]
autorelease]];
 
// Create date string from formatter, using the current date
NSString *dateString = [dateFormat stringFromDate:date];
 
// We're done with this now
[dateFormat release];
 
// Show that the locale on the device is still set to US
NSLocale *locale = [NSLocale currentLocale];
NSLog(@"Current Locale: %@", [locale localeIdentifier]);
 
// Display the date string in format we set above
NSLog(@"Date: %@:", dateString);
 
The output from the code above looks as follows:

 

How to “unfreeze” your iPhone Application

To avoid “freezing” your application, ensure that any logic that is changing your UI is performed by the Main thread. One approach to this would be to leverage the NSNotificationCenter but sometimes that is overkill. A much simpler approach is to use one of the following performSelectorOnMainThread methods on NSObject:
performSelectorOnMainThread:withObject:waitUntilDone: 
performSelectorOnMainThread:withObject:waitUntilDone:modes:

CGRect, CGSize and CGPoint

Digging into development of iPhone applications, you’ll eventually encounter references to CGRect, CGSize, and CGPoint. These references are to C structures (see this post for more information on structures). This post will provide a high-level view of what comprises CGRect and its counterparts. Here is how CGRect is defined:


struct CGRect {
CGPoint origin;
CGSize size;
};
typedef struct CGRect CGRect;
 
Going a little further, we can find that CGPoint and CGSize are defined as follows:

struct CGPoint {
CGFloat x;
CGFloat y;
};
typedef struct CGPoint CGPoint;
 
struct CGSize {
CGFloat width;
CGFloat height;
};
typedef struct CGSize CGSize;
 
And to wrap up, CGFloat looks as follows:

typedef float CGFloat;    // 32-bit
typedef double CGFloat; // 64-bit
 
Here’s what we can summarize, a CGRect defines a rectangle as an origin (x and y location) and its size (width and height), with all values represented as floats. Access to the member variables of these structures (and all structure members for that matter) is through dot syntax as shown in the example below:

CGRect bounds = [[UIScreen mainScreen] bounds];
CGRect appframe= [[UIScreen mainScreen] applicationFrame];
 
NSLog(@"mainScreen bounds: %.0f, %.0f, %3.0f, %3.0f",
bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height);
NSLog(@"mainScreen applicationFrame: %.0f, %.0f, %3.0f, %3.0f",
appframe.origin.x, appframe.origin.y, appframe.size.width, appframe.size.height);

Rename an Xcode Project

Renaming an Xcode Project
First, click on the Project menu and select Rename:




From there, fill in the new name, I’d also recommend creating a snapshot as a precaution:




Renaming an Xcode project is no longer a mysterious, troublesome endeavor.

Capture iPhone Simulator Screenshots – Revisited – iPhone Simulator Cropper

iPhone-Simulator Cropper
This tool works by capturing the screen of the simulator runnning on your system. One really slick feature is the option to create captured images in two primary formats – first, a format suitable for upload to iTunes for your application screenshots – second, capturing a screenshot that is suitable for display on a website.

The following images show the iPhone-Simulator Cropper application, as well as two sample images captured:

iTunes Connect / App Store:

Website (iPhone Device from Apple Marketing):