Archive for the ‘iPhone’ Category

Adding Placeholder Text to UITextView

Wednesday, December 8th, 2010

I decided one of my apps should display placeholder text in a text view. Unfortunately UITextView does not have a placeholder property. I’ve always thought this is odd especially given the fact that UITextField has a placeholder property.

I googled to see what others have done. I found some good approaches but no one solution that I liked. So I decided to write my own borrowing from the various ideas of others. The end result is KTTextView.

KTTextView derives from UITextView and enhances it with new features. At the moment the only new feature is the placeholder text. The placeholderText property works same as UITextField’s placeholder property. I also added the property placeholderColor in case an app has the need to use a color different then the default placeholder color.

KTTextView is hosted on github. The project includes a sample app showing how to use KTTextView. I plan to add more features as time allows. One feature I want to add is an option to display the text view in a rounded rectangle giving it a similar look to UITextField. In the meantime, I hope you find the KTTextView’s current implementation useful.


Customizing the Look of UINavigationBar and UINavigationController

Saturday, September 11th, 2010

CustomNavBar.png I’ve recently worked on a number of iOS projects that require a custom background for the UINavigationBar including the navigation bar used by the UINavigationController. Customizing the look of a UINavigationBar is actually pretty simple. You can set the tintColor property to the color of your choice. But sometimes you need to do more. For example, say you want to have a title in your navigation bar that uses a font not available on iPad and iPhone. For this you will need to use an image to customize the navigation bar.

Let’s take a look at the various ways to customize the navigation bar.

The UINavigationBar has a style property called barStyle. barStyle can be set to UIBarStyleDefault or UIBarStyleBlack. The default uses a blue gradient background. When using UIBarStyleBlack you can also set the translucent property to YES to give the navigation bar a partially opaque look. If you are using Interface Builder, then you will set the Style property in the Inspector window. Here you have the option to style the navigation bar as Default, Black Opaque, and Black Translucent.

What if you want a color other than blue and black? You can set the tintColor property (aka Tint in IB) to any color you like. This changes the navigation bar’s color. Best of all, setting the tintColor will also change the background color used by the bar button items displayed within the navigation bar.

Sometimes, however, just changing the tint of the UINavigationBar isn’t enough. Sometimes you want to have a fancier background. Maybe the background you want has a different gradient style or lighting effect, or maybe you want a title displayed in the center of the navigation bar to use a font not available in iOS. How would you change the look of the navigation bar for these scenarios? Simple. You use an image that is drawn on the navigation bar.

There are two approaches you can use to draw an image on the navigation bar. You can create a new class that inherits from UINavigationBar and override the drawRect method. For example:

@interface KTNavigationBar : UINavigationBar {

}
@end

@implementation KTNavigationBar

