2014-04-29 173 views
1

我三合一點擊PDF文件中的URL。和PDF文件在UIWebView中打開,但我不能點擊URL。如何打開PDF文件中的url

任何一個告訴我如何打開PDF文件中的URL。

UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 20, 320, 460)]; 
NSURL *targetURL = [NSURL URLWithString:@"http://partners.adobe.com/public/developer/en/acrobat/PDFOpenParameters.pdf"]; 
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL]; 
[webView loadRequest:request]; 

[self.view addSubview:webView]; 
+0

你是在iOS中自己創建PDF文件還是創建它? –

+0

它被創建... – Mihin

+0

我想打開任何PDF文件的URL ... – Mihin

回答

2

文件中,你webView的deleget方法,首先檢測URL從PDF,然後打開在web視圖等。我用下面的方法來做到這一點,可能是它的幫助你:

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
     NSURL *requestURL = [[ request URL] retain]; 

     if (([[requestURL scheme] isEqualToString: @"http"] || [[requestURL scheme] isEqualToString: @"https"]) && (navigationType == UIWebViewNavigationTypeLinkClicked)) 
     { 
      // Your method when tap on url found in PDF 
     } 

    } 
0

如果你在本地存儲

NSString *html = [NSString stringWithContentsOfFile:path1 encoding:NSUTF8StringEncoding error:nil]; 
[self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]bundlePath]]]; 

一個PDF文件,如果你有機會從互聯網

- (void) loadRemotePdf 
{ 
     CGRect rect = [[UIScreen mainScreen] bounds]; 
     CGSize screenSize = rect.size; 

     UIWebView *myWebView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0,screenSize.width,screenSize.height)]; 
     webView.autoresizesSubviews = YES; 
     webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth); 

     NSURL *myUrl = [NSURL URLWithString:@"http://www.mysite.com/test.pdf"]; 
     NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl]; 

     [webView loadRequest:myRequest]; 

     [window addSubview: myWebView]; 
     [myWebView release]; 

} 
+0

爲了讓您知道'API'代表'Application Programming Interface'並且您的代碼從不訪問'API'來獲取'PDF',所以我已經改回互聯網這是它從中獲得的地方。 – Popeye

+0

實際上第二次查看問題和答案,甚至沒有回答他們已經問過的-1。 – Popeye

+0

這不是答案,他/她想要。 –