问题
iOS 5 has deep twitter integration. How can I check if the user has an account in the built-in twitter app (meaning he uses it), and then do a UIApplication openURL:
to trigger following a user or pre-composing a tweet?
One of the new features for developers in the iOS 5 SDK is support for the Twitter API. Apple has made it easy for developers to add Twitter support to their apps and to allow users to easily control whether or not an app has access to post to their Twitter account.
According to this site it is possible. How's that Twitter Framework called?
回答1:
Try this:
- (void)tweetButtonPressed:(id)sender
{
Class tweetComposer = NSClassFromString(@"TWTweetComposeViewController");
if( tweetComposer != nil ) {
if( [TWTweetComposeViewController canSendTweet] ) {
TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];
[tweetViewController setInitialText:@"This is a test."]; // set your initial text
tweetViewController.completionHandler = ^(TWTweetComposeViewControllerResult result) {
if( result == TWTweetComposeViewControllerResultDone )
{
// user is done with composing
}
else if( result == TWTweetComposeViewControllerResultCancelled )
{
// user has cancelled composing
}
[self dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:tweetViewController animated:YES completion:nil];
}
else {
// The user has no account setup
}
}
else {
// no Twitter integration
}
}
回答2:
For the first part of your question...
ACAccountStore *accountStore = [[[ACAccountStore alloc] init] autorelease];
ACAccountType *twitterAccountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSArray *twitterAccounts = [accountStore accountsWithAccountType:twitterAccountType];
This is how we're doing it in DETweetComposeViewController.
回答3:
You could try something like:
BOOL didOpenTwitterApp = [UIApplication openURL:[NSURL URLWithString:@"twitter:///user?screen_name=senior"]];
This will return false if the app can't open the official Twitter app.
If you just want to create a new Tweet, use the native API
来源:https://stackoverflow.com/questions/8248068/is-it-possible-to-check-if-a-user-has-a-twitter-account-in-the-built-in-twitter