20

我有一段 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]];
        }
    }
}
4

2 回答 2

21

您需要弱链接 Twitter 和 Social 框架,并检查您的代码是否有可用的框架。弱链接是这样完成的:

  • 在 XCode 中点击你的项目,选择你的 Target,Build Phases,然后 Link Binary with Libraries
  • 如果您想在 iOS 5.x 或更早版本上运行此应用,请确保将 Social.framework 设置为 Optional
  • 如果您想在 iOS 4.x 或更早版本上运行它,请确保 Twitter.framework 设置为 Optional

我喜欢创建简单的类函数来确定哪些框架可用。这可能看起来像这样:

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

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

您的“tweet”代码可能如下所示:

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
}
于 2012-09-20T13:46:59.373 回答
7

不确定这些运行时操作有多昂贵,但这样做并没有什么坏处,因为在应用程序运行时这种状态发生变化的可能性为零:


+ (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;
}
于 2012-11-08T23:24:04.337 回答