Friday, December 31, 2010

Case-insensitive NSSortDescriptor usage

I found this little gem today which was quite useful! If you are using a NSSortDescriptor to sort data using strings, you can make it case insensitive with the following:

sorter = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

Thursday, November 4, 2010

null strings in JSONRepresentation

NSDictionary supports the JSONRepresentation method which is quite useful for generating JSON to send to servers. I recently had an issue where if one of my object's members was null, the JSONRepresentation would fail. If I set the member NSString to @"", then "" would get sent across, but what I really wanted was "member":null to be sent.

The solution is to set your object to [NSNull null], and then the correct JSONRepresentation will be created.

Tuesday, August 24, 2010

Resize a UIImage

Here's a really easy one. Resize a given UIImage that you've loaded in the following manner:

CGSize newSize = CGSizeMake(228.0, 228.0);
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Thursday, July 29, 2010

Using Next and Done with UITextFields and UIKeyboard

So you've probably seen in interface builder that its quite simple to pick which enter key you will use in your keyboard for a given UITextField. The minor complexity is that these different labels don't mean anything unless you add some code in your controller to handle the actions. Let's say you have three UITextFields:
  1. Username
  2. Password
  3. Email
You would want to set the first two to 'next' and the last to 'done' in interface builder. Then, you will need to implement UITextFieldDelegate in your View Controller, and use the following:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
if (textField == txtUsername) {
[txtPassword becomeFirstResponder];
}
else if (textField == txtPassword) {
[txtEmail becomeFirstResponder];
}
[textField resignFirstResponder];
return YES;
}

Monday, July 26, 2010

XCode 4 Developer Preview

Well, as registered developers we aren't really allowed to talk about it, but I'll just say WOW it is seriously nice. This is the first huge upgrade to XCode in a long time. It actually doesn't feel like Project Builder any more, and we can use git!

Friday, July 23, 2010

Accessing your UITabBarController from another UIViewController

I recently came across a situation where I wanted to show the user a UIAlertView on a view listing data if there was no data entered yet, and then direct them to an 'Add' tab in my application after they clicked OK. I ended up doing this by accessing my application's delegate which then had access to the Tab Bar Controller of the entire application.

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
//only one alertview so we'll just push here
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate tabBarController] setSelectedIndex:1];
}

The indexing is zero-based, as expected, so don't forget that :)

Setting the color of a UINavigationBar programmatically

If you are using a UINavigationController for your views, you can easily change the UINavigationBar's color using the following:

self.navigationController.navigationBar.tintColor = [UIColor brownColor];

and choose whatever color you wish.

Wednesday, July 21, 2010

Converting NSString to int, float, double, and bool

Here's another really basic & simple NSString tip for today to help you convert strings to various basic types. The NSString class has a bunch of conversions already built in:

float stringFloat = [myString floatValue];
int stringInt = [myString intValue]
double stringDouble = [myString doubleValue]
BOOL boolValue = [myString boolValue];

Easy NSString substring

Sadly, creating a simple substring in Objective-C isn't quite as obvious as in other languages. Here's a basic method to grab a substring of a given string before a defined character, in my case the ":" character:

NSRange end = [_contentsOfElement rangeOfString:@":"];
[myVar setName:[_contentsOfElement substringWithRange:NSMakeRange(0, end.location)]];

If you wanted grab a string between two characters, you could do:

NSRange start = [_contentsOfElement rangeOfString:@"|"];
NSRange end = [_contentsOfElement rangeOfString:@":"];
[myVar setName:[_contentsOfElement substringWithRange:NSMakeRange(start.location, end.location)]];

Monday, July 19, 2010

UITextView automatic keyboard display

I've come across a few instances where my view is simply a UITextView for the user to enter some text into, such as for a comment field, or a twitter entry, etc. In these scenarios it is best to automatically show the UIKeyboard for the user to just start typing. This is a really easy one, all you have to do is have the synchronized UITextView become the first responder to make the keyboard appear:

- (void)viewDidLoad {
    [super viewDidLoad];
    [commentTxt becomeFirstResponder];
}

You can use the same technique with UITextFields too.

Friday, July 16, 2010

Deselecting UITableViewCell's when a view appears

Here's a simple tip to make your application look a little more polished. It's very common for us to push new view controllers when a row is selected in a UITableView. If the user uses a UINavigationBar button to go back to the previous view, or the new view controller gets popped in some other manner, you will want to remove the highlight from the selected UITableViewCell. Here's a simple snippet to add to do just that assuming you have a UITableView that is an IBOutlet on your view controller:

- (void) viewDidAppear:(BOOL)animated {
if ([self.table indexPathForSelectedRow]) {
[self.table deselectRowAtIndexPath:[self.table indexPathForSelectedRow] animated:YES];
}
}

Facebook API Integration on the iPhone

