I am having an app in which I am opening a PDF file from a url.
I am successfully able to open it in a webView.
Now I want to download that PDF file and save it in my documents folder and want to send that PDF file in my mail.
I searched a lot and found the best solution below.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"myfile.pdf"];
if(![[NSFileManager defaultManager] fileExistsAtPath:filePath]){
NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"]];
//Store the downloaded file in documents directory as a NSData format
[pdfData writeToFile:filePath atomically:YES];
}
NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView setUserInteractionEnabled:YES];
[webView setDelegate:self];
webView.scalesPageToFit = YES;
[webView loadRequest:requestObj];
But when i try this code, it gives me error saying failed to find PDF header: `%PDF' not found
I am even not able to open my PDF also.
I know same questions are asked before but I am not able to solve this error right now.
I don't know what I am doing wrong over here.
Sorry for the inconvenience.
Please help me with this.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…