【问题标题】:Links inside Pdf on Webview are not opening in a browser in iPhoneWebview 上 Pdf 内的链接未在 iPhone 的浏览器中打开
【发布时间】:2011-12-31 06:01:08
【问题描述】:

我已经解析了 pdf 的 url 并在 webView 上显示了 pdf,但是我在 webview 中的链接没有在浏览器中打开。我没有使用 CGPDFDocument。我的代码很简单:)。谁能帮帮我。我见过很多类似的问题,但都在使用 Quartz。

代码:-

@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

这是我试图打开但未打开的链接:-

【问题讨论】:

    标签: iphone objective-c ios4 pdf uiwebview


    【解决方案1】:

    不,加载到 UIWebView 中的 pdf 文件中的链接默认情况下不会在您点击它们时打开。

    您可以像this other post I answered 中所示的那样,使用 Quartz 将链接解析出来。

    或者,您可以将正在加载的内容转换为 html 文件,而不是加载 pdf 文件吗?这样会更容易,然后链接应该可以工作。

    【讨论】:

    • 嘿,谢谢你的回复......这是一个很好的建议,但我想知道如何将 pdf 转换为 html......因为我正在从服务器获取 pdf 的 url......实际上我的要求是在 iPhone 上显示客户端上传到服务器的一些文件。所以我选择它们是pdf的。有没有其他办法..??
    • 那么,您可以选择将它们改为 HTML 吗?
    • 嘿,我想,这不可能按照我想要的方式完成,但是当你帮助我时,我将你的答案打勾为正确。
    【解决方案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;
       }
    }
    

    【讨论】:

    • 谢谢老兄......但是你能不能解释一下你已经实现了这个,因为它不起作用。 :)
    • 如果你加载任何请求这个方法调用和任何链接点击你会到达这里 UIWebViewNavigationType,这个委托方法将调用 webview 中的每个动作
    • 实际上,当我开始加载我的 pdf 时,它被调用了两次,第二次在加载我的 pdf 之后被调用了......但是当我点击任何链接时,它并没有调用这个方法......实际上它无法识别链接。请参阅我也在上传链接图片。
    猜你喜欢
    • 1970-01-01
    • 2013-05-22
    • 2014-07-12
    • 2018-02-11
    • 2014-03-12
    • 2017-08-06
    • 1970-01-01
    • 2021-09-23
    • 1970-01-01
    相关资源
    最近更新 更多