我有一段 twitter 共享代码适用于 iOS6,但我需要该应用程序也能很好地回退到 iOS5...
它看起来像这样:
- (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?
}
}
那么,我如何在 iOS5 中很好地回退(我假设我需要 TWTweetComposeViewController),以便我也可以在 iOS5 中使用本机 twitter?
编辑:最后我仍然懒得回退到 TWTweetComposeViewController 所以我决定简单地回退到这个顺序:iOS6 原生推文 -> 安装的推特应用程序 -> 网址。这是我整理的功能,希望对某人有所帮助:
+(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]];
}
}
}