7

在 iOS 5.0 中,我从我的应用程序中打开 Twitter 设置

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

但是,这个功能在 iOS 5.1 中被删除了,因此我无法打开 twitter 设置。

现在我正在使用

 + (void)makeRequestsWithURL: (NSURL *)url {
// 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];
[self canTweetStatus];

// 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:url parameters:nil 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]];
                iOS5Twitter *twitter5 = [[iOS5Twitter alloc] init];
                [twitter5 performSelectorOnMainThread:@selector(displayText:) withObject:output waitUntilDone:NO];
                [twitter5 release];                }];
        }
    }

}];

}

为了提出请求,我可以检查我是否已登录

if ([TWTweetComposeViewController canSendTweet])

但现在我想要:如果我没有登录,它会显示一个如图所示的警报,并想要移动到 twitter 设置。是否可以 ?或者我必须手动去 ti twitter 设置?在此处输入图像描述

4

4 回答 4

17

这有点棘手,我通过删除子视图得到*TWTWeetComposeViewController*,所以它只在用户未登录时显示警报,通过点击设置按钮,我们可以在我的应用程序中打开设置页面。

     + (void)setAlertForSettingPage :(id)delegate 
    {
     // Set up the built-in twitter composition view controller.
        TWTweetComposeViewController *tweetViewController = [[TWTweetComposeViewController alloc] init];


        // Create the completion handler block.
        [tweetViewController setCompletionHandler:^(TWTweetComposeViewControllerResult result) {
            [delegate dismissModalViewControllerAnimated:YES];
        }];

        // Present the tweet composition view controller modally.
        [delegate presentModalViewController:tweetViewController animated:YES];
        //tweetViewController.view.hidden = YES;
        for (UIView *view in tweetViewController.view.subviews){
            [view removeFromSuperview];
        }

     } 

在这里, deleate 是您的视图控制器,如果您在视图控制器中使用此方法,只需使用self而不是delegate.

于 2012-07-04T12:49:51.657 回答
8

iOS 6 使用 SLComposeViewController 而不是 TWTweetComposeViewController,所以现在如果你想让它在 iOS 6 和 iOS 5 上工作,你必须这样做:

    UIViewController *tweetComposer;

    if([SLComposeViewController class] != nil)
    {
        tweetComposer = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

        [(SLComposeViewController *)tweetComposer setCompletionHandler:^(SLComposeViewControllerResult result)
         {
            // do whatever you want
         }];
    }
    else
    {
        tweetComposer = [[TWTweetComposeViewController alloc] init];

        [(TWTweetComposeViewController *)tweetComposer setCompletionHandler:^(TWTweetComposeViewControllerResult result)
         {
            // do whatever you want
         }];
    }

    for (UIView *view in [[tweetComposer view] subviews])
        [view removeFromSuperview];

    [self presentViewController:tweetComposer animated:NO completion:nil];
于 2012-11-08T16:56:35.227 回答
1

基于以上高级和 PJR 的答案,这对我有用。

与 Senior 的相同,除了在完成处理程序中有一个 dimissViewController。对我来说,这解决了我从设置返回应用程序后空视图控制器会留下来的问题。它会覆盖我的视图控制器并使其无用。

出色的解决方案,感谢 Senior 和 PJR。

UIViewController *tweetComposer;

if([SLComposeViewController class] != nil)
{
    tweetComposer = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];

    [(SLComposeViewController *)tweetComposer setCompletionHandler:^(SLComposeViewControllerResult result)
     {
            [self dismissViewControllerAnimated:NO completion:nil];
     }];
}
else
{
    tweetComposer = [[TWTweetComposeViewController alloc] init];

    [(TWTweetComposeViewController *)tweetComposer setCompletionHandler:^(TWTweetComposeViewControllerResult result)
     {
           [self dismissViewControllerAnimated:NO completion:nil];

     }];
}

for (UIView *view in [[tweetComposer view] subviews])
    [view removeFromSuperview];

[self presentViewController:tweetComposer animated:NO completion:nil];
于 2013-03-11T15:34:55.287 回答
0

要删除帖子视图,请使用以下代码:

for (UIView *view in [[tweetComposer view] subviews])
    [view removeFromSuperview];

用这个:

tweetComposer.view.alpha = 0;

适用于推特和脸书。

于 2013-09-09T05:16:15.997 回答