简体   繁体   中英

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];

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM