gpt4 book ai didi

ios - 在 WKWebView objective c 中打开 PDF 文件

转载 作者:行者123 更新时间:2023-11-29 11:29:06 24 4
gpt4 key购买 nike

我在使用 WKWebView 和 objective c 可视化 pdf 文件的内容时遇到了问题。抱歉,我不熟悉 Swift。这是代码,但它显示空白页并返回以下错误:

Error Domain=NSCocoaErrorDomain Code=261“无法使用文本编码 Unicode (UTF-8) 打开文件“Sample1.pdf”。

NSString *filePath;
filePath = [[NSBundle mainBundle] pathForResource:@"Sample1" ofType:@"pdf"];

NSString *TextToBeDisplayed;
NSError *err = nil;
TextToBeDisplayed = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&err];

if (TextToBeDisplayed == nil)
{
NSLog(@"Case 1");
NSLog(@"Error reading %@: %@", filePath, err);
}
else
{
NSLog(@"Case 2");
NSLog(@"File found");
}

if(TextToBeDisplayed != nil || [TextToBeDisplayed isEqualToString:@""])
{
NSLog(@"Case 3");

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:TextToBeDisplayed]];

[webView setBackgroundColor:[UIColor whiteColor]];

[webView loadRequest:nsrequest];
[self.view addSubview:webView];
//[InstructionsTextView addSubview:webView];
}
else
{
NSLog(@"Case 4");
NSLog(@"Error");
//Error Domain=NSCocoaErrorDomain Code=261 "The file “Sample1.pdf” couldn’t be opened using text encoding Unicode (UTF-8).
}

最佳答案

你不需要这样做:

TextToBeDisplayed = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&err];

同样在这种方法中,你不会得到 NSURLRequest,而是从你的 PDF 文件中尝试转换为 NSURLRequest 的某种文本

NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL URLWithString:TextToBeDisplayed]];

你只需要这个:

    NSString *filePath;
filePath = [[NSBundle mainBundle] pathForResource:@"Sample1" ofType:@"pdf"];

WKWebViewConfiguration *theConfiguration = [[WKWebViewConfiguration alloc] init];
WKWebView *webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:theConfiguration];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]];


[webView setBackgroundColor:[UIColor whiteColor]];

[webView loadRequest:nsrequest];
[self.view addSubview:webView];

关于ios - 在 WKWebView objective c 中打开 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com