从我的iOS 5应用程序打开Twitter应用程序

时间:2012-04-04 04:36:59

标签: objective-c ios twitter

我正在尝试从iOS 5中的应用程序中打开Twitter应用程序,但它无法打开。任何帮助将不胜感激,我已经包含了我在下面使用的代码。

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"prefs:root=TWITTER"]];

请帮助我,并提前致谢!

2 个答案:

答案 0 :(得分:4)

如果您只是想打开实际的Twitter应用程序,那么代码就是

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"twitter://"]];

答案 1 :(得分:0)

您想推出Twitter应用,还是仅在您的应用中发送推文?我相信您上面显示的代码是在您的设置应用程序中启动Twitters首选项...我也相信在5.1中不允许这样做

如果您希望将Twitter集成添加到您的应用程序,Apple提供了很好的示例代码,向您展示如何在iOS 5中使用内置的Twitter框架来使用Twitter。

现在我建议你下载这个示例代码,看看发送推文还需要什么(比如检查CanTweetStatus),但我附上了如何在这篇文章中发送推文的基本概念。

https://developer.apple.com/library/ios/#samplecode/Tweeting/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011191

- (IBAction)sendCustomTweet:(id)sender {
    // Create an account store object.
    ACAccountStore *accountStore = [[ACAccountStore alloc] init];

    // Create an account type that ensures Twitter accounts are retrieved.
    ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

    // Request access from the user to use their Twitter accounts.
    [accountStore requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error) {
        if(granted) {
            // Get the list of Twitter accounts.
            NSArray *accountsArray = [accountStore accountsWithAccountType:accountType];

            // For the sake of brevity, we'll assume there is only one Twitter account present.
            // You would ideally ask the user which account they want to tweet from, if there is more than one Twitter account present.
            if ([accountsArray count] > 0) {
                // Grab the initial Twitter account to tweet from.
                ACAccount *twitterAccount = [accountsArray objectAtIndex:0];

                // Create a request, which in this example, posts a tweet to the user's timeline.
                // This example uses version 1 of the Twitter API.
                // This may need to be changed to whichever version is currently appropriate.
                TWRequest *postRequest = [[TWRequest alloc] initWithURL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:@"Hello. This is a tweet." forKey:@"status"] requestMethod:TWRequestMethodPOST];

                // Set the account used to post the tweet.
                [postRequest setAccount:twitterAccount];

                // Perform the request created above and create a handler block to handle the response.
                [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
                    NSString *output = [NSString stringWithFormat:@"HTTP response status: %i", [urlResponse statusCode]];
                    [self performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
                }];
            }
        }
    }];
}

祝你好运!