Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS6 - Social Framework - how does SLComposeViewController fallback to TWTweetComposeViewController for iOS5?

Tags:

I have a piece of piece of twitter sharing codes that works fine for iOS6 but I need the application to fall back nicely to iOS5 too...

It looks like this:

- (void) shareOnTwitter
{
    if([SLComposeViewController instanceMethodForSelector:@selector(isAvailableForServiceType)] != nil)
    {
        if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
        {
            NSLog(@"twitter available");
            SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
            [composeViewController setInitialText:self.sharingText];
            [self.sharingController presentViewController:composeViewController animated:YES completion:nil];
        }
        else
        {
            NSLog(@"twitter not available!");
        }
    }
    else
    {
        // SLComposeViewController not available, this is most likely <iOS6, what to do here?
    }
}

SO, how do I fall back nicely in iOS5 (I assume I need TWTweetComposeViewController) so that I can use the native twitter in iOS5 too?

EDIT: At the end I am still lazy to fallback to TWTweetComposeViewController so I decided to simply fallback in this sequence: iOS6 native tweet -> installed twitter app -> web url. This is the function I put together, hope it helps someone:

+(BOOL)isSocialFrameworkAvailable
{
     // whether the iOS6 Social framework is available?
    return NSClassFromString(@"SLComposeViewController") != nil;
}

- (void) shareOnTwitterWithText:(NSString*)text andURL:(NSString*)url andImageName:(NSString*)imageName
{
    // prepare the message to be shared
    NSString *combineMessage = [NSString stringWithFormat:@"%@ %@", text, url];
    NSString *escapedMessage = [combineMessage stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];
    NSString *appURL = [NSString stringWithFormat:@"twitter://post?message=%@", escapedMessage];

    if([SocialManager isSocialFrameworkAvailable] && [SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
    {
        // user has setup the iOS6 twitter account

        SLComposeViewController *composeViewController = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
        [composeViewController setInitialText:text];
        if([UIImage imageNamed:imageName])
        {
            [composeViewController addImage:[UIImage imageNamed:imageName]];
        }
        if(url)
        {
            [composeViewController addURL:[NSURL URLWithString:url]];
        }
        [self.sharingController presentViewController:composeViewController animated:YES completion:nil];
    }
    else
    {
        // else, we have to fallback to app or browser
        if([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appURL]])
        {
            // twitter app available!
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appURL]];
        }
        else
        {
            // worse come to worse, open twitter page in browser
            NSString *web = [NSString stringWithFormat:@"https://twitter.com/intent/tweet?text=%@", escapedMessage];
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:web]];
        }
    }
}
like image 880
mkto Avatar asked Sep 20 '12 09:09

mkto


2 Answers

You need to weak link the Twitter and Social frameworks, and check in your code whether a framework is available. Weak linking is done like this:

  • Click on your project in XCode, select your Target, Build Phases, then Link Binary with Libraries
  • Make sure Social.framework is set to Optional if you want to run this app on iOS 5.x or earlier
  • Make sure Twitter.framework is set to Optional if you want to run this on iOS 4.x or earlier

I like to create simple class functions to determine which frameworks are available. That could look something like this:

+(BOOL)isTwitterAvailable {
   return NSClassFromString(@"TWTweetComposeViewController") != nil;
}

+(BOOL)isSocialAvailable {
    return NSClassFromString(@"SLComposeViewController") != nil;
}

Your "tweet" code could look something like this:

if ([SomeClass isSocialAvailable]) {
   // code to tweet with SLComposeViewController
} else if ([SomeClass isTwitterAvailable]) {
   // code to tweet with TWTweetComposeViewController
} else {
   // Twitter not available, or open a url like https://twitter.com/intent/tweet?text=tweet%20text
}
like image 124
chris Avatar answered Nov 18 '22 06:11

chris


Not sure how expensive these runtime operations are, but no harm in doing this since there is zero chance of this status changing while the app is running:


+ (BOOL)isTwitterAvailable
{
    static BOOL available;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        available = NSClassFromString(@"TWTweetComposeViewController") != nil;        
    });
    return available;
}
+ (BOOL)isSocialAvailable
{
    static BOOL available;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        available = NSClassFromString(@"SLComposeViewController") != nil;        
    });
    return available;
}
like image 23
jpswain Avatar answered Nov 18 '22 06:11

jpswain