1

好吧,过去几周我一直在搜索和测试,我是这个 xcode 的新手,但我知道足以让应用程序启动并运行。所以我一直遇到的问题是添加这个开放功能。我可以让它与本地 pdf 一起工作(我在应用程序上手动和本地加载的 pdf),但我似乎无法让它适用于我目前在应用程序上查看的 PDF。假设我打开了一个 PDF,其中一个我没有加载到应用程序上,我什至不知道 URL 是什么,一个随机的,我似乎无法弄清楚如何获得打开操作按钮工作。这是我的代码。哦,顺便说一句,我正在尝试打开 Adob​​e 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:];零字符串参数'

- (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"];

在第二种方法(webView 一)中,您正在搜索应用程序包中的文件..但是您将其保存在应用程序的文档文件夹中..不是应用程序包。删除该行

并改变这个

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

当然,在 Shaggy 的帮助下找到了答案,请竖起大拇指,这样每个人都可以看到如何让它工作,(因为它确实花了我几个月的时间!)我添加了所有代码。

#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 回答