objective c - how to post image to twitter by using http://upload.twitter.com/1/statuses/update_with_media for below ios 5.0 -


i'm trying share image on twitter using new api statuses/update_with_media. searched , got below code dev.twitter.com/discussion page. here code 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 client information, tracking purposes @ 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 base clase     // our version "prepares" oauth url request     // --------------------------------------------------------------------------------     [therequest prepare];      [therequest sethttpbody:body];      // create connection using request, default timeout , caching policy,      // , appropriate twitter request , response types parsing , 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 implemented code in sa_oauthtwitterengine because i'm using oauth method post text or image twitter because application deployment target starting form 3.0(ios,ipod), try method.

now problem how create code send image twitter message.

i try send image twitter using below type

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

i got connection error means 401 error.

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

here solution image uploading using new twitter api update_with_media. using api can upload image direct way twitter account. above _uploadimage code should placed first in sa_oauthtwitter engine .m file , decalred in .h file.

and make small correct placed filenamed=1.jpg instead of 1.png.

and put below code in mgtwitterengine.m class. here code below

    - (nsstring *)_uploadimage:(uiimage *)image withstatus:(nsstring *)status {      return [self _uploadimage:image withstatus:status requesttype:mgtwitterimagerequest responsetype:mgtwitterstatuses]; } 

and use code upload image twitter. is,

[_engine _uploadimage:yourpicture withstatus:yourstatus];

hope work you....


Comments