我在本地资源文件夹中有pdf文件我想用UIWebView打开这个文件,用户可以阅读。但PDF文件无法显示以供阅读。
NSString *pathToBundle = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:pathToBundle];
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"Insupen_User_Guide" ofType:@"pdf"];
NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
//CGRect fullScreenRect=[[UIScreen mainScreen]applicationFrame];
catalogue=[[UIWebView alloc]initWithFrame:CGRectMake(50,400,600,600)];
[catalogue loadHTMLString:htmlString baseURL:baseURL];
catalogue.delegate=self;
[self.view addSubview:catalogue];
答案 0 :(得分:0)
试试这个。
NSString *strPath = [[NSBundle mainBundle]pathForResource:@"Insupen_User_Guide" ofType:@"pdf"];
[catalogue loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[strPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]]];
也许这会对你有帮助。
答案 1 :(得分:0)
NSString *path = [[NSBundle mainBundle]pathForResource:@"HR" ofType:@"pdf"];
NSURL *targeturl = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:targeturl];
// _webview = [[UIWebView alloc]init];
[[_webview scrollView] setContentOffset:CGPointMake(0, 500) animated:YES];
[_webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"window.scrollTo(0.0, 50.0)"]];
[_webview loadRequest:request];
//完美地为我工作,你可以试试这个
答案 2 :(得分:0)
您还可以使用此功能在UIDocumentInteractionController
中显示PDF。
UIDocumentInteractionController *docController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"PDF file name" ofType:@"pdf"]]];
docController.delegate = self;
//[DocView addSubview:docController];
[docController presentPreviewAnimated:YES];
并在您的课程中添加这些委托方法。
#pragma mark documentInteractionController methods
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
- (UIView *)documentInteractionControllerViewForPreview:(UIDocumentInteractionController *)controller {
return self.view;
}
- (CGRect)documentInteractionControllerRectForPreview:(UIDocumentInteractionController *)controller {
return self.view.frame;
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action{
return FALSE;
}
答案 3 :(得分:0)
在您发布的代码中,您正在将PDF文件的内容转换为UTF8编码的字符串,并在Web视图中显示字符串,这不是您想要的。
相反,您需要创建指向本地PDF的NSURLRequest并在Web视图中加载请求
NSString *pdfPath = [[NSBundle mainBundle] pathForResource:@"Insupen_User_Guide" ofType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pdfPath];
NSURLRequest *pdfUrlRequest = [NSURLRequest requestWithURL:pdfUrl];
[catalogue loadRequest:pdfUrlRequest];