Post Video on Vine Social Site - iphone

I am working on app where I have to upload the media on Vine Vine. I have tried following API and succesfully able to login.
NSString *abcd=[NSString stringWithFormat:#"https://api.vineapp.com/users/authenticate"];
NSURL *url = [NSURL URLWithString:abcd];
NSMutableURLRequest *request1 = [NSMutableURLRequest requestWithURL: url];
[request1 setURL:url];
[request1 setHTTPMethod:#"POST"];
NSString *stringBoundary = #"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
NSString *headerBoundary = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",
stringBoundary];
[request1 addValue:headerBoundary forHTTPHeaderField:#"Content-Type"];
NSMutableData *postBody = [NSMutableData data];
// final boundary
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"Content-Disposition: form-data; name=\"username\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"myuser" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"--%#\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"Content-Disposition: form-data; name=\"password\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"pass" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:#"--%#--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request1 setHTTPBody:postBody];
NSError *error;
NSURLResponse *response;
urlData = [NSURLConnection sendSynchronousRequest:request1 returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"responseString..:%#",responseString);
if (urlData!=nil) {
if(NSClassFromString(#"NSJSONSerialization"))
{
NSError *error = nil;
id object = [NSJSONSerialization
JSONObjectWithData:urlData
options:0
error:&error];
if(error) { }
if([object isKindOfClass:[NSDictionary class]])
{
NSDictionary *results = object;
NSLog(#"results..:%#",results);
}
}
}
}
I have read this documentation for the login process.Vine API Reference
But I am unable to implement logout and upload media API's.
Please If anyone has any suggestion then please let me know. And also tell me is it right way to post video on vine or is there any other API or library available for it.
Thanks in advance.

You need to implement post method to upload video and delete method to logout.In question you mention that you are implementing get method call. So change method type to Delete, Post , Get according to API.

Related

Posting data on server in my iphone app [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am sending data from iPhone application to server but when i submit form application crash.Please check code and suggest me advise thanks in advance.
-(IBAction)SAVE:(id)sender
{
NSString *post =[[NSString alloc] initWithFormat:#"amount=50&termid=1&state=%#&city=%#&post_title=%#&description=%#&post_price=10&post_location=asd&geo_latitude=%#&geo_longitude=%#&owner_name=%#&owner_email=abc#a.a&owner_phone=%#&post_tags=a&post_url=%#&coupon_code=a&current_userid=1&type=renew",txt4.text,txt5.text,txt6.text,latValue,longValue,txt7.text,txt8.text,txt9.text,txt10.text];
NSLog(#"String Posted:%#",post);
NSURL *url=[NSURL URLWithString:#"http://www.Example.com.mx/testing/publication.php?"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:#"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:url];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(#"%#",data);
con=[NSURLConnection connectionWithRequest:request delegate:self];
if(con){
responseData=[NSMutableData data];
}
}
-(void)uploadDataToServer
{
//Server Address URL
NSString *urlString = [NSString stringWithFormat:#"Your Server Address"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:#"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter first
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Attaching the key name #"parameter_first" to the post body
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"parameter_first\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Attaching the content to be posted ( ParameterFirst )
[body appendData:[[NSString stringWithFormat:#"%f",ParameterFirst] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter second
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Attaching the key name #"parameter_second" to the post body
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"parameter_second\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Attaching the content to be posted ( ParameterSecond )
[body appendData:[[NSString stringWithFormat:#"%f",ParameterSecond] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//parameter third
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Attaching the key name #"parameter_third" to the post body
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"parameter_third\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//Attaching the content to be posted ( ParameterThird )
[body appendData:[[NSString stringWithFormat:#"%d",ParameterThird] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//close form
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"Request = %#",[[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding]);
//setting the body of the post to the reqeust
[request setHTTPBody:body];
//now lets make the connection to the web
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"returnString %#",returnString);
NSDictionary* jsonResponse = [CommonFunctions parseJSON:returnData];
NSLog(#"Json responce %#",jsonResponse);
if ([[jsonResponse objectForKey:#"replyCode"] isEqualToString:#"success"])
{
isSuccessfulPost=YES;
}
else
{
isSuccessfulPost=NO;
}
}
Hope the answer is useful for you . You can increase/decrease the number of paramter by simply altering the body
Please add code to check.
In iOS applications I suggest using AFNetworking
for any server - client communication.

Uploading Image to server with text paramater iPhone

I am developing application which require uploading the Image from the iphone to Php Server, Following is the code i used to upload the file to server but I can see only the text parameter but the Image is not getting uploaded .
NSString *BoundaryConstant = #"----------V2ymHFg03ehbqgZCaKO6jy";
NSString* FileParamConstant = #"image";
NSURL* requestURL = [NSURL URLWithString:#"http://bb.trackfleet.biz/smscalling/upload_iphone_image.php"];
NSMutableDictionary* _params = [[NSMutableDictionary alloc] init];
[_params setObject:[NSString stringWithFormat:#"919964823246"] forKey:#"contact"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:60];
[request setHTTPMethod:#"POST"];
// post body
NSMutableData *body = [NSMutableData data];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// add params (all params are strings)
for (NSString *param in _params) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#\r\n", [_params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
}
UIImage *imageToPost=[UIImage imageNamed:#"image3.jpg"];
NSData *imageData = UIImageJPEGRepresentation(imageToPost, 1.0);
if (imageData) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: attachment; name=\"%#\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
// set the content-length
NSString *postLength = [NSString stringWithFormat:#"%d", [body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
// set URL
[request setURL:requestURL];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
NSLog(#"%#", returnString);
Problem: Image is not getting uploaded at the path where has the text is stored in database .
What I Did: For some reference, I tried Flickr API iOS app "POST size too large!" and Objective C: How to upload image and text using HTTP POST?
This may help you.I tried this post: "image upload tutorial iOS".

Upload pdf data to server

I want to send my pdf file data do server with some user data also like userId and fileName.
I have tried 2 solutions but not got the pdf file on backend. Even i am having the pdf file in my document directory and it is opening successfully.
Tried 1:
-(void)uploadDock{
NSString *fileName =[NSString stringWithFormat: #"%#.pdf",#"Inspection"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *data=[NSData dataWithContentsOfFile:path];
NSString *content=[data base64EncodedString];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([self.responseData retainCount]) {
NSLog(#"Do nothing");
[self.responseData release];
}
self.responseData=[[NSMutableData alloc]init];
NSURL *url = [NSURL URLWithString:#"http://abcgroup.delivery-projects.com:81/api/index.php?keyword=docinsert"];
//self.request = [NSMutableURLRequest requestWithURL:url
//cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
self.request = [NSMutableURLRequest requestWithURL:url];
NSLog(#"Requst %#",request);
NSString *userid=[userDefaults valueForKey:#"UIDD"];
NSString *title=fileName;
NSString *docname=fileName;
NSString *size=[NSString stringWithFormat:#"%d",[data length]];
NSString *post = [NSString stringWithFormat:#"userid=%#&title=%#&docname=%#&size=%#&content=%#",userid,title,docname,size,content];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
[self.request setHTTPMethod:#"POST"];
//NSString *POSTBoundary = [NSString stringWithFormat:#"0xKhTmLbOuNdArY"];
//[self.request addValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#\r\n", POSTBoundary] forHTTPHeaderField:#"Content-Type"];
[self.request setValue:[NSString stringWithFormat:#"%d", postData.length] forHTTPHeaderField:#"Content-Length"];
[self.request setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[self.request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:self.request delegate:self];
}
Tried 2:
I am reading the NSData of pdf file from Document directory.
/////
-(void)uploadDock{
self.condition=3;
NSString *fileName =[NSString stringWithFormat: #"%#.pdf",#"Inspection"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *data=[NSData dataWithContentsOfFile:path];
NSString *content=[data base64EncodedString];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if ([self.responseData retainCount]) {
NSLog(#"Do nothing");
}
else{
self.responseData=[[NSMutableData alloc]init];
}
NSURL *url = [NSURL URLWithString:#"http://abcgroup.delivery-projects.com:81/api/index.php?keyword=docinsert"];
//self.request = [NSMutableURLRequest requestWithURL:url
// cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
self.request = [NSMutableURLRequest requestWithURL:url];
NSLog(#"Requst %#",request);
NSString *userid=[userDefaults valueForKey:#"UIDD"];
NSString *title=fileName;
NSString *docname=fileName;
NSString *size=[NSString stringWithFormat:#"%d",[data length]];
/*
NSString *post = [NSString stringWithFormat:#"userid=%#&title=%#&docname=%#&size=%#&content=%#",userid,title,docname,size,content];
NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding];
[self.request setHTTPMethod:#"POST"];
//NSString *POSTBoundary = [NSString stringWithFormat:#"0xKhTmLbOuNdArY"];
//[self.request addValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#\r\n", POSTBoundary] forHTTPHeaderField:#"Content-Type"];
[self.request setValue:[NSString stringWithFormat:#"%d", postData.length] forHTTPHeaderField:#"Content-Length"];
[self.request setValue:#"application/x-www-form-urlencoded charset=utf-8" forHTTPHeaderField:#"Content-Type"];
[self.request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:self.request delegate:self];
*/
/////
NSMutableURLRequest *request11 = [[NSMutableURLRequest alloc] init] ;
[request11 setURL:url];
[request11 setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request11 addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
// Pdf File
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"content\"; filename=\"INSPECTION.pdf\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[#"Content-Disposition: form-data; name=\"content\"\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",userid] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",#"TITLE_TEST1"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"docname\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",docname] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"size\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",size] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[request11 setHTTPBody:body];
[NSURLConnection connectionWithRequest:request11 delegate:self];
//[NSURLConnection sendSynchronousRequest:request11 returningResponse:nil error:nil];
/////
}
I used following function to upload images, audio and videos with slight modifications.
In this function urloptions is the query string that you want to send with file like userId and fileName.
Didn't get chance to upload PDF but hope this will help you.
-(NSString *)uploadFile:(NSString *)urloptions: (NSString *) ext :(NSData *)imageData{
NSURL *url=[NSURL URLWithString:[NSString stringWithFormat:#"%#&%#",APP_URL,urloptions]];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[request addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"formFile\"; filename=\".%#\"\r\n",ext] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
//NSLog(#"%#",returnString);
return returnString ;
//[returnString release];
}
While you can find a way to correctly post your data, it will be much easier and maintainable to use a wrapper around NSURLConnection such as STHTTPRequest https://github.com/nst/STHTTPRequest. Here is what your code will look like:
STHTTPRequest *r = [STHTTPRequest requestWithURLString:#"http://abcgroup.delivery-projects.com:81/api/index.php?keyword=docinsert"];
[r setFileToUpload:#"Inspection.pdf" parameterName:#"myFile"];
[r setPOSTDictionary:#{#"userid":#"", #"title":#"", #"docName":#""}]; // your parameters here
r.completionBlock = ^(NSDictionary *headers, NSString *body) {
// ...
};
r.errorBlock = ^(NSError *error) {
// ...
};
[r startAsynchronous];
To upload any type of file you can use ASIHttpRequest library that you can get from https://github.com/pokeb/asi-http-request.
To upload file use ASIFormDataRequest class of this library that makes your work easy.
first convert your pdf in binary
NSData theData = [NSData dataWithData:[GTMBase64 decodeString:theBinary]]; //first transfer it to NSData.
[m_oTestingWeb loadData:theData
MIMEType:#"application/pdf"
textEncodingName:#"UTF-8"
baseURL:nil]; //using the web view to show it back
then upload this data to server.
You have to create an API on the web server that the iPhone can contact in order to POST data to your web server. You can simply create an NSURLConnection in order to build your packet to post the data from the iPhone app.
Inside an NSURLConnection you can tell it to be a POST packet and then add data to the body of the request. Your image data should be converted to UTF8 and stored as a nvarchar or something along those lines in your database.
Understand, this is an overview of what you have to do, without knowledge of your internal workings of the web app I cannot give you specifics.
You can use the latest Library by AFNewtork:
With AFNetworking you can do it by:
NSURL *url = [NSURL URLWithString:#"my_base_url"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:#"yourfile.pdf"], 0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:#"POST" path:#"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
[formData appendPartWithFileData:imageData name:#"MainMedia" fileName:#"MainMedia" mimeType:#"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(#"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation start];
}
Download AFNetworking library and add it to your project.
Two real issues that I can spot in "Tried 2"
It seems, you forgot the final delimiter - see Irfan DANISH post.
You need the binary of the pdf - not the UTF8.
Additionally, do not use a synchronous request - use the asynchronous style implementing the delegate methods.
And, you "should" set a Content-Length header for the pdf part.

how to share image to twitter from iphone application

I'm using twitter+OAuth with MGTwitterengine in my application and successfully post a comment to twitter. But now my problem is share image to twitter using
https://upload.twitter.com/1/statuses/update_with_media.json . I searched many web sites and get source code by using this direct upload to twitter api
https://upload.twitter.com/1/statuses/update_with_media.json . here the code is below.
-(void)UploadimageToTwitter
{
//NSString *boundary = #"----------------------------991990ee82f7";
// NSURL *finalURL = [NSURL URLWithString:#"http://upload.twitter.com/1.1/statuses/update_with_media.json"];
NSString *accessTokenKey = kOAuthaccessTokenKey;
NSString *secretTokenKey = kOAuthsecretTokenKey;
NSURL *finalURL = [NSURL URLWithString:#"http://upload.twitter.com/1/statuses/update_with_media.json"];
/*if (!finalURL)
{
return nil;
}*/
OAConsumer *consumer = [[OAConsumer alloc] initWithKey:kOAuthConsumerKey secret:kOAuthConsumerSecret];
//Shearing picture on twitter with Oauth without any third party api.
OAToken *token = [[OAToken alloc] initWithKey:accessTokenKey secret:secretTokenKey]; //Set user Oauth access token and secrate key
//OAConsumer *consumer = [[OAConsumer alloc] initWithKey:ConsumerToken secret:ConsumerSecrateKey]; // Application cosumer token and secrate key
OAMutableURLRequest *theRequest = [[OAMutableURLRequest alloc] initWithURL:finalURL consumer:consumer token:token realm: nil signatureProvider:nil];
[theRequest setHTTPMethod:#"POST"];
[theRequest setTimeoutInterval:120];
[theRequest setHTTPShouldHandleCookies:NO];
// Set headers for client information, for tracking purposes at Twitter.
[theRequest setValue:DEFAULT_CLIENT_NAME forHTTPHeaderField:#"X-Twitter-Client"];
[theRequest setValue:DEFAULT_CLIENT_VERSION forHTTPHeaderField:#"X-Twitter-Client-Version"];
[theRequest setValue:DEFAULT_CLIENT_URL forHTTPHeaderField:#"X-Twitter-Client-URL"];
NSString *boundary = #"--0246824681357ACXZabcxyz";// example taken and implemented.
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[theRequest setValue:contentType forHTTPHeaderField:#"content-type"];
// NSMutableData *body = [NSMutableData dataWithLength:0];
NSMutableData *body=[NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//status
[body appendData:[[NSString stringWithFormat:#"--%#\r\n\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition:form-data; name=\"status\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"status\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithFormat:#"%#",status] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"%#",#"Latest Uploading"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n"]dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//media
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition:form-data; name=\"media_data[]\"; filename=\"sunflower.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithString:#"Content-Disposition: form-data; name=\"media[]\"; filename=\"index.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Type:application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
UIImage *image1=[UIImage imageNamed:#"sunflower.jpg"];
NSData *imageData = UIImageJPEGRepresentation(image1, 1.0);
// [body appendData:[[NSString stringWithString:[UIImageJPEGRepresentation(image, 1.0) base64EncodingWithLineLength:0]] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:#"\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];
NSString *oAuthHeader = [theRequest valueForHTTPHeaderField:#"Authorization"];
[theRequest setHTTPBody:body];
NSHTTPURLResponse *response = nil;
NSError *error = nil;
NSData *responseData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(#"oAuthHeader = %# =",oAuthHeader);
NSLog(#"ResponsString = \n%#",responseString);
}
Using this code i'm getting responsestring is below
{"errors":[{"message":"Internal error","code":131}]}
It is a server error or my error. really do not know where i was wrong in the above code.
if anybody know answer please guide to me. your help saved my life cause i was searching the above task past three months using three kind of different api's like Twitpic, yfrog and now post/update_with_media.
NSString *status = tweetText.text;
TWRequest *sendTweet;
if(image1 == nil)
{
sendTweet = [[TWRequest alloc]
initWithURL:[NSURL URLWithString:#"https://api.twitter.com/1/statuses/update.json"]
parameters:[NSDictionary dictionaryWithObjectsAndKeys:status, #"status", nil]
requestMethod:TWRequestMethodPOST];
}
else
{
sendTweet = [[TWRequest alloc] initWithURL:[NSURL URLWithString:#"https://upload.twitter.com/1/statuses/update_with_media.json"]
parameters:[NSDictionary dictionaryWithObjectsAndKeys:status, #"status",image1,#"media" , nil]
requestMethod:TWRequestMethodPOST];
[sendTweet addMultiPartData:[tweetText.text dataUsingEncoding:NSUTF8StringEncoding] withName:#"status" type:#"multipart/form-data"];
[sendTweet addMultiPartData:UIImagePNGRepresentation(image1) withName:#"media" type:#"multipart/png"];
}
sendTweet.account = self.account;
[sendTweet performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error) {
if ([urlResponse statusCode] == 200)
{
dispatch_sync(dispatch_get_main_queue(),
^{
[self displayText:#"Tweet Done"];
});
}
else
{
NSLog(#"Problem sending tweet: %#", error);
}
}];
Hope this code works for you, it works for me, in this you can post image as well as text only. Best of luck

Access path of audio files stored in native ipod application of iPhone

I have built an iPhone application whereby I want to upload video, images and audio stored in user's iPhone to a server. I have successfully done this for video and images stored in photo library. The code for this is as follows:
imageData = UIImageJPEGRepresentation(image.image, 90);
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
if (image_selected) {
stringForImagePathToUpload = [[NSString alloc]initWithFormat:#"..."];
[request setURL:[NSURL URLWithString:stringForImagePathToUpload]];
}else if (video_selected){
stringForVideoPathToUpload = [[NSString alloc]initWithFormat:#"..."];
[request setURL:[NSURL URLWithString:stringForVideoPathToUpload]];
}
[request setHTTPMethod:#"POST"];
NSString *boundary = [NSString stringWithString:#"*****"];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
if (image_selected) {
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"uploadedfile\";filename=\"%#"".jpg""\r\n\r\n", imageTitle.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
}else if (video_selected){
NSFileHandle *file = [NSFileHandle fileHandleForReadingFromURL:videoURL error:nil];
NSData *data = [file readDataToEndOfFile];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"uploadedfile\";filename=\"%#"".m4v""\r\n\r\n", imageTitle.text] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:data]];
}
[body appendData:[[NSString stringWithFormat:#"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
/* setting the body of the post to the reqeust */
[request setHTTPBody:body];
/* setting up the connection to the web*/
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding]autorelease];
NSLog(#"Content uploaded %#", returnString);
Now, I found ot the audio files get stored in the ipod application of the iPhone. I want to know if I can access the path to this audio like i did for images and videos from photo library. If it is possible to access the audio files, please help me to know how to access them.