How can I improve my TWTweetComposeViewController code in iOS?

谁说胖子不能爱 提交于 2019-12-21 05:43:09

问题


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

回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/8291345/how-can-i-improve-my-twtweetcomposeviewcontroller-code-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!