【问题标题】:Making Post Request to twitter with IOS使用 IOS 向 twitter 发出 Post 请求
【发布时间】:2014-07-21 13:16:53
【问题描述】:

我对 POST 到 twitter 的概念不熟悉。我查看了here,以了解如何从 twitter 获取内容。但我找不到任何关于如何在 Twitter 上发布内容的示例。

我如何告诉 twitter userXYZ 正在发布一条推文?推文本身的正文怎么样。

再说一次,我对这个概念很陌生。

【问题讨论】:

    标签: ios twitter


    【解决方案1】:

    您可以在 API 文档中轻松找到这些内容。我想你是looking for this

    SO 中已经提出了一些问题。你也可以check this

    【讨论】:

    • 我已经看到了。我是菜鸟。我不知道该怎么做。在示例代码中,有一个用于 screen_name 的槽。假设用户在 iOS 的设置部分登录,我如何获取 screen_name?我什至需要一个昵称还是这个请求会自动发布到正确的 Twitter 帐户上?
    • @Calimari3 > 发布状态即可,无需考虑screen_name。
    【解决方案2】:
    Using Twitter SDK import #import<Twitter/Twitter.h> in your .h or .m file
    
    
    if ([TWTweetComposeViewController canSendTweet])
    {
    
        NSURL* aURL = [NSURL URLWithString:@"your image Url"];
        NSData* data = [[NSData alloc] initWithContentsOfURL:aURL];
        UIImage *img1 = [UIImage imageWithData:data];
        // Initialize Tweet Compose View Controller
        TWTweetComposeViewController *vc = [[TWTweetComposeViewController alloc] init];
        // Settin The Initial Text
    
        [vc setInitialText:@"Setting The Initial Text"];
        [vc addImage:img1];
        // Adding a URL
        strWEBTwitter = @"Pass Your URL here"
        [vc addURL:[NSURL URLWithString:strWEBTwitter]];
        // Setting a Completing Handler
        [vc setCompletionHandler:^(TWTweetComposeViewControllerResult result)
        {
            [self dismissViewControllerAnimated:YES completion:nil];
            NSLog(@"Result : %d", result);
            if (result == TWTweetComposeViewControllerResultDone)
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Message" message:@"Thanks for sharing." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];                [alertView show];
    
            }
    
            else if (result == TWTweetComposeViewControllerResultCancelled)
            {
                NSLog(@"Cancle")
            }
    
        }];
        // Display Tweet Compose View Controller Modally
        [self presentViewController:vc animated:YES completion:nil];
    } else
    {
        // Show Alert View When The Application Cannot Send Tweets
        NSString *message = @"There are no Twitter accounts configured. You can add or create a Twitter account in Settings.";
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"No Twitter Accounts" message:message delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"Cancel",nil];
        [alertView show];
    }
    

    【讨论】:

      【解决方案3】:

      谢谢拉沙德。这就是我现在正在使用的。有用。

      - (void)testPost {
      
          ACAccountStore *accountStore = [[ACAccountStore alloc] init];
          ACAccountType *accountType = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
          [accountStore requestAccessToAccountsWithType:accountType options:nil completion:^(BOOL granted, NSError *error)
           {
             if (granted)
               {
                 NSArray *accounts = [accountStore accountsWithAccountType:accountType];
                 if (accounts.count)
                   {
                     NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
                     [dict setValue:@"check Check CHECK" forKey:@"status"];
      
                     NSString *retweetString = [NSString stringWithFormat:@"https://api.twitter.com/1.1/statuses/update.json"];
                     NSURL *retweetURL = [NSURL URLWithString:retweetString];
                     SLRequest *request = [SLRequest requestForServiceType:SLServiceTypeTwitter requestMethod:SLRequestMethodPOST URL:retweetURL parameters:dict];
      
                     request.account = [accounts objectAtIndex:0];
      
                     [request performRequestWithHandler:^(NSData *responseData1, NSHTTPURLResponse *urlResponse, NSError *error)
                      {
      
                        if (responseData1)
                          {
      
                            NSError *error1 = nil;
                            id response = [NSJSONSerialization JSONObjectWithData:responseData1 options:NSJSONReadingMutableLeaves error:&error1];
      
                            NSLog(@"%@", response);    
                          }
                      }];
                   }
               }
      
           }];
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-05-30
        • 2018-08-19
        • 1970-01-01
        • 2019-11-23
        • 2021-11-12
        • 1970-01-01
        • 2021-10-26
        相关资源
        最近更新 更多