1

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

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

请帮助我,并提前感谢!

4

2 回答 2

4

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

 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"twitter://"]];
于 2012-04-04T05:52:59.470 回答
0

你想启动 Twitter 应用程序,还是只是从你的应用程序内发送推文?我相信您上面显示的代码是在您的设置应用程序中启动 Twitters 首选项...我也相信这在 5.1 中已被禁止

如果您希望将 Twitter 集成添加到您的应用程序中,Apple 提供了很好的示例代码来向您展示如何将 Twitter 与 iOS 5 中的内置 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];
                }];
            }
        }
    }];
}

祝你好运!

于 2012-04-04T05:15:53.497 回答