1

I am fairly new to iOS Development and I've always wondered if a user running my application on iOS 4 were to try and run this code:

//POST TWEET//
- (void)showTweetSheet
{
    TWTweetComposeViewController *tweetSheet =
    [[TWTweetComposeViewController alloc] init];
    tweetSheet.completionHandler = ^(TWTweetComposeViewControllerResult result) {
        switch(result) {
            case TWTweetComposeViewControllerResultCancelled:
                break;
            case TWTweetComposeViewControllerResultDone:
                break;
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:^{
                NSLog(@"Tweet Sheet has been dismissed.");
            }];
        });
    };
    [tweetSheet setInitialText:@"Check out this cool picture I found on @Pickr_"];
    //  Add an URL to the Tweet.  You can add multiple URLs.
    if (![tweetSheet addURL:[NSURL URLWithString:ImageHost]]){
        NSLog(@"Unable to add the URL!");
    }
    [self presentViewController:tweetSheet animated:YES completion:^{
        NSLog(@"Tweet sheet has been presented.");
    }];
}

What would happen? Would the application just terminate with an error or will the code just not run? And how do I properly implement features that are OS specific? Would I just use something like this:

NSString *DeviceVersion = [[UIDevice currentDevice] systemVersion];
int DeviceVersionInt = [DeviceVersion intValue];
if (DeviceVersionInt > 5)
{
    //do something.
}
else
{
    //don't do a thing.
}
ecnepsnai
  • 1,882
  • 4
  • 28
  • 56

3 Answers3

7

It will crash on iOS 4 if you write iOS5 features without checking if they are available or not. Try to implement Twitter like this

Class twClass = NSClassFromString(@"TWTweetComposeViewController");
if (!twClass) // Framework not available, older iOS
{
    //use iOS4 SDK to implement Twitter framework
}
else {
    //use Apple provided default Twitter framework

}

Make sure you have added Twitter Framework with weak link.

Manish Agrawal
  • 10,958
  • 6
  • 44
  • 76
  • 2
    Checking for features is better than checking a version number, +1 – jrturton Aug 11 '12 at 05:32
  • 1
    What to do you mean by using a weak link with the Twitter Framework? I set it to "Optional" in my projects settings, is that what you mean? – ecnepsnai Aug 11 '12 at 05:35
1

Id imagine that it would work the same as with any other api. If you link against a function which is not in a previous version, the program will crash on an attempt to call the function. Therefore, version switches are used, as you demonstrated, to avoid crashes.

chacham15
  • 13,719
  • 26
  • 104
  • 207
1

The app would crash. If you want to implement features based on iOS, you can use a variety of methods. See this question.

Community
  • 1
  • 1
WolfLink
  • 3,308
  • 2
  • 26
  • 44