2016-09-01 10 views
-1

私のプロジェクトにはPDFがあります。私はそのファイルをモバイルディレクトリにダウンロードして他のアプリにロードする機能を提供したいと思います。Appディレクトリからローカルにファイルをダウンロードして読み込む方法

+0

正しく取得できません。あなたのプロジェクトのpdfは何を意味していますか?あなたのアプリのバンドルで?それがあなたのアプリのバンドルにあれば、なぜそれをダウンロードしたいのですか?? – Lion

+0

すべてのPDFを私のアプリケーションに添付しました。ユーザーには表示されません。だから、そのPDFをユーザのローカルフォルダにコピーしたいので、どのアプリでも簡単に使うことができます。 –

+0

iPhoneにはローカルフォルダがありません。どこにPDFをコピーしますか?あなたはUIActivityViewControllerを介して共有を調べることができます。 –

答えて

1
NSError *error; 
NSFileManager *fileManager = [NSFileManager defaultManager]; 

//NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:@"/RTOFiles"];; 
if (![[NSFileManager defaultManager] fileExistsAtPath:documentsDir]) 
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDir withIntermediateDirectories:NO attributes:nil error:&error]; 

NSString *pdfName = @"form_1.pdf"; 

NSString *docPdfFilePath = [documentsDir stringByAppendingPathComponent: pdfName]; 

//Using NSFileManager we can perform many file system operations. 
BOOL success = [fileManager fileExistsAtPath: docPdfFilePath]; 

if (!success) { 
    NSString *samplePdfFile = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent: pdfName]; 

    success = [fileManager copyItemAtPath: samplePdfFile toPath: docPdfFilePath error: &error]; 

    if (!success) 
     //    NSAssert1(0, @"Failed to copy file ‘%@’.", [error localizedDescription]); 
     NSLog(@"Failed to copy %@ file, error %@", pdfName, [error localizedDescription]); 
    else { 
     NSLog(@"%@ File copied to %@", pdfName,documentsDir); 
    } 
} 
else{ 
    NSLog(@"File already Exists !"); 
} 
+0

これは、docsディレクトリ内のアプリケーションバンドルにもあるため、ダブルメモリを使用します。あなたは、アプリケーションバンドルから直接ファイルを使用することができます。私はそれを参照して回答を書いています。 – Lion

1

それはのようなアプリのbunldleである場合は、直接自分のイメージを使用することができ、

UIImage *img = [UIImage imageNamed:@"Amy.png"]; 

あなたは、あなたのような何かを行うことができ、アプリケーションバンドルからwebviewpdfをロードしたい場合は、

NSString *path = [[NSBundle mainBundle] pathForResource:@"yourPdfName" ofType:@"pdf"]; 
NSURL *url = [NSURL fileURLWithPath:path]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
[webView loadRequest:request]; 

つまり、アプリケーションバンドルでファイルを利用できる場合は、ダブルメモリを使用するため、documentDirectoryにコピーする必要はありません。バンドルから使用しました。

+0

ユーザーがアプリケーションからファイルを開こうとしている場合、ドキュメントディレクトリで開くこともできますので、これを使用しました。ご協力ありがとうございます ! –

+0

ユーザーはアプリバンドルから直接開くことができます。アプリバンドルにある場合は、documentdirectoryにコピーする必要はありません。 – Lion

関連する問題