- (void) drawRect:(CGRect)rect
{
   [super drawRect:rect];

   UIImage *image = [UIImage imageNamed: @"navbar.png"];
   [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

In this example, the image navbar.png is used to customize, or skin, the UINavigationBar. This is a nice, clean, object orientated way to customize the look of your navigation bar. The only thing you must remember to do is to use your custom UINavigationBar class within your application. This means changing the class type for those of us using IB.

Another trick you can use that doesn’t require changing the class type is to use a category. For example:

@interface UINavigationBar (KTCustomLook)

- (void) drawRect:(CGRect)rect;

@end

@implementation UINavigationBar (KTCustomLook)

- (void) drawRect:(CGRect)rect
{
   UIImage *image = [UIImage imageNamed: @"navbar.png"];
   [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

@end

I admit I have used this approach in more than one project, but doesn’t feel right to me. It feels more like using voodoo or black magic then following good OO design. But this approach does work and it does save you time by eliminating the need to change the class type for each instance of UINavigationBar within your app. [Update: This approach doesn't feel right because it overrides a method in a category. You should know this is a bad, bad thing, and I don't encourage others to do it. That said, it does work.]

One thing to note when using an image to customize the UINavigationBar, you still want to set the tintColor for the navigation bar. As I mentioned, setting this property will also change the background color used by the bar button items. So you want to set the tintColor to the appropriate color making the button color fit in with the navigation bar color style.

For those of you who want to see these concepts in action, I posted a sample project, called CustomNavBar, on github. Enjoy.


Speaking at VTM and 360iDev This Fall

Friday, September 10th, 2010

Fall is almost here and it’s going to be a busy on for me. Not only will I be heads down trying to finish my upcoming book, I will be speaking at two iOS developer conferences. The first conference is Voices That Matter in Philadelphia on October 16 and 17. This will be my first VTM, and I’m very excited to be apart of the event. There are a lot of great talks planned for the 2-day conference. My talk will be on the advantages of writing universal apps for iPad and iPhone. Early bird pricing ends today so register now to save money. Use the discount code PHASPKR to save even more money.

The second conference I’m speaking at this fall is 360iDev Austin. It’s no secret that 360iDev is one of my favorite developer conferences. I have made numerous friends since attending my first 360iDev last year, and the networking I’ve done at past 360iDev conferences has help land me paying gigs. My session at 360iDev will be on the fundamentals of iPad programming. This session is intended for anyone just starting out with iPad programming. Earlier bird pricing as already ended for 360iDev Austin but even at the full price of $599, it is money well spent if you are serious about iOS programming.


My App Store Pricing Experiment: The Final Chapter

Wednesday, September 1st, 2010

Today is my final update on the pricing experiment I started back in May. You can read the previous posts here and here. The goal of the experiment was to see if the sales trend for Labor Mate would continue at the higher price of $1.99. After three full months, I can say the higher price has done more harm than good.

Labor Mate continues to bring in over $1K per month despite the higher price, but the trend doesn’t look good. Over the last few weeks, the number of sales has decreased everywhere except in Japan. For some reason, sales are up in Japan and if not for Japanese sales in August, Labor Mate would have posted it’s worse sales month in 6 months.

Since it’s inception date, revenue for Labor Mate has been on a slow but steady rise. The higher price has reversed that trend. Labor Mate now appears to be on a slow but steady fall. Not only that, Labor Mate, which at one time was in the Health and Fitness Top 100 category in App Stores throughout the world, is not longer visible on any Top 100 chart, anywhere. This three month pricing experiment maybe the start of a slow death for Labor Mate, but it’s not done for yet.

I have big plans in the works, and a new update is just around the corner. I was planning a big release in a few weeks that would hopefully justify the $1.99 price tag in the eyes of consumers but I’m changing my strategy. Starting today, Labor Mate is back down to 99 cents, and instead of a big update in a few weeks, I’m going to release 3 or 4 updates over the next 1 to 2 months. My goal here is to get Labor Mate back on track and return to the trend I was seeing before the price increase.

Update: One thing I should point out is Labor Mate has a lot of competition. The other similar apps cost between free and 99 cents. This maybe a key reason why a $1.99 Labor Mate cannot sustain the slow but steady growth seen by the 99 cent Labor Mate.


Don’t Rely on UIDevice orientation for Rotation

Monday, August 23rd, 2010

Last week I tweeted about having rotation issues with an iPad app I’m working on. This is the second time in recent weeks I’ve encountered rotation issues in an app. In both instances I was using a UIScrollView so I started thinking the UIScrollView was source of my problems. In the most recent instance the UIScrollView contains a UIView that uses a number of CALayer instances for content display.

For those who don’t know, in Mac OS X 10.5 and greater, CALayer has the autoresizingMask property. Unfortunately this property does not exists under iOS, so it’s up to my code to do the resizing CALayers as needed. This is where the rotation issue revealed itself.

As the device is rotated from portrait to landscape, or vice versa, my view must resize and adjust the layout of the CALayers. Because UIView does not receive the rotation notifications I decided to be smart and use the orientation property from UIDevice. So in my view I had code similar to this:

UIInterfaceOrientation interfaceOrientation = [[UIDevice currentDevice] orientation];
if (UIInterfaceOrientationIsLandscape(interfaceOrientation) == YES) {
  // Adjust for landscape.
} else {
  // Adjust for portrait.
}

What I failed to realize, however, is that the property orientation will always return 0 unless orientation notifications are enabled. Here is a quote directly from the Developer Documentation:

“The value of this property always returns 0 unless orientation notifications have been enabled by calling beginGeneratingDeviceOrientationNotifications.”

Doh! Guess I should have RTFM sooner.

Because the orientation property will return 0, sometimes the view in my app would not rotate. But I didn’t know this was the source of the problem at the time.

After banging my head over and over on the wall, I decided to investigate exactly what was happening. I never suspected [[UIDevice currentDevice] orientation] but eventually I noticed it was returning 0. This seemed odd so I checked the Developer Documentation. I wasn’t expecting to learn anything new. Boy, was I wrong. I was surprised when I read the property always returns 0 when orientation notifications are not enabled.

I now knew the source of my rotation problems, and a likely quick fix to the problem would have been to call beginGeneratingDeviceOrientationNotifications, get the device orientation, then call endGeneratingDeviceOrientationNotifications. But this just seemed wrong to me. The better fix, in my opinion, is to rely on the rotation notifications received by the view controller, so that’s exactly what I did.

Now in my particular situation, there are two times I want to resize and layout the CALayers in the UIView. One of those times is when the device is rotated, which is when the view controller receives the willRotateToInterfaceOrientation:duration: message. This was easy to solve. I added an adjustLayoutToInterfaceOrientation: method to my UIView and I call the method inside the UIViewController’s willRotateToInterfaceOrientation:duration: method. For example:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
   [view_ adjustLayoutToInterfaceOrientation:toInterfaceOrientation];
}

The other time my UIView needs to adjust the layout of the CALayers is when the UIScrollView scrolls. I’m using a modified version of Matt Gallagher’s virtual pages in a UIScrollView approach, so the contents of my view changes as the user scrolls. This is where, previously, I was trying to be smart and use the orientation property from UIDevice. But what I really need is the current orientation as received in the most recent call to willRotateToInterfaceOrientation:duration:.

My solution was to add the ivar currentOrientation to my view controller. My view already has a reference to the view controller. I exposed currentOrientation so my view can retrieve the property value. This allowed me to replace the [[UIDevice currentDevice] orientation] code with [controller currentOrientation]. Now the view always knows the current orientation and the app does not need to enable orientation notification in code.

As a result, the first code snippet in this posting changes to:

UIInterfaceOrientation interfaceOrientation = [controller_ currentOrientation];
if (UIInterfaceOrientationIsLandscape(interfaceOrientation) == YES) {
  // Adjust for landscape.
} else {
  // Adjust for portrait.
}

And the willRotateToInterfaceOrientation:duration: implementation changes to:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
                                duration:(NSTimeInterval)duration
{
   currentOrientation_ = toInterfaceOrientation;
   [view_ adjustLayoutToInterfaceOrientation:toInterfaceOrientation];
}

This has been an eye opener for me, and I now have a new rule of thumb. Never use UIDevice orientation in code that is responsible for the resizing and layouts of subviews and CALayers.


Some Useful Books for iOS Developers

Monday, July 26th, 2010

Last week I had the opportunity to teach a one-day class on learning iPad programming. One of the students asked what books I recommend. A friend asked the same question over the weekend, so I thought I might as well post the list of books on iOS and Objective-C programming I have found helpful.


A Really Simply Timer

Monday, July 19th, 2010

SimpleTimer.pngA friend asked if I would put together a sample iOS app that shows how to display a stopwatch timer like the one used in Labor Mate. It seemed like a fun exercise to break up the night, so I said, “Sure, why not.”

I decided others might find the sample source code useful so I posted the project to github for all to enjoy. The piece devs might find interesting is the KTStopwatch class. This is a simplified version of the class I use in Labor Mate. It supports wall clock and elapsed time.

The source code is licensed using The MIT License, so do with it what you like. Enjoy.


Hiring iOS Developers in the Boston Area

Friday, July 9th, 2010

Jonathan Kardos has an excellent article on Boston Innovation explaining how to find a mobile app developer in Boston. NSHappyHour even gets a mention, which is great. But one thing left out of Jonathan’s article are national events such as 360idev.

Speaking from personal experience, attending events like 360idev is a great way to not only meet and network with developers but for developers to find work, whether they are looking for a permanent position or contract work. This is one of the many reasons I return to 360idev event after event, and why I will be at the next one in Austin TX on November 7th through the 10th.


Sale Numbers are In: An Update on My App Store Pricing Experiment

Friday, July 2nd, 2010

Last month I mentioned my experiment with the pricing for Labor Mate to see what effects, if any, a price increase will have. The initial results were interesting. The number of units sold went down, but revenue had gone up. On the surface it seems the price increase was a success, but I needed more data.

I increased the price of Labor Mate by $1 on May 15, going from $0.99 to $1.99. By increasing the price on May 15, I was able to compare the first half of the month with the second half. And as I mentioned in the previous post revenue had indeed gone up. But I was curious to see if this would continue and what might be the long term effects, so I left Labor Mate at $1.99. After all, I made more money in May as a result of the price increase.

June is over and the sales numbers are in. I’m now able to compare a full month of sales (for June) at the higher $1.99 price to a full month of sales (for April) at the lower $0.99 price. And I can compare these numbers to May’s numbers. The results might be surprising to some, but are inline with what I secretly thought would happen.

In June, Labor Mate earned a whooping $31.94 more money than in April, and it earned $30.90 less compared to May. In April, Labor Mate averaged $35 per day. The average was $37 per day in May, and only $36 per day in June.

Revenue from Labor Mate has been on a slow but steady increase since it was first released back in 2008. Though I cannot prove it, based on past trends, my gut tells me Labor Mate would have likely hit June’s revenue number in May without the price increase. And my gut, again based on the trend, says June would have probably hit May’s number without the price increase. In other words, while the price increase did improve Labor Mate’s revenue, the amount of additional revenue resulting from the price increase is actually no different from the slow and steady increase in revenue I was already seeing at the lower, 99 cent price point. As a matter of fact, I saw a bigger jump in revenue between March and April, with April bringing in a whooping $175.71 more than March.

I’m now convinced the price increase did little to improve revenue, and actually the price increase likely did more harm than good. Prior to the price increase Labor Mate was ranked in the Top 100 in the Health and Fitness category for a number of different countries including the U.S. Today Labor Mate is no were near ranking in the Top 100 in most stores.

Another negative effect caused by the price increase is that fewer people are now using Labor Mate. As I noted in the previous post, the number of daily downloads dropped. This means fewer people are buying Labor Mate, which in turns means fewer people are using it. I believe Labor Mate’s slow but steady raise was due in part to word of mouth advertising. Now that there are fewer new moms and dads buying and using Labor Mate, there are fewer people recommending Labor Mate to other new moms and dads. And I admit, ignoring price for a moment, I’m a little disappointed that fewer people are using the app. A part of me prefers selling at a lower price point so more mom and pops to be will use it. (Hmm, maybe I should release a free, iAd supported version.)

So what’s next? I’ve thought about dropping the price back down to 99 cents, but this could lead to a backlash from the folks who purchased Labor Mate over the last 6 weeks. Plus, $1.99 is still cheaper than a large cup of Starbucks coffee. The better idea, and the one I have been planning all along, is to continue improving Labor Mate and make it stand out above the other 99 cent copy cats. This includes leaving the price at $1.99 for now. After all, as one recent new user said to me in email, “it is a very practical and intuitive app and certainly justified at $1.99.”


ANN: Labor Mate 1.7 is in the App Store

Friday, July 2nd, 2010

The latest version of Labor Mate, the best dang labor contraction timer for the iPhone and iPod touch, is now available in the App Store. This update is FREE to existing user. The update includes a much improved UI, compatibility with iOS4 and multitasking, and support for the new retina display on iPhone 4.