Printing to Google Cloud Print using iOS - objective-c

I have been trying to print a PDF file to Google cloud print using the following code. Instead of actually printing the document, it is only printing a string that shows the location of the file as well as the file name. I would like to have it print the actual document.
We can assume that the data being passed into this method is valid as it seems to be finding the correct printer from calls to cloud prints /search
Update: The code is updated from its original format to include some multi-part form data
NSString *currentPDFFileName = [[NSBundle mainBundle] pathForResource:#"TestPDF" ofType:#"pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:currentPDFFileName];
NSData *validPDF = [[NSString stringWithString:#"%PDF"] dataUsingEncoding: NSASCIIStringEncoding];
if (!(pdfData && [[pdfData subdataWithRange:NSMakeRange(0, 4)] isEqualToData:validPDF]))
{
NSLog (#"Not valid");
}
NSLog (#"%#", currentPDFFileName);
NSLog (#"%#", [currentPDFFileName lastPathComponent]);
//create the body
NSMutableData *body = [NSMutableData data];
//create the POST vars
NSString *thePrinter = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"printerid\"\r\n\r\n%#", printerID];
NSString *pdf = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"content\"; filename=\"%#\"\r\n", [currentPDFFileName lastPathComponent]];
NSString *theContent = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"contentType\"\r\n\r\n%#", contentType];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[thePrinter dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[pdf dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[theContent dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/pdf\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:pdfData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
NSMutableURLRequest *pdfRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"https://www.google.com/cloudprint/submit"]];
[pdfRequest setValue:#"multipart/form-data; boundary=----foo" forHTTPHeaderField:#"Content-type"];
[pdfRequest setHTTPMethod:#"POST"];
[pdfRequest setHTTPBody:body];
[self.authenticate authorizeRequest:pdfRequest completionHandler:^(NSError *error) {
NSString *output = nil;
if(error) {
output = [error description];
} else {
NSURLResponse *response = nil;
NSData *data = [NSURLConnection sendSynchronousRequest:pdfRequest
returningResponse:&response
error:&error];
if(data) {
output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
} else {
output = [error description];
}
}
NSLog(#"%#", output);
}];

Related

How can i upload text and image data in a single request to server Objective c

I am using this dictionary but not able to upload.
dictionary = #{
kRequest: sign_in_owner,
o_first_name: s_fname,
o_middle_name: s_mname,
o_last_name: s_lname,
o_email_id: s_email,
o_primary_no: s_pnum,
o_alternative_no: s_anum,
o_project: s_proj,
o_block: s_block,
o_flat: s_flat,
o_total_sq_ft: s_square,
o_flat_intercom_no: s_intercom,
o_maintenance: s_maintenance};
Here is my rest of the code:
NSMutableArray* s_photo=[dictAttributes objectForKey:kPhoto];
isPhoto=NO;
// the boundary string : a random string, that will not repeat in post data, to separate post data fields.
NSString *BoundaryConstant = #"----------V2ymHFg03ehbqgZCaKO6jy";
// string constant for the post parameter 'file'. My server uses this name: `file`. Your's may differ
NSString* FileParamConstant = #"photo[]";
// the server url to which the image (or the media) is uploaded. Use your server url here
NSURL* requestURL = [NSURL URLWithString:#"http://-----"];
// create request
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setHTTPMethod:#"POST"];
// set Content-Type in HTTP header
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", BoundaryConstant];
[request setValue:contentType forHTTPHeaderField: #"Content-Type"];
// post body
NSMutableData *body = [NSMutableData data];
// add params (all params are strings)
for (NSString *param in dictionary) {
[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", param] dataUsingEncoding:NSUTF8StringEncoding]];
}
// add image data
if (s_photo) {
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", BoundaryConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"; filename=\"image.jpg\"\r\n", FileParamConstant] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[s_photo objectAtIndex:0]];
[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:#"%lu", (unsigned long)[body length]];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
// set URL
[request setURL:requestURL];
You can use AFNetworking Framework
It provides methods to upload text and image at a time (in one service call).
NSData *imageData = UIImageJPEGRepresentation(imgAttachment, 0.5);
NSMutableURLRequest *request = [[AFJSONRequestSerializer serializer] multipartFormRequestWithMethod:#"POST" URLString:#"http://yourURL" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (imageData != nil) {
[formData appendPartWithFileData:imageData name:#"param_name" fileName:#"AnyNameForImage.jpg" mimeType:#"image/jpeg"];
}
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress) {
// This is not called back on the main queue.
// You are responsible for dispatching to the main queue for UI updates
} completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(#"ERROR : %#", error.description);
} else {
NSLog(#"Response Headers : %#", response);
NSLog(#"Response Data : %#", responseObject);
}
}];
[uploadTask resume];

Uploading array of images(wrapped in NSData) to the server in iOS

Good day,
I'm trying to upload an array of images to the server, images are wrapped into NSData, but the server returning only one image, this is my code:
- (void)actionSave{
NSArray *imagesArray = [NSArray arrayWithObjects:[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageOne.image)],
[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageTwo.image)],
[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageThree.image)],
[NSData dataWithData:UIImagePNGRepresentation(self.imageViewForImageFour.image)], nil];
NSData *images = [NSKeyedArchiver archivedDataWithRootObject:imagesArray];
NSMutableURLRequest *requestImg = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#&mid=%#", SEND_POST_TO_WALL, [self.currentFriend objectForKey:#"uid"]]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0f];
[requestImg setHTTPMethod:#"POST"];
NSString *boundary = #"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", boundary];
[requestImg addValue:contentType forHTTPHeaderField:#"Content-Type"];
NSMutableData *postData = [NSMutableData dataWithCapacity:[images length] + 512];
[postData setData:images];
NSMutableData *body = [NSMutableData data];
//Photo
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:postData];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"--%#--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//Text
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"text\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[self.textView.text dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[requestImg setHTTPBody:body];
NSURLConnection *connectionForImage = [NSURLConnection connectionWithRequest:requestImg delegate:self];
if (connectionForImage) {
self.infoData = [NSMutableData data];
} else {
NSLog(#"Connection failed");
}
}
This is the result that server returns:
array(3) {
["act"]=>
string(12) "comments.add"
["module"]=>
string(7) "account"
["mid"]=>
string(4) "7728"
}
POST:
array(1) {
["text"]=>
string(5) "Gjdgy"
}
FILES:
array(1) {
["image"]=>
array(5) {
["name"]=>
string(9) "image.png"
["type"]=>
string(10) "image/jpeg"
["tmp_name"]=>
string(14) "/tmp/php9oudKt"
["error"]=>
int(0)
["size"]=>
int(463652)
}
}
What I'm doing wrong???
Thanks in advance!
You showed us the POST of the data but not the GET of that data again, so not sure what you mean by "only returns one image". What its returning should be the exact same data blob you went up, which is the archive. You can test this by logging the size of what you send up with the size of what you get back - they should both be the same if the server is returning your original blob back (if its not, that's a web issue not an ios issue).
With the blob you get back from the web, you then need to NSKeyUnarchive it to get your original array of data items. The unraveling is the reverse of the raveling.

How to upload multiple images without using ASIHTTPRequest

I want to upload two images to the server with following code can any one please help me, here is my code for post data
- (void)uploadPath:(NSString*)path withOptions:(NSDictionary*)options withImageData1:(NSData*)data1 ofImageName1:(NSString *)imageName1 andImageData2:(NSData*)data2 ofImageName2:(NSString *)imageName2 {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSURL *url = [NSURL URLWithString:path relativeToURL:[NSURL URLWithString:baseURLString]];
NSMutableURLRequest *mutableRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:90.0];
NSString *boundary = #"-------1234567";
[mutableRequest setValue:[NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary] forHTTPHeaderField:#"Content-Type"];
[mutableRequest setHTTPMethod:#"POST"];
NSMutableData *postbody = [NSMutableData data];
for(NSString *key in options)
{
NSString *value = [options objectForKey:key];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"%#\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[value dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//******************* Append 1st Image ************************/
if ([imageName1 isEqualToString:#"picture1.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"picture1\"; filename=\"%#\"\r\n",imageName1] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data1]];
[postbody appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
//******************* Append 1st Image ************************/
//******************* Append 2nd Image ************************/
if ([imageName2 isEqualToString:#"picture2.jpg"]) {
[postbody appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"picture2\"; filename=\"%#\"\r\n",imageName2] dataUsingEncoding:NSUTF8StringEncoding]];
}
[postbody appendData:[[NSString stringWithString:#"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data2]];
//******************* Append 2nd Image ************************/
[mutableRequest setHTTPBody:postbody];
urlConnection = [[NSURLConnection alloc] initWithRequest:mutableRequest delegate:self];
if (self.urlConnection) {
self.dataXml = [NSMutableData data];
}
self.urlConnection = nil;
[mutableRequest release];
[pool release];
}
One solution to see what is wrong:
make a new project, using ASIHttpRequest
Get a web debugging proxy like Charles
Turn on Charles (or whatever), do a multi-file upload that succeeds.
Record data from HTTP traffic to server.
Now run your own code. Record that traffic. What is different from
your traffic to the working traffic? Makes yours IDENTICAL in all
respects, and how can it not work?
You can also replace step (1) with a curl command line that uploads multiple files to a server and record that traffic, but I'm not versed enough in curl to say off the top of my head what that command would look like.
- (NSString*) nameValString: (NSDictionary*) dict {//example CHLOVA_FORM_BOUNDARY is #"aaa"
NSArray* keys = [dict allKeys];
NSString* result = [NSString string];
int i;
for (i = 0; i < [keys count]; i++) {
result = [result stringByAppendingString:
[#"--" stringByAppendingString:
[CHLOVA_FORM_BOUNDARY stringByAppendingString:
[#"\r\nContent-Disposition: form-data; name=\"" stringByAppendingString:
[[keys objectAtIndex: i] stringByAppendingString:
[#"\"\r\n\r\n" stringByAppendingString:
[[dict valueForKey: [keys objectAtIndex: i]] stringByAppendingString: #"\r\n"]]]]]]];
}
return result;
}
- (void)sendPhoto{
NSString *param = [self nameValString:dic];//dic is post info like {lat=36;lng=120;}
NSString *footer = [NSString stringWithFormat:#"\r\n--%#--\r\n", CHLOVA_FORM_BOUNDARY];
param = [param stringByAppendingString:[NSString stringWithFormat:#"--%#\r\n", CHLOVA_FORM_BOUNDARY]];
param = [param stringByAppendingString:#"Content-Disposition: form-data; name=\"pic\";filename=\"image.jpg\"\r\nContent-Type: image/jpeg\r\n\r\n"];
NSData *jpeg = UIImageJPEGRepresentation([UIImage imageWithContentsOfFile:_path], 0.55);
//NSLog(#"jpeg size: %d", [jpeg length]);
NSMutableData *data = [NSMutableData data];
//img one
[data appendData:[param dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg];
[data appendData:[footer dataUsingEncoding:NSUTF8StringEncoding]];
//img two
[data appendData:[param2 dataUsingEncoding:NSUTF8StringEncoding]];
[data appendData:jpeg2];
[data appendData:[footer2 dataUsingEncoding:NSUTF8StringEncoding]];
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#", CHLOVA_FORM_BOUNDARY];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url];
[request setHTTPShouldHandleCookies:NO];
[request setHTTPMethod:#"POST"];
[request setValue:contentType forHTTPHeaderField:#"Content-Type"];
[request setValue:[NSString stringWithFormat:#"%d", [data length]] forHTTPHeaderField:#"Content-Length"];
[request setHTTPBody:data];
//then do sending
}

UIIMagePickerController & NSURLConnection

UIImagePickerController freezes while I think the Image is being compressed or being sent by NSURLConnection although I'm not convinced it's the later.
What I want is, all this to be done in the background. Instead when I pick a photo from the library, the Screen freezes for what feels like forever. This isn't optimal, obviously. What approach should I take here?
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
{
int i = 0;
NSString *uniquePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/selectedImage.png"];
while ([[NSFileManager defaultManager] fileExistsAtPath:uniquePath])
{
uniquePath = [NSString stringWithFormat:#"%#/%#-%d.%#", [NSHomeDirectory() stringByAppendingPathComponent:#"Documents"], #"selectedImage", ++i, #"png"];
}
NSLog(#"Writing selected Image to Documents Folder, %#", uniquePath);
dataForPNGFile = UIImagePNGRepresentation(img);
if (!dataForPNGFile) return NO;
[UIImagePNGRepresentation(img) writeToFile:uniquePath atomically:YES];
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
[[self parentViewController] dismissModalViewControllerAnimated:YES];
[picker release];
NSString *urlString = #"http://localhost:3000/photos";
NSURL *url = [NSURL URLWithString:urlString];
NSString *boundary = #"----1010101010";
NSString *contentType = [NSString stringWithFormat:#"multipart/form-data; boundary=%#",boundary];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:#"POST"];
[request addValue:contentType forHTTPHeaderField: #"Content-Type"];
NSString *photoPath = [[NSBundle mainBundle] pathForResource:#" my-photo " ofType:#"jpg"];
NSData *photoData = [NSData dataWithContentsOfFile:photoPath];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"photo-description\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"testing 123" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithFormat:#"Content-Disposition: form-data; name=\"photo-file\"; filename=\"%#\"\r\n", " my-photo.jpg"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:photoData];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Disposition: form-data; name=\"tags\"\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"random,test,example" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request addValue:[NSString stringWithFormat:#"%d", body.length] forHTTPHeaderField: #"Content-Length"];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
You should use:
detachNewThreadSelector:toTarget:withObject:
Detaches a new thread and uses the specified selector as the thread entry point.
+ (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
Documentation here: NSThread
This will create a new thread for the operation that is eating up the cpu, so your UI isn't affected remember to create a NSAutoreleasePool for the new thread, as only the main thread gets a NSAutoreleasePool pool by default.
So at the beginning of your selector do:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//and at the end:
[pool drain]:

Failing to POST NSData of PDF to server

I have an iPad application in which I need to send a server a PDF file, alongside two POST variables, a user ID and a job ID. I am using the code below to do this:
NSData *pdfData = [NSData dataWithContentsOfFile:currentPDFFileName];
NSData *validPDF = [[NSString stringWithString:#"%PDF"] dataUsingEncoding: NSASCIIStringEncoding];
if (!(pdfData && [[pdfData subdataWithRange:NSMakeRange(0, 4)] isEqualToData:validPDF]))
{
NSLog (#"Not valid");
}
NSLog (#"%#", currentPDFFileName);
NSLog (#"%#", [currentPDFFileName lastPathComponent]);
//create the body
NSMutableData *body = [NSMutableData data];
//create the POST vars
NSString *jobID = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"jobid\"\r\n\r\n%#", job.jobID];
NSString *userID = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"userid\"\r\n\r\n%#", delegate.userID];
NSString *pdf = [NSString stringWithFormat:#"Content-Disposition: form-data; name=\"image_file\"; filename=\"%#\"\r\n", [currentPDFFileName lastPathComponent]];
[body appendData:[[NSString stringWithFormat:#"--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[jobID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[userID dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:#"\r\n--%#\r\n", #"----foo"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[pdf dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[#"Content-Type: application/pdf\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:pdfData];
NSMutableURLRequest *pdfRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:#"http://example.com"]];
[pdfRequest setValue:#"multipart/form-data; boundary=----foo" forHTTPHeaderField:#"Content-type"];
[pdfRequest setHTTPMethod:#"POST"];
[pdfRequest setHTTPBody:body];
NSData *pdfDataReply;
NSURLResponse *pdfResponse;
NSError *pdfError;
pdfDataReply = [NSURLConnection sendSynchronousRequest:pdfRequest returningResponse:&pdfResponse error:&pdfError];
NSString *pdfResult = [[NSString alloc] initWithData:pdfDataReply encoding:NSASCIIStringEncoding];
NSLog (#"%#", pdfResult);
The people on the other end of the connection are saying that they are not receiving the PDF file. I have tried all the usual suspects such as trying to find any nil objects.
Would greatly appreciate if anyone could please lend a hand with this!
Thanks.
Ricky.
Turns out there was an issue with the boundaries. I needed to add one at the end, after pdfData was added. Thank you for your help.