Easwaramoorthy Kanagaraj
Easwaramoorthy Kanagaraj

Reputation: 4213

How to store NSData from NSURLConnection sendSynchronousRequest as a pdf file to documents directory in iOS?

I am getting the NSData using

NSData * responseData = [NSURLConnection sendSynchronousRequest:mutableUrlRequest returningResponse:nil error:nil];

How to store this as a PDF to local documents directory? My service is in java which returns byte array.

Thanks !

Upvotes: 0

Views: 311

Answers (2)

CouchDeveloper
CouchDeveloper

Reputation: 19164

You really need to confirm that your request has been successful:

  • Provide a pointer for the response which on return contains the status code and the MIME type of the response data, possibly also content length and other useful info. Check against what you expect.

  • Provide a NSError pointer, too in order to get the error when the connection fails (returns nil).

That's what you should always do when you make a toy app. When you make a more serious app, you should use the asynchronous style to perform a network request. Basically, you implement the two NSURLConnection Delegate protocols. There is lot of info already in SO how to accomplish this, and as well in Apple samples. If you have any specific questions, please ask again :)

How to store this as a PDF to local documents directory?

This question has been answered already.

Upvotes: 0

NSLog
NSLog

Reputation: 649

try like this

NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
[data writeToFile:[docPath stringByAppendingPathComponent:@"name.pdf"] 
       atomically:YES];

Upvotes: 1

Related Questions