objective c - iOS - Unable to Upload media with Twitter/Fabric New SDK -
i want post photo twitter ios app. can post tweet without media when trying attach media throws error.
i following twitter documentation , according first need upload media https://upload.twitter.com/1.1/media/upload.json , able attach tweet using media-id.
here code uploading media.
app crashing @ urlrequestwithmedthod call.
help me resolve issue.
uiimage *image = [uiimage imagenamed:@"shareit.png"]; nsdata *imagedata = uiimagejpegrepresentation(image, 0.7); nsstring *statusesshowendpoint = @"https://upload.twitter.com/1.1/media/upload.json"; nsdictionary *params = @{@"media" : imagedata}; nserror *clienterror; nsurlrequest *request = [[[twitter sharedinstance] apiclient] urlrequestwithmethod:@"post" url:statusesshowendpoint parameters:params error:&clienterror]; if (request) { [[[twitter sharedinstance] apiclient] sendtwitterrequest:request completion:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) { if (data) { // handle response data e.g. nserror *jsonerror; nsdictionary *json = [nsjsonserialization jsonobjectwithdata:data options:0 error:&jsonerror]; nslog(@"%@",json); } else { nslog(@"error: %@", connectionerror); } }]; } else { nslog(@"error: %@", clienterror); }
well pretty simple. missing conversion of imagedata base64encodedstring. here solution.
nsstring *media = @"https://upload.twitter.com/1.1/media/upload.json"; nsdata *imagedata = uiimagejpegrepresentation(image, 0.9); nsstring *imagestring = [imagedata base64encodedstringwithoptions:0]; nserror *error; nsurlrequest *request = [[[twitter sharedinstance] apiclient] urlrequestwithmethod:@"post" url:media parameters:@{@"media":imagestring} error:&error]; [[[twitter sharedinstance] apiclient] sendtwitterrequest:request completion:^(nsurlresponse *urlresponse, nsdata *data, nserror *connectionerror) { nserror *jsonerror; nsdictionary *json = [nsjsonserialization jsonobjectwithdata:data options:0 error:&jsonerror]; nslog(@"media id : %@",[json objectforkey:@"media_id_string"]); // post tweet media_id }];