How to use iOS 5 Twitter features and maintain earlier iOS compatibilty of your app
Making use of the new Twitter features built into iOS 5 allows you to leverage some of the powerful features of Twitter. But how do you do this without breaking backward compatibility with iOS 4? The new framework, Twitter.framework, must be linked to your build and this will break if you attempt to build using a previous to iOS 5 deployment target. Weak linking helps us sort that out. Weak linking basically means making a framework an ‘optional’ framework as opposed to ‘required’. In your project’s list of frameworks, you’ll see a drop down next to the framework title, labeled Required. Tapping on this will show you the two options you can set for each frame work; Required and Optional. Set the Twitter framework to optional. You can then perform some checks within your code to check if a class is available and make use of it if it is. You can do this like so;
Class tweetClass = NSClassFromString(@"TWTweetComposeViewController");
if(tweetClass) {
if([TWTweetComposeViewController canSendTweet]) {
TWTweetComposeViewController *tweetSheet = [[TWTweetComposeViewController alloc] init];
[tweetSheet addURL:[NSURL URLWithString:kAllDunneApp_NutritionCheckShort]];
[tweetSheet setInitialText:@"Tweet away!"];
[self presentModalViewController:tweetSheet animated:YES];
}else {
//right iOS but device can't tweet
}
}else{
//wrong iOS
}
Apply a gradient to a UIView background without sub classing
Applying a gradient background to a UIView is easy with the following code:
UIView* v = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 50.0f)];
CAGradientLayer *gradient = [CAGradientLayer layer];
gradient.frame = v.bounds;
gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor whiteColor] CGColor], (id)[[UIColor blackColor] CGColor], nil];
[v.layer insertSublayer:gradient atIndex:0];
If there are any other views added onto this UIView (such as a UILabel) you may want to consider setting the background color of those UIView’s to [UIColor clearColor] so the gradient view is presented instead of the background color for sub views. Using clearColor does have a slight performance hit.
How to pause or end a UIView animation via the CALayer
For anyone iPhone developers trying to pause an animation on a .layer object use the following two methods
-(void)pauseLayer:(CALayer*)layer
{
CFTimeInterval paused_time = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = paused_time;
}
-(void)resumeLayer:(CALayer*)layer
{
CFTimeInterval paused_time = [layer timeOffset];
layer.speed = 1.0f;
layer.timeOffset = 0.0f;
layer.beginTime = 0.0f;
CFTimeInterval time_since_pause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - paused_time;
layer.beginTime = time_since_pause;
}
For example, if you’ve started an animation on a UILabel’s layer using something similar to the following code:
CGRect frame = labelQuizTitleLabel.frame;
CGRect new_frame = CGRectMake(frame.origin.x, frame.origin.y, 0.0f, frame.size.height);
/* labelQuizTitleLabel is just a UILabel class member */
[UIView beginAnimations:@"" context:NULL];
[UIView setAnimationDuration:10.0f];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
labelQuizTitleLabel.frame = new_frame;
[UIView setAnimationDelegate:self];
[UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
[UIView commitAnimations];
(This code will animate a UILabel along it’s width – which is used to simulate a ‘countdown’ bar)
You could then arrest the countdown (i.e. the animation) at any time using the following code:
[self pauseLayer:labelQuizTitleLabel.layer];
Simples.
iOS SDKs you should know about
Below is a list of the developer SDKs that I’ve come accross for iOS/Android devices, each useful for adding functionality to your iOS/Android apps. Leave a comment if there are any other SDKs you think should also be here.
- Aviary
http://www.aviary.com/tools/feather
iOS and Android
Adds some amazing photo editng functionality to your apps. This is an SDK definitely worth looking into. - AdWhirl
AdWhirl lets you serve ads from eleven ad networks including: - Shiva3D
If you’re into game development or media on iOS/Android then this is worth a look. The SDK isn’t free to use for commercial games. But it does feature cross platform compatibility with Windows, Mac, Linux, iPhone, iPad, Android, WebOS, Airplay SDK and Wii. Very interesting - Corona
This SDK lets you write in the Lua programming language and deploy to both Android and iOS devices. It’s the same language that Angry Birds was written in. It contains a large number of features and is not free, I’m afraid. But then, that can be a good thing, no?
If you can think of any other’s please leave a comment below!

