Archive | objective c RSS for this section

iOS 5 Twitter Framework Integration

One of the new features in iOS 5 that will save you a lot of time is the Twitter integration.

The first thing you need to do is add the Twitter framework to your project.

Then include the Twitter header file in your header

#import Twitter/Twitter.h

Next you need to create a TWTweetComposeViewController and set the text/link/image that you want to share.

TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init ];
[tweetSheet setInitialText:@"Tweet Text"]];
 
[tweetSheet addImage:[UIImage imageNamed:@"yourimage"]];
 
[tweetSheet addURL:[NSURL URLWithString:@"http://www.jamie.co.za"]];
 
tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
        [self dismissModalViewControllerAnimated:YES];
    };
 
[self presentModalViewController:tweetSheet animated:YES];

Thats all there is to it, simple and quick.

There is also a canSendTweet function you can call to detect if the user has a twitter account setup on the device. The completionHandler will be called once the tweet has been sent.

Simultaneous Gesture Recognizers

To recognize 2 gestures simultaneously, such as to enable panning and rotating at the same time, there is a method called gestureRecognizer:shouldRecognizeSimultaneouslyWithGestureRecognizer: in the UIGestureRecognizerDelegate protocol. By default it returns NO so implement the method and return YES.

 

- (BOOL) gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
 
    return YES;
 
}

Custom UINavigationBar

Since I’ve moved over to just doing mobile dev (iPhone/iOS specifically), I thought I’d start occasionally posting some Objective-C code snippets. Coming from a PHP background its been a bit of a learning curve.

This is a simple Category to create a custom UINavigationBar inorder to apply it to the entire app. I only needed to set a background image so its really simple.

 

UINavigationBarSilver.h

#import 
 
@interface UINavigationBar (UINavigationBarSilver)
 
@end
UINavigationBarSilver.m
#import "UINavigationBarSilver.h"
 
@implementation UINavigationBar (UINavigationBarSilver)
 
- (void)drawRect:(CGRect)rect {
	UIColor *color = [UIColor clearColor];
	UIImage *img	= [UIImage imageNamed: @"top_nav_bg.png"];
	[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
	self.tintColor = color;
}
 
@end