在UIWebview上查看的任何PDF的Open-In

时间:2013-03-05 15:45:23

标签: objective-c uiwebview

好吧我过去几周一直在搜索和测试,我是这个xcode的新手,但我知道足够让应用程序启动并运行。所以我遇到的问题是添加这个开放式功能。我可以使用本地pdf(pdfs我手动和本地加载应用程序),但我似乎无法让它适用于我目前在应用程序上查看的PDF。可以说我提出了一个PDF,其中一个我没有在应用程序上加载,我甚至不知道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] 
}

2 个答案:

答案 0 :(得分:2)

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

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

并更改此

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

docController =[UIDocumentInteractionController interactionControllerWithURL: url]; // url is of the file that you saved by downloading.

答案 1 :(得分:0)

在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