简体   繁体   English

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

[英]Open Twitter app from my iOS 5 app

I am trying to open the Twitter app from within my app in iOS 5, but it won't open.我试图从 iOS 5 中的应用程序中打开 Twitter 应用程序,但它打不开。 Any help would be appreciated, I have included the code I am using below.任何帮助将不胜感激,我在下面包含了我正在使用的代码。

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

Please help me, and thanks in advance!请帮助我,并在此先感谢!

If you are just trying to open the actual Twitter app then the code is如果您只是想打开实际的 Twitter 应用程序,那么代码是

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

Do you want to launch the Twitter app, Or just send tweets from within your app?你想启动 Twitter 应用程序,还是只是从你的应用程序中发送推文? I believe that the code you are showing above is to Launch Twitters preferences in your settings app... Which I also believe has been disallowed in 5.1我相信您在上面显示的代码是在您的设置应用程序中启动 Twitters 首选项...我也相信这在 5.1 中已被禁止

If you are looking to add Twitter integration to your app Apple provides great sample code to show you how to use Twitter with the built in Twitter frameworks in iOS 5.如果您希望将 Twitter 集成添加到您的应用程序中,Apple 会提供出色的示例代码,向您展示如何将 Twitter 与 iOS 5 中内置的 Twitter 框架一起使用。

Now I recommend you download this sample code and see what else is required to send a tweet (like checking CanTweetStatus) but I'm attaching a basic idea of how to send a tweet in this post.现在我建议您下载此示例代码并查看发送推文还需要什么(例如检查 CanTweetStatus),但我在这篇文章中附上了如何发送推文的基本概念。

https://developer.apple.com/library/ios/#samplecode/Tweeting/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011191 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];
                }];
            }
        }
    }];
}

Good Luck!祝你好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM