2012-05-11 26 views
2

我使用以下代碼在我的UiWebView上顯示本地html文件。如何鏈接html文件上的本地pdf文件 - 該html文件在UIWebView上顯示

NSString *path = [[NSBundle mainBundle] pathForResource:@"test.html"]; 

NSString* htmlString = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];  

NSString *bundlePath = [[NSBundle mainBundle] bundlePath]; 
NSURL *baseURL = [NSURL fileURLWithPath:bundlePath]; 

[_webView loadHTMLString:htmlString baseURL:baseURL]; 
[_webView setBackgroundColor:[UIColor clearColor]]; 

應用被正確顯示的HTML。在test.html中有一個到本地pdf文件hello.pdf的鏈接。這個pdf被添加到項目中。但是當我點擊鏈接時,什麼也沒有發生。我想在用戶點擊鏈接時在我的web視圖中加載pdf。

我想使用UIWebView委託向互聯網超鏈接(例如http://www.google.com)發送請求到Safari瀏覽器應用程序。

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType 
{ 
    NSLog(@"%@",inRequest.URL.absoluteString); 
    if([inRequest.URL.absoluteString rangeOfString:@"hello.pdf"].location == NSNotFound) 
    { 
     if (inType == UIWebViewNavigationTypeLinkClicked) 
     { 
      [[UIApplication sharedApplication] openURL:[inRequest URL]]; 
      return NO; 
     } 
     return YES; 
    } 
    else 
    { 
     return NO; 
    } 
} 
+0

鏈接是什麼樣的?它只是href ='document.pdf'? – BBog

+0

是的。簡單

回答

0

你應該說明更清楚,你想互聯網的超鏈接在Safari和地方超鏈接來加載在你的UIWebView加載。

我剛剛測試這和它你想要做什麼:

-(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType 
{ 
    if (inType == UIWebViewNavigationTypeLinkClicked) { 
     if ([inRequest.URL.absoluteString rangeOfString:@"http://"].location == NSNotFound) { 
      return YES; 
     } 
     else { 
      [[UIApplication sharedApplication] openURL:[inRequest URL]]; 
      return NO; 
     } 
    } 
    else { 
     return YES; 
    } 
} 

這是通過在UIWebView只加載鏈接不包含超文本類型協議前綴(HTTP://) 。

提示:確保您已將UIWebView的代理設置爲viewDidLoad方法。

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    webView.delegate = self; 
} 
+0

我不知道如何在我的問題上提供更多解釋。但是你說得對,我不應該在我的其他人身上回復NO。在此基礎上,我接受你回答 –

+0

我對原始問題進行了編輯。您並未完全清楚您希望在Safari瀏覽器應用程序中打開超鏈接。 「我想在用戶點擊鏈接時在我的網頁視圖中加載PDF。BTW當用戶點擊鏈接(http://www.google.com)時,我使用UIWebView代理來發送用戶到網站」< - 發送一個用戶訪問網站並不意味着開放Safari瀏覽器!雖然你原諒了:) –