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]];
}
}
}
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:
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
}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With