如何改进 iOS 中的 TWTweetComposeViewController 代码?

发布于 2023-02-23 00:20:03 字数 1473 浏览 26 评论 0原文

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

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

  1. Do a proper Run Time check?? (since it may crash on IOS 4 and earlier) EVEN though it did not.
  2. Weak Link the Twitter frame work

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

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");
    }
}

I have implemented the following code to do a Twitter Share. In my code I try to test for iOS 5 and if that does not work, I go back to the old way of sharing using ShareKit's Twitter code.

I showed the code to a co worker and he suggested that my code may have flaws and that I need to do two things:

  1. Do a proper Run Time check?? (since it may crash on IOS 4 and earlier) EVEN though it did not.
  2. Weak Link the Twitter frame work

Can someone kindly explain what a proper run time check would be? and Why Weak Link?

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");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

傲影 2023-03-02 00:20:04

弱链接仅仅意味着框架不需要在设备上。 或者换句话说,当您向项目添加框架时,应用程序将要求该框架位于设备上。 因此,如果您需要一个框架,但它不存在,那么该应用程序将会崩溃。 在您的情况下,如果您希望该应用程序在 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 的用户混淆。

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

A weak link simply means the a framework is not required to be on the device. Or put another way, when you add frameworks to your project, the app will require that framework to be on the device. So if you require a framework to be there, and it isn't, then the app will crash. In your case, you would want to weak link the twitter framework if you want the app to run on iOS version prior to iOS 5 (ie iOS 4.x).

A proper run time check means you should load the app onto your device (running iOS 5 or later) and test the twitter feature of your app. If it crashes, then you know you have a problem.

I skimmed your code and everything looks fine. I didn't run it through my complier though so I can't speak for syntax errors and such. The one change I would make is:

if ([TWTweetComposeViewController canSendTweet])

to

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

The reason why I use that is becuase that literally checks if the framework is on that device, while canSendTweet checks if the user is logged in. So I don't want to confuse a user not being logged in with a user whose device doesn't support Twitter with iOS 5.

Let me know if you need anymore help.

°如果伤别离去 2023-03-02 00:20:04

DETweetComposeViewController 是另一种选择。 它也与 iOS 4 兼容。

DETweetComposeViewController is another option. It's compatible with iOS 4 too.

萌梦深 2023-03-02 00:20:04

我认为您还泄漏了控制器(就像我见过的大多数代码示例一样)。

另一方面,您注意了有关完成处理程序的文档,并正确确保在主线程中执行 UI 工作。 我需要修复我的实现来做同样的事情。

I think you also leak the controller (as do most of the code samples I've seen).

On the other hand, you paid attention to the documentation about the completion handler, and correctly make sure you do UI work in the main thread. I need to go fix my implementation to do the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击“接受”或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文