1

さて、私はここ数週間検索とテストを行ってきました。このxcodeは初めてですが、アプリケーションを起動して実行するのに十分な知識があります。ですから、私が抱えている問題は、このオープンイン機能を追加することです。ローカルのPDF(手動でローカルにアプリにロードしたPDF)で動作させることはできますが、現在アプリで表示しているPDFで動作させることができないようです。PDFを表示したとしましょう。そのうちの1つはアプリにロードされておらず、URLが何であるかさえわかりません。ランダムなもので、オープンアクションボタンを取得する方法がわからないようです。仕事。これが私のコードです。ああ、ところで、私はAdobe Readerにオープンインしようとしていますが、ローカルpdfで正常に実行できます。アプリがインストールされていて、すべてが揃っています。助けてください!

1.)注:これによりPDFがローカルに読み込まれます。これは機能します。

- (IBAction)shareButton:(id)sender;{

    NSString * currentURL = _webview.request.URL.absoluteString;
    NSString * filename = [currentURL stringByDeletingPathExtension];
    NSLog(filename);
    NSString * filePath = [[NSBundle mainBundle]pathForResource:@"OOP_ObjC" ofType:@"pdf"];
    NSLog(filePath);
    docController =[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:currentURL]];
    docController.delegate = self;
    docController.UTI = @"com.adobe.pdf";
    [docController presentOpenInMenuFromBarButtonItem:_shareButton animated:YES];

    [super viewDidLoad] 
}

2.)注:これは、PDFをダウンロードしてローカルにロードするために使用しようとしているものです。しかし、私は-[NSURL initFileURLWithPath:];nil文字列パラメータを取得し続けます '

- (IBAction)shareButton:(id)sender;{

     NSString * currentURL = _webview.request.URL.absoluteString;
     NSString * filename = [currentURL stringByDeletingPathExtension];
     NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:filename]];


     //Store the Data locally as PDF File

     NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
     NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"mypdf.pdf"];
    [pdfData writeToFile:filePath atomically:YES];

    //Now create Request for the file that was saved in your documents folder

    NSURL *url = [NSURL fileURLWithPath:filePath];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView setUserInteractionEnabled:YES];
    [webView setDelegate:self];
    [webView loadRequest:requestObj];

    NSLog(filePath);
    NSString * path =
    [[NSBundle mainBundle]pathForResource:filePath ofType:@"pdf"];
    docController =[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];
    docController.delegate = self;
    docController.UTI = @"com.adobe.pdf";
    [docController presentOpenInMenuFromBarButtonItem:_shareButton animated:YES];

    [super viewDidLoad] 
}

4

2 に答える 2

2
 NSString * path =
    [[NSBundle mainBundle]pathForResource:filePath ofType:@"pdf"];

2番目の方法(webView one)では、アプリのバンドル内のファイルを検索しています。ただし、アプリのバンドルではなく、アプリのドキュメントフォルダーにファイルを保存しています。その行を削除します

これを変更します

docController =[UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:path]];

docController =[UIDocumentInteractionController interactionControllerWithURL: url]; // url is of the file that you saved by downloading.
于 2013-03-05T15:52:59.457 に答える
1

もちろん、シャギーの助けを借りて答えを見つけました。これを親指で上げて、これを機能させる方法を誰もが理解できるようにしてください(文字通り何ヶ月もかかったので!)すべてのコードを追加します。

#import <UIKit/UIKit.h>

@interface NinthViewController : UIViewController  <UIDocumentInteractionControllerDelegate> {
    IBOutlet UIWebView *webView;
    UIDocumentInteractionController *docController;
}
- (IBAction)shareButton:(id)sender;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *activityind;
@property (weak, nonatomic) NSTimer *timer;
@property (nonatomic, retain) UIDocumentInteractionController *docController;
@end

#import "NinthViewController.h"

@interface NinthViewController ()
@end
@implementation NinthViewController
@synthesize docController;


-(void)webView:(UIWebView *)X didFailLoadWithError:(NSError *)error {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Can't connect. Please check your internet connection and try again." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Try again", nil];
    [alert show];

}

//Implement the 'Try again' button action
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 1) {
        NSURL *url = [NSURL URLWithString:@"YOUR MAIN  URL SITE"];
        NSURLRequest *req = [NSURLRequest requestWithURL:url];
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
        [webView loadRequest:req];
    }
}

- (void)viewDidLoad
{
    NSString *website = @"YOUR MAIN URL SITE";
    NSURL *url = [NSURL URLWithString:website];
    NSURLRequest *requestUrl = [NSURLRequest requestWithURL:url];
    (webView.scalesPageToFit = YES);
    [webView loadRequest:requestUrl];
    [webView addSubview:_activityind];
    _timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(loading) userInfo:nil repeats:YES];
}

- (IBAction)shareButton:(id)sender
{
    NSString * currentURL = webView.request.URL.absoluteString;
    NSData *pdfData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:currentURL]];
    NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Documents"]];
    NSString* theFileName = [[currentURL lastPathComponent] stringByDeletingPathExtension];
    NSString *filePath = [resourceDocPath stringByAppendingPathComponent:[theFileName stringByAppendingString:@".pdf"]];
    [pdfData writeToFile:filePath atomically:YES];
    NSURL *url = [NSURL fileURLWithPath:filePath];
    [NSURLRequest requestWithURL:url];
    [webView setUserInteractionEnabled:YES];
    docController =[UIDocumentInteractionController interactionControllerWithURL:url];
    docController.delegate = self;
    [docController presentOpenInMenuFromBarButtonItem:sender animated:YES];
    [super viewDidLoad];
}

-(void)loading {

    if (!webView.loading)
        [_activityind stopAnimating];
    else
        [_activityind startAnimating]; 
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
于 2013-03-19T13:40:36.087 に答える