0

i'm trying to share image on twitter by using new api statuses/update_with_media. Then i searched and got below code from dev.twitter.com/discussion page. Here the code is below.

enter code here

- (NSString *) _uploadImage:(UIImage *)image requestType:(MGTwitterRequestType)requestType responseType:(MGTwitterResponseType)responseType
{

    NSString *boundary = @"----------------------------991990ee82f7";

    NSURL *finalURL = [NSURL URLWithString:@"http://upload.twitter.com/1/statuses/update_with_media.json"];
    if (!finalURL) 
    {
        return nil;
    }

    NSLog(@"-> Open Connection: %@", finalURL);


    OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL
                                                                      consumer:self.consumer
                                                                         token:_accessToken 
                                                                         realm: nil
                                                             signatureProvider:nil];

    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPShouldHandleCookies:NO];

    // Set headers for client information, for tracking purposes at Twitter.
    [theRequest setValue:_clientName    forHTTPHeaderField:@"X-Twitter-Client"];
    [theRequest setValue:_clientVersion forHTTPHeaderField:@"X-Twitter-Client-Version"];
    [theRequest setValue:_clientURL     forHTTPHeaderField:@"X-Twitter-Client-URL"];


    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [theRequest setValue:contentType forHTTPHeaderField:@"content-type"];

    NSMutableData *body = [NSMutableData dataWithLength:0];
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"media_data[]\"; filename=\"1.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];  
    [body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Honeymoon uploads image\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

    // --------------------------------------------------------------------------------
    // modificaiton from the base clase
    // our version "prepares" the oauth url request
    // --------------------------------------------------------------------------------
    [theRequest prepare];

    [theRequest setHTTPBody:body];

    // Create a connection using this request, with the default timeout and caching policy, 
    // and appropriate Twitter request and response types for parsing and error reporting.
    MGTwitterHTTPURLConnection *connection;
    connection = [[MGTwitterHTTPURLConnection alloc] initWithRequest:theRequest 
                                                            delegate:self 
                                                         requestType:requestType 
                                                        responseType:responseType];

    if (!connection) 
    {
        return nil;
    } 
    else 
    {
        [_connections setObject:connection forKey:[connection identifier]];
        //[connection release];
    }

    return [connection identifier];  
}

Then i was implemented this code in SA_OAuthtwitterengine because i'm using oauth method for post text or image to twitter because my application deployment target is starting form 3.0(ios,ipod), so i try to this method.

Now my problem is how to create code for send image to twitter with message.

i try to send image to twitter using the below type

[_engine sendupdate:@"http://www.prepare-community.com/ShareFiles/index-fr.jpg"];

i got connection error that means 401 error.

could you please send me how to send image to twitter using above code?

raman
  • 11
  • 4
  • http://stackoverflow.com/questions/29555971/ios-unable-to-upload-media-with-twitter-fabric-new-sdk/36509939#36509939 – jose920405 Apr 08 '16 at 21:58

1 Answers1

0

Here is the solution for image uploading using new twitter api update_with_media. Using this api you can upload image direct way to twitter account. The above _uploadimage code should be placed first in SA_OAuthtwitter engine .m file and decalred in .h file.

And make a small correct that is where placed filenamed=1.jpg instead of 1.png.

And now just put the below code in MGTwitterengine.m class. here the code is below

    - (NSString *)_uploadImage:(UIImage *)image withStatus:(NSString *)status
{
     return [self _uploadImage:image withStatus:status requestType:MGTwitterImageRequest responseType:MGTwitterStatuses];
}

And then we use this code for upload image to twitter. that is,

[_engine _uploadImage:yourpicture withStatus:yourstatus];

Hope it will work for you....

raman
  • 11
  • 4