如何在没有在ios中显示对话的情况下在Twitter上发布?

时间:2014-05-28 13:11:40

标签: ios twitter slcomposeviewcontroller

我想在Twitter上发布我的状态。我想添加图片,但我不想要分享对话。

相反,我喜欢像Instagram这样的界面,用户只需选择推特并按下“分享”,这样很容易。

到目前为止,这是我正在运行的代码:

//  Create an instance of the Tweet Sheet
SLComposeViewController *tweetSheet = [SLComposeViewController
                                       composeViewControllerForServiceType:
                                       SLServiceTypeTwitter];

// Sets the completion handler.  Note that we don't know which thread the
// block will be called on, so we need to ensure that any required UI
// updates occur on the main queue
tweetSheet.completionHandler = ^(SLComposeViewControllerResult result) {
    switch(result) {
            //  This means the user cancelled without sending the Tweet
        case SLComposeViewControllerResultCancelled:
            break;
            //  This means the user hit 'Send'
        case SLComposeViewControllerResultDone:
            break;
    }
};

//  Set the initial body of the Tweet
[tweetSheet setInitialText:@"Socia"];

//  Adds an image to the Tweet.  For demo purposes, assume we have an
//  image named 'larry.png' that we wish to attach
if (![tweetSheet addImage:[UIImage imageNamed:@"icon120x120.png"]]) {
    NSLog(@"Unable to add the image!");
}

//  Add an URL to the Tweet.  You can add multiple URLs.
if (![tweetSheet addURL:[NSURL URLWithString:@"http://stackoverflow.com/questions/ask?title="]]){
    NSLog(@"Unable to add the URL!");
}

//  Presents the Tweet Sheet to the user
[self presentViewController:tweetSheet animated:NO completion:^{
    NSLog(@"Tweet sheet has been presented.");
}];

1 个答案:

答案 0 :(得分:2)

 [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 

....
 ....

以上内容来自link,此代码适用于我。

在这里,您可以找到如何使用媒体here发布Twitter的更新。