0

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

相反,我想要一个像 Instagram 这样的界面,用户只需选择 twitter 并按下“分享”,这样就很容易了。

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

//  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.");
}];
4

1 回答 1

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 

....
 ....

以上来自此链接,此代码对我有用。

您可以在这里找到如何通过媒体发布 Twitter 的更新

于 2014-05-28T14:37:21.263 回答