This is another short post, for I can't take credit for any of the code. I just wanted to pass across a link to the Facebook iPhone SDK that they've put together. I despise Facebook, but must admit this is one of the most well polished and documented libraries for the iPhone that I've seen yet. The framed webview that pops up for the authorization looks great.

Thursday, July 8, 2010

Default UITableViewCell font, and how to change it

The default font is... Helvetica! If you want to change the size of the font, you can use:

cell.textLabel.font = [UIFont fontWithName:@"Helvetica" size:14.0];

You can also change the font using the above statement with a different font name.

Hiding the keyboard when using UISearchBar and UITableView

In one of my recent applications, I set up a UIViewController containing a UISearchBar and a UITableView. I implemented all the necessary delegate methods for both classes, and my search was working just fine (after working out a few kinks..). I did have one remaining problem though... Since I was searching as the user was typing, similar to an autocomplete on the web, the results were automatically being shown in the tableview but the keyboard was still there! This only showed the user a few results and was not very user friendly. I next figured out how to hide the keyboard once the user touched the tableview and started scrolling.

It's actually really easy to do this - all you have to do is implement the UIScrollViewDelegate as follows:

@interface YourViewController : UIViewController <UITableViewDataSource, UITableViewDelegate, UIScrollViewDelegate>

...and then put the following into your class.m source:

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
[search resignFirstResponder];
}

HTTP Basic Authentication on the iPhone

I can't steal credit for this one. I just wanted to pass along this excellent link to a post titled Objective-C and HTTP  Basic Authentication. It still amazes me that the iPhone SDK doesn't have base 64 encoding built in... but at least some smart folks out there have gone through the trouble of putting it together for us.

What I ended up doing was putting the encode function in my parent REST client class, then I was able to call it as necessary in my subclasses.

NSURLErrorDomain error -1012

So have you run across this one when trying to issue some url requests? What it means is there was an authentication error - NSURLErrorUserCancelledAuthentication. Okay, so what does that mean? You likely are trying to issue a synchronous http request and not passing proper authentication, or you've messed up your auth in some other manner. It's a lot easier to handle authentication when using asynchronous calls and responding to the auth challenge:

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge previousFailureCount] == 0) {
        NSURLCredential *newCredential;
        newCredential=[NSURLCredential credentialWithUser:@"username"
                                                 password:@"password"
                                              persistence:NSURLCredentialPersistenceNone];
        [[challenge sender] useCredential:newCredential
               forAuthenticationChallenge:challenge];
    } else {
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}

Changing UITableViewCell text colors

This is a really easy but, but may not be immediately apparent. Personally, I first tried changing the color of the cell directly, then tried changing the color of the text within the cells. What you actually need to do is change the textColor property of the UILabel's that are inside of the cell. For example, if you want to change the color of the textLabel, you would use:

cell.textLabel.textColor = [UIColor lightGrayColor];

The same goes for the detail label if you are using that type of cell:

cell.detailTextLabel.textColor = [UIColor blackColor];

Sunday, June 13, 2010

Declaring constants in Objective-C

Yes, I'm back! Sadly my last post was in January, but I'm hoping to get back on the wagon here adding tips and tricks. Today I'll bring you the rather simple task of declaring and using constants in Objective-C. Let's assume you want to declare an NSString constant in your class that holds a url. In your header .h file you will need the following:

#import 

extern NSString * const BaseURL;

@interface ClassName : NSObject {

You will then need to set it's value in your main .m file as follows:

#import "ClassName.h"

NSString * const BaseURL = @"http://some.url.com/path/";

@implementation ClassName

You can now access this constant throughout your class or subclasses. Here's an example of usage:

NSString *urlString = [NSString stringWithFormat:@"%@%@", BaseURL, @"filename.html"];

Tuesday, January 26, 2010

Disabling keyboard on UITextField selection

There are often cases where you want the look of UITextField but don't want to use the keyboard. There are a few hacky ways such as hidden buttons overlaying text fields, but I think this is the most intelligent way to disable the show of the UIKeyboard, and replacing it with something of your choice:


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField { 
// Make a new view, or do what you want here
UIDatePicker *pv = [[UIDatePicker allocinitWithFrame:CGRectMake(0,185,0,0)];
[self.view addSubview:pv];

return NO;
}

You'll need to be implementing UITextFieldDelegate, and you can catch this event prior to the framework showing the UIKeyboard. In the above example I am showing a UIDatePicker instead. Returning NO is what is making the keyboard not show, since we aren't actually letting the editing begin.

Making the keyboard disappear

So... I said this blog was going to be about basics, right? Well, here's a really basic concept that may elude some folks. If you have a UITextField on your view, and are implemented UITextFieldDelegate in your delegate class, perhaps you have noticed that after you enter some text using the keyboard for your field, the keyboard won't disappear when you hit return? Well, here's all you have to do:


- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}

Since you are implementing the UITextFieldDelegate class, this method will be called on Return, your text field will give up focus, and the UIKeyboard will disappear. If you want to be more particular and a little safer in your method you can do the following:

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
  if(textField == [self yourTextField]) {
[textField resignFirstResponder];
  }
return YES;
}

