これは私がやろうとしていることです:
- 外部 URL から .pdf を取得する
- ローカル ディスクに保存する
- WebView で表示する
- ユーザーが .pdf を、.pdf を読み取ることができる別のアプリに移動できるようにする
1から3まですべてうまくいきます。ただし、他のアプリに移動/共有されるものはありません。私が間違っていることを理解できません。これが私がやっていることです。
Documents フォルダー (viewDidLoad) に PDF を保存する方法:
// to save the pdf into local file system (tempString is the pdf url)
NSData *pdfData = [[NSData alloc]
initWithContentsOfURL:[NSURL URLWithString:tempString]];
NSString *resourceToPath = [[NSString alloc]
initWithString:[[[[NSBundle mainBundle] resourcePath]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:@"Documents"]];
NSString *filePAth = [resourceToPath stringByAppendingPathComponent:@"myPDF.pdf"];
[pdfData writeToFile:filePAth atomically:YES];
// to populate the WebView
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url2];
[my_web_view setUserInteractionEnabled:YES];
//[editoriale_view setDelegate:self];
[my_web_view loadRequest:requestObj];
私の viewDidLoad() 関数では、ユーザーが .pdf ファイルを読み取ることができるアプリのリストを開くことができるボタンを作成します。
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self
action:@selector(show_Button)];
そして、ここに私の show_Button 関数があります:
-(void)show_Button {
NSString *resourceToPath = [[NSString alloc]
initWithString:[[[[NSBundle mainBundle] resourcePath]
stringByDeletingLastPathComponent]
stringByAppendingPathComponent:@"Documents"]];
NSString *filePAth = [resourceToPath
stringByAppendingPathComponent:@"myPDF.pdf"];
NSLog(@"filePath = %@", filePAth);
NSURL *url2 = [NSURL fileURLWithPath:filePAth];
NSLog(@"url2 = %@", url2);
UIDocumentInteractionController *docContr = [UIDocumentInteractionController
interactionControllerWithURL:url2];
[docContr presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
}
デバイスでこれを試してみると、リスト内のアイコンの 1 つ (iBooks のアイコン) をタップするまで、すべて正常に動作します。次に、アプリが閉じます (クラッシュするのではなく、単に閉じます)。
show_Button 関数に入れた 2 つのログのコンソール出力は次のとおりです。
1. filePath = /Users/[MY_USER]/Library/Application Support/iPhone
Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf
2. url2 = file://localhost/Users/[MY_USER]/Library/Application%20Support/
iPhone%20Simulator/6.1/Applications/[MY_EXAD_APP_ID]/Documents/myPDF.pdf
誰かが私が間違っていることを理解させようとしていますか? Xcode 4.6 を使用しています。サードパーティのソフトウェアを使用して iPhone アプリのファイル システムを参照したところ、ファイル「MyPDF.pdf」は実際には Documents フォルダーにありました。WebView が正しく読み込まれているため、これは明らかです。