2013-03-05 29 views
1

好吧我一直在搜索和測試過去的幾周,我是這個xcode的新手,但我知道足以讓應用程序啓動並運行。所以我遇到的問題是增加了這個開放功能。我可以使用本地pdf(pdfs手動和本地加載應用程序)工作,但我似乎無法讓它適用於我目前在應用程序中查看的PDF。比方說,我提出了一個PDF,其中一個我沒有加載的應用程序,我甚至不知道URL是什麼,一個隨機的,我似乎無法弄清楚如何獲得開放的行動按鈕工作。繼承人我的代碼。哦,順便說一句,我試圖打開的Adobe Reader,再次我可以成功地做到與本地PDF格式,我安裝了應用程序和一切。請幫忙!對於在UIWebview上查看的任何PDF都是Open-In

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] 
} 

回答

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

在第二個方法(webView的一個),你正在尋找應用程序的包文件。但是你把它保存在應用程序的文件夾。 .not應用程序捆綁。刪除該行

,改變這一

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

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

奏效,但是當我按下它不保存正確的PDF按鈕,我有一個索引PDF具有多個環節分離的PDF文件,選擇一個,然後按下按鈕後它使我回到索引pdf,然後顯示按鈕在adobe中打開的工作,我只想保存我在屏幕上按下按鈕時所用的PDF,有什麼想法?感謝您的幫助,非常感謝。 – 2013-03-08 15:45:27

+0

我最終得到它的工作,請大拇指這樣每個人都可以看到如何讓這個工作 – 2013-03-19 13:32:06

0

發現場的粗野的幫助下回答,請豎起大拇指這件事讓每個人都可以看到如何獲得這工作,(因爲它真的花了我幾個月!)即時添加所有的代碼。

#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