Monday, January 25, 2010

REST calls using Objective-C

Over the weekend I was working on a project where I needed to create some REST calls to an XML service. I already created a generic SOAP Request class in the past, so I figured this would be a little simpler. Before going and re-inventing the wheel I took a quick look around the web and found this excellent Base REST Client class out on the web, so I wanted to share the link with everyone.

I still pains me that I can't use XPATH or XSL in the iPhone SDK. Do you hear me Apple?

Wednesday, January 20, 2010

"Abstract Classes" in Objective-C

One of my favorite interview questions for a programming position is "What is the difference between a class, an abstract class, and an interface?". You'd be surprised how many people can't answer that. Anyways, that's my intro to today's lesson, where we attempt to create an abstract class in Objective-C.

Hopefully by now you know that an interface is a type whose methods are devoid of implementation, and it cannot serve as an instantiated class on it's own; it's not actually a class. An abstract class can have methods devoid of implementation, but has at least one method that contains implementation. It also contains member variables.

Enough of the introduction, and let's get down to business. First off, you cannot create an abstract class in Objective-C; there is no abstract modifier. I recently had the need for an abstract class and an interface in a project I was working on, and I emulated an abstract class to the best of my ability. Here's what I came up with:



//  MyObject.h

#import

@protocol IMyObject
-(void) myMethod;
@end

@interface MyObject : NSObject {
NSUInteger myID;
}

@property (nonatomic) NSUInteger myID;

-(void) myMethod2;


@end



//  MyObject
#import "MyObject.h"

@implementation MyObject

@synthesize myID;

- (id)init
{
[self doesNotRecognizeSelector:_cmd];
[self release];
return nil;
}

-(void) myMethod {
[self doesNotRecognizeSelector:_cmd];
}

-(void) myMethod2 {
  //do stuff
}

@end


So, the above class header file contains a nested @protocol which declares a required method signature. You can declare optional methods inside the @protocol with a @optional declaration. Essentially, the protocol is defining my interface. Next I declare my actual class and implement my IMyObject interface, which "forces" me to implement myMethod. I quote the "forces" in that last because the compiler really just gives me a warning, not a full error.

I've also declared a member variable myID and a property for it in my "abstract" class implementation, along with myMethod2, which can contain actual implementation to inherit to the subclasses of this "abstract" class. It is key to note the [self doesNotRecognizeSelector:_cmd]; lines, where I am basically using that to declare the method as abstract. It will throw an error if you try to call the method on the base call. If you put that within your init method, the class will not be able to be instantiated on its own.

Monday, January 18, 2010

UITableViewController subclassing in XCode has incorrect stub

If you've ever tried to create a UITableViewController subclass using XCode with SDK 3.x (3.1.2 at the time of this writing), it is important to note that the commented out code in the didSelectRowAtIndexPath method is incorrect.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //Navigation logic may go here. Create and push another view controller.
AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
[self.navigationController pushViewController:anotherViewController];
[anotherViewController release];
}

If you try to modify the above code to use one of your own custom view controllers, you will see a compilation warning stating:

  'UINavigationController' may not respond to '-pushViewController:'

If you actually try to run your application and select an item which tries to push an additional view controller, an exception will be thrown that states:

  [NSObject doesNotRecognizeSelector:]

The exception is definitely very misleading. The reason for both of the above problems is a second required parameter for this call, as follows:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //Navigation logic may go here. Create and push another view controller.
AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
[self.navigationController pushViewController:anotherViewController animated:YES];
[anotherViewController release];
}

iPhone Application Development - First Steps

Your first step in the wonderful world of iPhone SDK Development is to download the SDK, err... I mean, register for the iPhone Development Program... oh wait... you actually need a Mac... that's right :) Ok, let's try to make this more clear:
  1. Obtain a modern day Intel based Mac running Mac OS X Leopard. My suggestion is a Macbook Pro due to it's portability. Always check out the Refurb section of the Apple store!
  2. Register for the iPhone Developer Program
  3. Download the latest version of the iPhone SDK, including XCode
  4. Install the SDK
  5. Follow the iPhone Development Quick Start guide
I won't waste the time to completely go through a basic Hello World application, so instead I'll link you to an excellent beginner's resource - the iPhone Development Quick Start from Apple themselves. Depending upon your experience level with C-based programming languages, another recommendation of mine is to read through Apple's primer on Learning Objective-C, which I will not lie about... it can be a doozy at times.

Welcome to iPhone SDK Basics

During my own learning of various iPhone Development techniques over the past several months, I grew tired of the constant search for tips and tutorials related to the basics of iPhone application development. I'm talking about general layouts and navigation flow - things like setting up an application that has a tab bar and some tableviews, etc. Given my background in software engineering, some things that looked to be rather simple were much more complex than I expected.

I'll also be posting links to valuable resources that I found throughout the web.