objective-c - 从我的iOS 5应用打开Twitter应用

标签 objective-c ios twitter

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

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

请帮助我,在此先感谢!

最佳答案

您要启动Twitter应用程序,还是仅从应用程序内发送推文?我相信您上面显示的代码是在您的设置应用中启动Twitter偏好设置...我也认为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];
                }];
            }
        }
    }];
}

祝好运!

关于objective-c - 从我的iOS 5应用打开Twitter应用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10005000/

相关文章:

ios - 选择照片后 UIPickerController 内存泄漏

ios - 从 SKScene 中删除节点时调用方法

objective-c - 目标/iOS : NSCFString not NSString?

ios - NSExpression 使用 sqrt : function results in NSInvalidArgumentException

ios - 在 swift 4 IOS 应用程序中手动添加和引用控件

objective-c - CoreMIDI 制造商预设

ios - 如何计算以特定字符串开头的文件数?

Angularjs 有在社交网络中分享的问题

python - 使用 Python 将 JSON 数据漂亮地打印到文件中

javascript - 使用 javascript 或 jquery [twitter 卡片] 动态添加元标记