通过应用程序发送推文而不需要iPhone中的推文

时间:2013-08-30 10:58:34

标签: ios twitter social-networking

如果我使用 TWTweetComposeViewController 或Twitter的社交框架,则会显示默认的推文表以发布推文。我不希望弹出的tweetsheet对话框,我想发送推文而不使用它。我该怎么办?

2 个答案:

答案 0 :(得分:0)

你可以用技巧。我在我的项目中做了这个,我把这个方法Bellow希望这个帮助是你

-(void)TwitteFromShareBtnApear
{

        Class TWTweetComposeViewControllerClass = NSClassFromString(@"TWTweetComposeViewController");

        if (TWTweetComposeViewControllerClass != nil) {
            if([TWTweetComposeViewControllerClass respondsToSelector:@selector(canSendTweet)]) {
                TWTweetComposeViewController *twitter = [[TWTweetComposeViewController alloc] init];



                [twitter setInitialText:@"twitter text"];

               [twitter addImage:[UIImage imageNamed:@"icon.png"]];
                [twitter addURL:[NSURL URLWithString:self.strsmallUrl]];
            [self findSubViewofTwitter:twitter.view];
            [self presentViewController:twitter animated:YES completion:nil];

                twitter.completionHandler = ^(TWTweetComposeViewControllerResult res) {

                    if(res == TWTweetComposeViewControllerResultDone)
                    {

                     //done alert


                    }
                    else if(res == TWTweetComposeViewControllerResultCancelled)
                    {
                    //cancel alert
                    }

                    [self dismissModalViewControllerAnimated:YES];

                };
            }
        }

}
- (void) findSubViewofTwitter:(UIView*)theView
{
    for (UIView * subview in theView.subviews)
    {
        subview.hidden=TRUE;

        if ([subview isKindOfClass:[UIButton class]])
        {
            UIButton *btn = (UIButton *)subview;

            if([btn.titleLabel.text isEqualToString:@"Send"])
            {
                [btn sendActionsForControlEvents:UIControlEventTouchUpInside];
            }
        }
        [self findSubViewofTwitter:subview];
    }
}

请看一下关于TWTweetComposeViewControllerClass

的非常好的解释

答案 1 :(得分:-1)

ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];

        [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error){
            if (granted) {

                NSArray *accounts = [accountStore accountsWithAccountType:accountType];
                twitterAccount = [accounts objectAtIndex:0];
            }
        }];

        if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
        {
            SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
            [tweetSheet setInitialText:@"tweeting from my own app! :)"];
            [tweetSheet addURL:[NSURL URLWithString:@"www.web.com"]];
            [self presentViewController:tweetSheet animated:YES completion:nil];
        }
        else
        {
            UIAlertView *alertView = [[UIAlertView alloc]
                                      initWithTitle:@"Sorry"
                                      message:@"You can't send a tweet right now, make sure your device has an internet connection and you have at least one Twitter account setup"
                                      delegate:self
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];
            [alertView show];
        }

试试这段代码......