【问题标题】:Tweet, without using the tweet sheet推文,不使用推文表
【发布时间】:2013-09-10 03:27:57
【问题描述】:

我正在使用以下代码通过 twitter 分享内容(来自UITextViewUIImageView

-(void)shareViaTweet:(NSString *)shareMessage{
if ([SLComposeViewController isAvailableForServiceType:SLServiceTypeTwitter])
{
    SLComposeViewController *tweetSheet = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeTwitter];
    [tweetSheet setInitialText:[NSString stringWithFormat:@"%@",shareMessage]];
    if (self.imageString)
    {
        [tweetSheet addImage:[UIImage imageNamed:self.imageString]];
    }

    if (self.urlString)
    {
        [tweetSheet addURL:[NSURL URLWithString:self.urlString]];
    }
    [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];
}

}

但我需要分享这个,而不使用弹出视图(我认为是推特表)。这是因为下面的代码,

[self presentViewController:tweetSheet animated:YES completion:nil];

当我点击我的应用程序的“分享”按钮时,我需要在 Twitter 上发布。

已编辑:

- (IBAction)doneButtonClicked:(id)sender
{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
NSString *message = messageTextView.text;
//hear before posting u can allow user to select the account
NSArray *arrayOfAccons = [account accountsWithAccountType:accountType];
for(ACAccount *acc in arrayOfAccons)
{
    NSLog(@"%@",acc.username); //in this u can get all accounts user names provide some UI for user to select,such as UITableview
}


NSURL *url = [NSURL URLWithString:@"https://api.twitter.com"
              @"/1.1/statuses/user_timeline.json"];
             NSDictionary *params = @{@"screen_name" : message,
                                     @"forKey":@"status",
                                      @"trim_user" : @"1",
                                      @"count" : @"1"};
// Request access from the user to access their Twitter account

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

 {
     if (granted == YES)
     {
         // Populate array with all available Twitter accounts
         NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
         if ([arrayOfAccounts count] > 0)
         {
             //use the first account available
             ACAccount *acct = [arrayOfAccounts objectAtIndex:0]; //hear this line replace with selected account. than post it :)




             SLRequest *request =
             [SLRequest requestForServiceType:SLServiceTypeTwitter
                                requestMethod:SLRequestMethodPOST
                                          URL:url
                                   parameters:params];


             //Post the request
             [request setAccount:acct];

             //manage the response
             [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
              {
                  if(error)
                  {
                      //if there is an error while posting the tweet
                      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Error in posting" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                      [alert show];

                  }
                  else
                  {
                      // on successful posting the tweet
                      NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                      UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Successfully posted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                      [alert show];


                  }
              }];

         }
         else
         {
             UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"You have no twitter account" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [alert show];

         }
     }
     else
     {
         //suppose user not set any of the accounts
         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Permission not granted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
         [alert show];

     }
 } ];


//[widgetsHandler closeWidget:nil];


//[self postImage:shareImageView.image withStatus:messageTextView.text];
}

更新:错误

