user973067
user973067

Reputation: 337

Objective-c: Downloading a PDF file

I am trying to eneble a button to download a pdf file from a server. (Onto the divice) I'm not being successful with this code (to post this question, i've changed the address) Would you give me some advice to make it work..? Thanks in advance.

NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL     URLWithString:@"http://www.mydomain.com/mypdffile.pdf"]];

//Store the Data locally as PDF File
NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"<Application_Home>/Documents/"]];

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:filePath atomically:YES];

//Now create Request for the file that was saved in your documents folder
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
[webView loadRequest:requestObj];

Upvotes: 1

Views: 5360

Answers (1)

Gabriel
Gabriel

Reputation: 3359

You are not building a correct NSURL object to reach a server. fileURLWithPath: is a method to build URL that points to files in the file system.

Use URLWithString: writing the complete url, that is for example "http://myserver.com/myfile.pdf"

Upvotes: 1

Related Questions