0

In my work, I just want to read a downloaded PDF in iphone device

At first, I tried to use the "bundleRoot" directory, it is work in simulator but not on device. Now, I just test in simulator for Directory of Document or Cache.

How can I get a writable path on the iPhone?

After I read the above topic,

I have tried using Directory of Document or Cache.

Here is my code in finding the writable path

/******************************************************************************/

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;

if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
    [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}


NSString *path_to_file = [cachePath stringByAppendingPathComponent:@"testing6.pdf"];

/******************************************************************************/

I can find the downloaded file and get the size in this directory,but I cannot read the file by using the following code :

            pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

    page = CGPDFDocumentGetPage(pdf, 1);
    CGPDFPageRetain(page);

    CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);

The return pageRect.size.width is 0

After I print out the download and retrieve file path also are :

/Users/ITDEV/Library/Application Support/iPhone Simulator/4.3.2/Applications/806ED359-70F8-4C07-996F-6AFC959B3FC7/Library/Caches/testing6.pdf

Anyone can help me pls?

Community
  • 1
  • 1
user972268
  • 113
  • 1
  • 4

2 Answers2

0

You can read the file in UIWebView.

And By using below code you can open the file.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
BOOL isDir = NO;
NSError *error;

if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
    [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}


NSString *path_to_file = [cachePath stringByAppendingPathComponent:@"testing6.pdf"];

NSURL *targetURL = [NSURL fileURLWithPath:path_to_file];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[YourWebViewName loadRequest:request];

Don't Forget to connect the IBOutlet of your webview with nib file's UIWebview.

SJS
  • 2,647
  • 1
  • 17
  • 34
  • thanks for reply. I have used this way to view PDF in my apps before but we think that is not good enough. We want to store a small size PDF into apps before viewing. – user972268 Sep 30 '11 at 09:09
  • Do You have the url of that pdf file from where you want to store in your application path. – SJS Sep 30 '11 at 10:07
  • YES! I can download the PDF to Iphone document directory from url perfectly! But I cannot open it programmatically, I can open the pdf manually if I go to the folder directly. – user972268 Oct 01 '11 at 01:52
0

If all other answers won't work then try using NSBundle as below:

NSString *path = [[NSBundle mainBundle] pathForResource:@"myfilename" ofType:@"pdf"];
NSLog("path : %@",path);

Same problem i faced and i found that my plist file was not in Documents directory but instead it shows me path to my app and then it shows MYAppName.app/myPlist.plist file. This means that the app file includes my plist file in its own bundle i.e. MainBundle.

DShah
  • 9,768
  • 11
  • 71
  • 127
  • thanks for reply. I have tried this way(Use Bundle) before too. It is work in simulator perfectly but not on device. Someone said the Bundle are not writable in device. is that true? – user972268 Sep 30 '11 at 09:10
  • I believe that when files are in bundle they will not be writable. But I am not sure. – DShah Sep 30 '11 at 12:01
  • So I cannot save the pdf file into device's bundle T_T – user972268 Oct 01 '11 at 01:54
  • No you cannot save in MainBundle. But you can save in Document's Directory which is the actual place where resources should be placed. – DShah Oct 01 '11 at 02:54
  • I am using the Document Directory, but I cannot retrieve the downloaded file programmatically, even if load it into a webview, I am fail to do this. Is there any permission setting in this folder? – user972268 Oct 01 '11 at 03:35
  • I think I am. I use the same path to save and load the file. dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); docsDir = [dirPaths objectAtIndex:0] NSString *path_to_file = [docsDir stringByAppendingPathComponent:@"testing6.pdf"]; I get the path by above code I can save the file correctly. but is it right path to retrieve the file too? – user972268 Oct 01 '11 at 06:57
  • https://github.com/akisute/iPhonePDF here is the simple create and save, and read example of PDF. Please go through this example on GitHub. – DShah Oct 01 '11 at 10:07
  • the retrieving file path format must like as follow : NSURL *path_to_file = [NSURL fileURLWithPath:path2]; which can be different from "writetofile" path. It wasted me 2 days time T_T – user972268 Oct 03 '11 at 07:08