【问题讨论】:

    标签: iphone ios twitter


    【解决方案1】:

    要发送图像,您需要这样做


      [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
     {
       if (granted == YES)
        {
          // Populate array with all available Twitter accounts
         NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
         if ([arrayOfAccounts count] > 0)
          {
               //use the first account available
           ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
    
           //create this request 
           SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL   URLWithString:@"https://api.twitter.com"@"/1.1/statuses/update_with_media.json"] parameters:  [NSDictionary dictionaryWithObject:message forKey:@"status"]];
           UIImage *imageToPost = [UIImage imageNamed:@"image.jpg"];
           NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0f);//set the compression quality
          [postRequest addMultipartData:imageData withName:@"media" type:@"image/jpeg" filename:@"image.jpg"];
    
       //set account and same as above code 
    
    ....
     ....
    


    【讨论】:

    • 这给了我一个错误,请参阅编辑。我通过this 链接。是的,在调试中出现该错误需要更多时间。我不知道如何解决它。
    • 你设置了所有的 URLSchemes
    • 它对我来说非常有效,我发布了该代码。它应该可以尝试一个测试项目并从中发布,看看它是否正在发布
    • 我需要将消费者密钥、消费者密码放在我的应用程序中的什么位置。可能这就是问题所在。
    【解决方案2】:

    如果您想在不使用推文表的情况下分享推文,请参阅我的答案,它将在不使用推文表的情况下发布在推特墙上,请参阅hear,并在设备中设置推特帐户。希望这会有所帮助

    不幸的是,TWRequest 类在 iOS 6 中已弃用,但我们也可以在社交框架中使用 SLRequest

    这个答案与旧答案相似 我注释掉了我不想要的东西,但是如果你想选择使用哪个帐户,那么取消注释注释代码


     - (IBAction)doneButtonClicked:(id)sender
        {
          ACAccountStore *account = [[ACAccountStore alloc] init];
          ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
          NSString *message = _textView.text;
    
        //    NSArray *arrayOfAccons = [account accountsWithAccountType:accountType];
        //    for(ACAccount *acc in arrayOfAccons)
        //    {
        //        NSLog(@"%@",acc.username);
        //        NSDictionary *properties = [acc dictionaryWithValuesForKeys:[NSArray arrayWithObject:@"properties"]];
        //        NSDictionary *details = [properties objectForKey:@"properties"];
        //        NSLog(@"user name = %@",[details objectForKey:@"fullName"]);
        //        NSLog(@"user_id  =  %@",[details objectForKey:@"user_id"]);
        //    }
    
    
        // Request access from the user to access their Twitter account
        [account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)
        {
            if (granted == YES)
            {
             // Populate array with all available Twitter accounts
                NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
                if ([arrayOfAccounts count] > 0)
                 {
                   //use the first account available
                   ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
    
                   // Build a twitter request
                   // TWRequest *postRequest = [[TWRequest alloc] initWithURL:
                   // [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:message forKey:@"status"] requestMethod:TWRequestMethodPOST]; //commented the  deprecated method of TWRequest class
    
                 SLRequest *postRequest = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:[NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:message forKey:@"status"]]; //use this method instead
    
    
    
                 //Post the request
                 [postRequest setAccount:acct];//set account
    
                 //manage the response
                 [postRequest performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
                  {
                     if(error)
                      {
                          //if there is an error while posting the tweet
                          UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Error in posting" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                          [alert show];
                          [alert release];
                      }
                      else
                      {
                          // on successful posting the tweet
                          NSLog(@"Twitter response, HTTP response: %i", [urlResponse statusCode]);
                          UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"Successfully posted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                          [alert show];
                          [alert release];
    
                      }
                  }];
    
             }
             else
             {
                 UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"You have no twitter account" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                 [alert show];
                 [alert release];
             }
         }
         else
         {
             //suppose user not set any of the accounts
             UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Twitter" message:@"You have no twitter account" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
             [alert show];
             [alert release];
         }
     } ];
    
    [account release];
    
     }
    


    【讨论】:

    • 方法名称[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error)TWRequest *postRequest = [[TWRequest alloc] initWithURL: [NSURL URLWithString:@"http://api.twitter.com/1/statuses/update.json"] parameters:[NSDictionary dictionaryWithObject:message forKey:@"status"] requestMethod:TWRequestMethodPOST]; 已弃用,您能帮我解决一下吗?
    • 你添加了#import #import #import #import 所有这些标题还有框架
    • 是的,但这是不使用我遵循的推文表的方式。
    • 尝试在您的代码中设置“SLRequestMethodPOST”。你正在使用“SLRequestMethodGET”
    • 否,但它显示“已成功发布”我是否需要添加 Twitter 应用 ID 或其他地方。查看修改。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-11
    • 1970-01-01
    • 2020-09-07
    • 2017-08-22
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多