Best Practices: UIButtons and their IBAction/IBOutlets

Declare the UIButton outlet:

@property (weak, nonatomic) IBOutlet UIButton *cancelButton;

Declare the action method:

-(IBAction)cancelButtonAction:(id)sender;

Now when you are writing code you will never confuse the outlet and the action. You will always know what this means:

[self cancelButton];

or even worse:

self.cancelButton;

It’s a simple, easy to remember best practice. If you don’t follow this, or a similar convention (cancelButton/cancelAction) it WILL bite you in the ass one day.

DMActivityInstagram

A UIActivity class for sharing images to Instagram.

iOS 6 introduced the new UIActivityViewController, which presents sharing options like Facebook and Twitter (as well as printing and copying). I wanted to adapt this for CatPaint, which currently uses ShareKit for sharing. My current Instagram sharer stopped working in iOS 6, and I figured now would be a good time to implement Instagram sharing using this new technique.

Usage is simple:

DMActivityInstagram *instagramActivity = [[DMActivityInstagram alloc] init];

NSArray *activityItems = @[self.imageView.image, @"CatPaint #catpaint", [NSURL URLWithString:@"http://catpaint.info"]];

UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:activityItems applicationActivities:@[instagramActivity]];
[self presentViewController:activityController animated:YES completion:^{}];

Hopefully this code will be helpful for other people implementing their own UIActivity sharers, and getting Instagram support in to their apps.

I would love if someone took an axe to the visual design of the resizer view. It’s not my best work (especially on iPad it looks funny).

The resizer view is only needed if your app creates non-square images.

This is not in use in a shipping app yet, but it will be soon.

Interesting NSURLConnection quirk

You would expect this code always return a response, sometimes return data, and when there was no data, you could look at the response and the error to determine what happened. Not so! The response object is nil if it hits a NSURLErrorUserCancelledAuthentication (-1012).

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!data) NSLog(@"Network error: HTTP %i - %@", [response statusCode], error);

Changed to the code below, for now. Not sure how to deal with it long term though, may have to move away from my sendSynchronousRequest from within an NSBlockOperation technique of network queuing.

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
if (!data || !response) NSLog(@"Network error: HTTP %i - %@", [response statusCode], error);

Thing #244 I don’t like about Xcode:

Because the structure of your project is abstracted from the on-disc project structure, I always end up with tidy projects and messy project directories.

0 notes

Open-Source Cocoa Controls

Some quality controls right here!

vector vector: NSDateFormatter format strings for Twitter and Facebook

vectorvector:

Format strings for processing dates from facebook or twitter’s json api’s

//2010-09-12T22:21:18+0000
#define DF_FACEBOOK @"yyyy-MM-dd'T'HH:mm:ssZZ"
// Sun Sep 12 17:06:56 +0000 2010
#define DF_TWITTER @"EEE LLL dd HH:mm:ss ZZ yyyy"

Usage:

NSDateFormatter *df = [[NSDateFormatter alloc]...

vectorvector:

As per Dave Dribin’s fantastic article on C99 syntax in Xcode, I’ve done away completely with CGRectMake() and CGSizeMake() in my code. GOOD RIDDANCE!



The syntax reminds me of css.

vectorvector:

As per Dave Dribin’s fantastic article on C99 syntax in Xcode, I’ve done away completely with CGRectMake() and CGSizeMake() in my code. GOOD RIDDANCE!

The syntax reminds me of css.