iphone - 如何改进 iOS 中的 TWTweetComposeViewController 代码?

标签 iphone objective-c ios sharekit

我已经实现了以下代码来进行 Twitter 分享。在我的代码中,我尝试针对 iOS 5 进行测试,如果这不起作用,我将返回到使用 ShareKit 的 Twitter 代码进行共享的旧方法。

我向一位同事展示了代码,他建议我的代码可能存在缺陷,我需要做两件事:

  1. 进行适当的运行时检查?? (因为它可能会在 IOS 4 和更早版本上崩溃)即使它没有。
  2. 微弱链接 Twitter 框架

有人可以解释一下什么是正确的运行时检查吗?为什么是薄弱环节?

NSString *text = [NSString stringWithFormat:@"@Awesome chart: %@", self.titleLabel.text];

if ([TWTweetComposeViewController canSendTweet]) 
{

    TWTweetComposeViewController *tweetComposeViewController = [[TWTweetComposeViewController alloc] init];
    [tweetComposeViewController setInitialText:text];
    [tweetComposeViewController addImage:image];
    [tweetComposeViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result){

        dispatch_async(dispatch_get_main_queue(), ^{
            [self dismissModalViewControllerAnimated:YES];
            if (result == TWTweetComposeViewControllerResultDone)
            {
                NSLog(@"iOS 5 onwards Twitter share complete");
            }
        });
    }];

    [self presentViewController:tweetComposeViewController
                       animated:YES
                     completion:^{ }];
}
else
{
    SHKItem *item = [SHKItem image:image title:text];

    // Share the message.
    [SHKTwitter shareItem:item];
    NSLog(@"Device does not support Twitter library");
    }
}

最佳答案

弱链接只是意味着框架不需要安装在设备上。或者换句话说,当您向项目添加框架时,应用程序将要求该框架位于设备上。因此,如果您需要一个框架,但它不存在,那么该应用程序将会崩溃。在您的情况下,如果您希望该应用程序在 iOS 5 之前的 iOS 版本(即 iOS 4.x)上运行,则需要弱链接 twitter 框架。

正确的运行时检查意味着您应该将应用程序加载到您的设备上(运行 iOS 5 或更高版本)并测试应用程序的 Twitter 功能。如果它崩溃了,那么你就知道你遇到了问题。

我浏览了您的代码,一切看起来都很好。我没有通过我的编译器运行它,所以我不能说语法错误等等。我要做的一个改变是:

if ([TWTweetComposeViewController canSendTweet])

 Class twClass = NSClassFromString(@"TWTweetComposeViewController");
 if (!twClass) // Framework not available, older iOS
    return;

我之所以使用它,是因为它从字面上检查框架是否在该设备上,而 canSendTweet 检查用户是否已登录。所以我不想让用户感到困惑使用 iOS 5 的设备不支持 Twitter 的用户登录。

如果您需要更多帮助,请告诉我。

关于iphone - 如何改进 iOS 中的 TWTweetComposeViewController 代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8291345/

相关文章:

ios - iphone可以拍红眼照片吗

iphone - 在 Safari 中打开 WebView

iphone - Root.plist 文件不可编辑

ios - Objective-C:方法的返回值和Completion block ,它们是如何执行的?

iphone - 当用户向下滚动 UITableView,经过最后一个单元格时,为什么我的应用程序退出?

iphone - 从字符串中删除 Html 格式

iphone - UITableViewCell 文本标签被截断

ios - Xcode 5 中的 UIViewController - socket 未显示

ios - PresentViewController iOS 上的 PushView

ios - 当应用程序运行时,本地通知不会弹出,但当应用程序处于后台时,本地通知会弹出