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
}

How to easily create a custom UIButton

Creating custom buttons for your iOS app can be a bit of a pain if you decide to use images. A lot of messing about with frame positions and going back and forth between an image editor and your iPhone app. Well, here’s a very simple and effective way to create a custom UIButton which doesn’t look like the boring default iPhone (UIButtonTypeRoundedRect) button and is very easy to adjust the ‘look and feel’ of.

There are two buttons, the first is a UIImage inside a UIImageView and the second is a simple gradient placed inside a UIButtonTypeCustom.

 

        // Start by creating a custom button
        UIButton* enter_data_button = [UIButton buttonWithType:UIButtonTypeCustom];
        //Set the button size and position in its parent view
        enter_data_button.frame = CGRectMake(20, 20, 280, 180);
        //Set the tet in the button
        [enter_data_button setTitle:@"Begin Your Nutrition Check!" forState:UIControlStateNormal];
        [enter_data_button addTarget:self action:@selector(didTapNewEntry:) forControlEvents:UIControlEventTouchUpInside];

        //Create an image view so we can adjust the way the image is displayed
        UIImageView *carrots_image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"applecarrots.png"]];
        //The layer is where we can make some changes
        CALayer* l = [carrots_image layer];
        [l setMasksToBounds:YES];
        //This rounds the corner of the image
        [l setCornerRadius:10.0f];
        //Set the border and it's color, if you like
        //[l setBorderWidth:4.0];
        //[l setBorderColor:[[UIColor blueColor] CGColor]];

        //get the frame of the UIButton and your image
        CGRect button_frame = enter_data_button.frame;
        CGRect image_frame = carrots_image.frame;

        //position your image in the upper right
        CGRect new_image_frame = CGRectMake(button_frame.size.width/2.0f - image_frame.size.width/2.0f, 4.0f, image_frame.size.width, image_frame.size.height);
        enter_data_button.titleEdgeInsets = UIEdgeInsetsMake(image_frame.size.width+4.0f, 0, 0, 0);
        enter_data_button.titleLabel.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:20.0f];
        carrots_image.frame = new_image_frame;

        //add your image as a subview of the button's view
        [enter_data_button addSubview:carrots_image];
        [self.view addSubview:enter_data_button];

        // Now lets start the custom button with a gradient and some text. Simple and effective.
        UIButton* view_last_entry_button = [UIButton buttonWithType:UIButtonTypeCustom];
        view_last_entry_button.frame = CGRectMake(20, 220, 280, 80);
        [view_last_entry_button setTitle:@"View last check" forState:UIControlStateNormal];
        [view_last_entry_button addTarget:self action:@selector(didTapViewEntry:) forControlEvents:UIControlEventTouchUpInside];
        view_last_entry_button.titleLabel.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:26.0f];
        // Create a gradient layer
        CAGradientLayer *gradient = [CAGradientLayer layer];
        gradient.cornerRadius = 8.0f;  //Set the corner radius so the edges of the button are rounded
        gradient.frame = view_last_entry_button.bounds; //We need to tell the gradient how much and where to fill
        // Now supply an array of colors, which will form the gradient. You can add more than two, but we're sticking with two here
        gradient.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithRed:100.0f/255.0f green:180.0f/255.0f blue:96.0f/255.0f alpha:1.0f] CGColor], (id)[[UIColor colorWithRed:6/255.0f green:105/255.0f blue:57/255.0f alpha:1.0f] CGColor], nil];
        // Insert the gradient to the top of the button's sublayer
        [view_last_entry_button.layer insertSublayer:gradient atIndex:0];
        [self.view addSubview:view_last_entry_button];

Here’s a screenshot of the resulting buttons, take from my Nutrition Check app, available in the App Store now!

Original Apple and Carrots image
Here’s the original image, note there’s no rounded corners!
Example of two customised UIButton's
The top button shows how the UIImageView of the apple and carrots is finally displayed, just above the text in the button – note the round corners. The second button down (Review previous check) is a simple gradient applied to a custom button.

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:

    1. AdMob
    2. Brightroll
    3. Google AdSense for Mobile Applications
    4. Greystripe
    5. iAd
    6. InMobi
    7. Jumptap
    8. MdotM
    9. Millennial Media
    10. SAY Media [VideoEgg]
    11. ZestADZ
  • 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!

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress