2011-11-17 1 views
0

pdf의 URL을 구문 분석하고 webview에 pdf를 표시했지만 webviews 내의 내 링크가 browser.i에서 열리지 않습니다. CGPDFDocument가 사용되지 않았습니다. 내 코드는 간단합니다 :). 아무도 나를 도울 수 있습니까? 나는 많은 비슷한 질문을 보았지만 모두 Quartz를 사용하고 있습니다.Webview의 Pdf 내부 링크가 iPhone의 브라우저에서 열리지 않습니다.

코드 : - 여기

@class AppDelegate_iPhone; 
@interface PdfShowViewController : UIViewController<UIWebViewDelegate> { 

    IBOutlet UIWebView *pdfWebview; 
    AppDelegate_iPhone *appDelegate; 
    NSMutableData *receivedData; 
    IBOutlet UIActivityIndicatorView *myIndicator; 
    IBOutlet UIProgressView *progress; 

    NSURLRequest* DownloadRequest; 
    NSURLConnection* DownloadConnection; 

    long long bytesReceived; 
    long long expectedBytes; 
    IBOutlet UILabel *downloadLabel; 

} 

@property (nonatomic,retain) IBOutlet UILabel *downloadLabel; 
@property (nonatomic,retain) IBOutlet UIWebView *pdfWebview; 
@property (nonatomic,retain) IBOutlet UIActivityIndicatorView *myIndicator; 
@property (nonatomic,retain) IBOutlet UIProgressView *progress; 
@property (nonatomic,retain) NSMutableData *receivedData; 
@property (nonatomic, readonly, retain) NSURLRequest* DownloadRequest; 
@property (nonatomic, readonly, retain) NSURLConnection* DownloadConnection; 
@property (nonatomic, retain, readwrite) NSURL *openURL; 


-(IBAction)onTapBack; 

@end 



@implementation PdfShowViewController 

@synthesize pdfWebview,myIndicator,progress,receivedData,DownloadRequest,DownloadConnection,downloadLabel,openURL; 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 
    [receivedData appendData:data]; 

    unsigned char byteBuffer[[receivedData length]]; 
    [receivedData getBytes:byteBuffer]; 
    NSLog(@"Data === %ld",receivedData); 

    NSInteger receivedLen = [data length]; 
    bytesReceived = (bytesReceived + receivedLen); 
    NSLog(@"received Bytes == %f",bytesReceived); 

    if(expectedBytes != NSURLResponseUnknownLength) 
    { 
     NSLog(@"Expected Bytes in if == %f",expectedBytes); 
     NSLog(@"received Bytes in if == %f",bytesReceived); 

     float value = ((float) (bytesReceived *100/expectedBytes))/100; 
     NSLog(@"Value == %f",value); 
     progress.progress=value; 
    } 

} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 
    [connection release]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { 
    expectedBytes = [response expectedContentLength]; 
    NSLog(@"%f",expectedBytes); 

} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    [myIndicator stopAnimating]; 
    [myIndicator removeFromSuperview]; 
    [progress setHidden:YES]; 
    [downloadLabel setHidden:YES]; 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:@"iPhonePdf.pdf"]; 

    unsigned char byteBuffer[[receivedData length]]; 
    [receivedData getBytes:byteBuffer]; 

    [self.receivedData writeToFile:pdfPath atomically:YES]; 

    [DownloadConnection release]; 

    //Now create Request for the file that was saved in your documents folder 

    NSURL *url = [NSURL fileURLWithPath:pdfPath]; 

    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 

    [pdfWebview setScalesPageToFit:YES]; 
    [pdfWebview loadRequest:requestObj]; 

} 

-(BOOL)webView:(UIWebView *)webView1 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSURL *requestURL = [request URL]; 
    if(navigationType==UIWebViewNavigationTypeLinkClicked) 
    { 
     [[UIApplication sharedApplication] openURL:requestURL]; 
     return NO; 
    } 
    else 
    { 
     return YES; 
    } 
} 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    pdfWebview.delegate = self; 

    appDelegate = (AppDelegate_iPhone *)[[UIApplication sharedApplication] delegate]; 

    [downloadLabel setText:@"Downloading..."]; 
    [downloadLabel setHidden:NO]; 

    [myIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    myIndicator.hidesWhenStopped = YES; 
    [myIndicator startAnimating]; 

    // NSString *urlString = [appDelegate.currentBookPressed stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]; 
    NSString *urlString = [appDelegate.currentBookPressed stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 

// NSLog(@"The Url Stirng=======%@",urlString); 

    NSURL *targetURL = [NSURL URLWithString:urlString]; 
    //NSLog(@"Trageted String ------======++++++++%@",targetURL); 
    DownloadRequest = [NSURLRequest requestWithURL:targetURL cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:1200.0]; 
    DownloadConnection = [[NSURLConnection alloc] initWithRequest:DownloadRequest delegate:self]; 

    if (DownloadConnection) { 
     receivedData = [[NSMutableData data]retain]; 
    } 

    [pdfWebview setScalesPageToFit:YES]; 
    [pdfWebview loadRequest:DownloadRequest]; 


} 

-(IBAction)onTapBack 
{ 
    [self dismissModalViewControllerAnimated:YES]; 
} 



@end 

내가 열려고하지만 개방하고 있지 않다 링크입니다 : -

enter image description here

답변

2

아니요, 탭을 누르면 UIWebView에로드 된 PDF 파일 내의 링크가 열리지 않습니다. 기본적으로 m입니다.

this other post I answered에 표시된 것처럼 Quartz를 사용하여 어려운 방법으로 링크를 파싱 할 수 있습니다.

또는 pdf를로드하는 대신로드하는 컨텐츠를 html 파일로 변환 할 수 있습니까? 그게 더 쉬울거야, 그리고 링크가 작동합니다.

+0

안녕하세요, 회신 주셔서 감사합니다 .... 그 훌륭한 제안,하지만 어떻게 내가 HTML로 PDF로 변환 할 수 있을지 궁금 ... 나는 서버에서 PDF의 URL을 가져 오는 중 .... 사실 내 요구 사항은 아이폰에 서버에 업로드 된 클라이언트의 일부 문서를 표시하는 것입니다. 그래서 나는 그들을 PDF 파일로 선택합니다 .. 다른 방법이 있습니까? – mAc

+0

그럼, 대신 HTML이되도록 선택할 수 있습니까? – Altealice

+0

안녕하세요 당신이 옳은 대답으로 똑딱 거리는데 도움이되었으므로, 내가 원했던 방식대로 할 수는 없다고 생각합니다. – mAc

2

당신은 대리자 메서드 아래 사용해야

-(BOOL)webView:(UIWebView *)webView1 shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    NSURL *requestURL = [request URL] ; 
    if(navigationType==UIWebViewNavigationTypeLinkClicked) 
    { 
    [[UIApplication sharedApplication] openURL:requestURL]; 
    return NO; 
    } 
    else 
    { 
    return YES; 
    } 
} 
+0

고마워 ...하지만 작동하지 않는 이유는 무엇을 구현했는지 조금 설명 할 수 있습니까? :) – mAc

+0

이 대리자 메서드는이 메서드 호출을 요청할 때마다 webview의 모든 액션을 호출하고 모든 링크를 클릭하면 여기로 이동합니다. UIWebViewNavigationType – Narayana

+0

실제로 내 PDF를로드하기 시작할 때 두 번째로 호출되고 두 번째로 내 PDF를로드하면이 호출됩니다. ....하지만 내가 링크를 클릭하면이 메서드를 호출하지 않습니다 .... 사실 링크를 인식하고 있지 않습니다. 참조하십시오 또한 링크 이미지를 업로드 오전. – mAc

관련 문제