1

これは私がやろうとしていることです:

  1. 外部 URL から .pdf を取得する
  2. ローカル ディスクに保存する
  3. WebView で表示する
  4. ユーザーが .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 が正しく読み込まれているため、これは明らかです。

4

2 に答える 2

0

解決しました。.h ファイルに UIDocumentenInteractionController デリゲートを実装していませんでした。今、私は持っていて、すべてがうまくいきます。役立つヒントを提供してくれた @trojanfoe に感謝します。

于 2013-02-20T10:37:06.107 に答える
0

CGRectZeroドキュメント コントローラーを表示するときに、self.view.bounds に変更します。

于 2013-02-08T16:01:53.980 に答える