无法在 iOS 中使用 loadHTMLString:baseURL 在 UIWebView 中加载 html 字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17155458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 10:12:08  来源:igfitidea点击:

Unable to load html string in UIWebView using loadHTMLString:baseURL in iOS?

htmliphoneiosuiwebview

提问by Bharath

I am trying to embed youtube video into my iOS application.For that I have created a UIWebView& trying to load the Youtubevideo from following here

我正在尝试将 youtube 视频嵌入到我的 iOS 应用程序中。为此,我创建了一个UIWebView& 尝试Youtube这里加载视频

I have gone through the all the answers for the above problem. Even then its not working. I have also tried loading very simple HTML

我已经完成了上述问题的所有答案。即使这样它也不起作用。我也试过加载非常简单的 HTML

 NSString *embedHTML =[NSString stringWithFormat:@"<html><body>Hello World</body></html>"];
 [webView loadHTMLString:embedHTML baseURL:nil];

Even then, I am getting compile error Parse Issue Expecte ']'

即使那样,我也会收到编译错误 Parse Issue Expecte ']'

I have tried cleaning, quitting the XCode & relaunching it again. I donno, I am not able to use that method. How to use the above loadHTMLStringmethod for my UIWebView.

我试过清理,退出 XCode 并重新启动它。我不,我不能使用那种方法。如何将上述loadHTMLString方法用于我的UIWebView.

PS : Please do not tag this question as duplicate. I have tried all the solutions in Stackoverflow. Nothing has worked

PS:请不要将此问题标记为重复。我已经尝试了Stackoverflow. 没有任何效果

回答by msk_sureshkumar

WebView *webDesc = [[UIWebView alloc]initWithFrame:CGRectMake(12, 50, 276, 228)];

NSString *embedHTML = @"<html><head></head><body><p>1. You agree that you will be the technician servicing this work order?.<br>2. You are comfortable with the scope of work on this work order?.<br>3. You understand that if you go to site and fail to do quality repair for  any reason, you will not be paid?.<br>4. You must dress business casual when going on the work order.</p></body></html>";

webDesc.userInteractionEnabled = NO;
webDesc.opaque = NO;
webDesc.backgroundColor = [UIColor clearColor];
[webDesc loadHTMLString: embedHTML baseURL: nil];

回答by Mitul Marsoniya

- (NSString *)getHTMLContent
{
    NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"baseline" ofType:@"css"];

    NSData *cssData = [NSData dataWithContentsOfFile:cssPath];
    NSString *cssStyle = [[NSString alloc] initWithData:cssData encoding:NSASCIIStringEncoding];

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    NSString *subtitle = [NSString stringWithFormat:@"%@ | %@", self.article.author, [dateFormatter stringFromDate:self.article.publishedDate]];

    NSString *htmlString = [NSString stringWithFormat:@"<html><head><meta name='viewport' content='width=device-width; initial-scale=1.0; maximum-scale=1.0;'></head><style type=\"text/css\">%@</style><body><div id=\"container\"><h1>%@</h1><p class='subtitle'>%@</p>%@</div></body></html>", cssStyle, self.article.title, subtitle, self.article.content];

    return htmlString;
}

回答by Patel Jigar

It is very simple. You just have to add only one line. Try It:

这很简单。您只需要添加一行。尝试一下:

NSString *htmlString = @"<html><head></head><body><p>1. You agree that you will be the technician servicing this work order?.<br>2. You are comfortable with the scope of work on this work order?.<br>3. You understand that if you go to site and fail to do quality repair for  any reason, you will not be paid?.<br>4. You must dress business casual when going on the work order.</p></body></html>";
[WebView loadHTMLString: htmlString baseURL: nil];

回答by Mike

You probably need to provide more code if you want people to help you. I just used webView in a similar way and it's working fine.

如果您希望人们帮助您,您可能需要提供更多代码。我只是以类似的方式使用了 webView,它运行良好。

self.webView = [[UIWebView alloc] initWithFrame:myFrame];
self.webView.scalesPageToFit = YES;
[self.webView setBackgroundColor:[UIColor whiteColor]];

//pass the string to the webview
[self.webView loadHTMLString:[[self.itineraryArray objectAtIndex:indexPath.row] valueForKey:@"body"] baseURL:nil];

//add it to the subview
[self.view addSubview:self.webView];

Can you provide more information and code?

你能提供更多的信息和代码吗?

回答by Lithu T.V

it doesnt seems like an issue on webview.Please do add the code where it breaks.Error says you missed a ]somewhere in the code

这似乎不是 webview 上的问题。请在它中断的]地方添加代码。错误说你错过了代码中的某个地方

回答by Rajath Kornaya

try to load the url directly into the webview :

尝试将 url 直接加载到 webview 中:

 UIWebView *webview=[[UIWebView alloc]initWithFrame:CGRectMake(100, 100, 200, 250)];

[webview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://youtube.com/embed/-0Xa4bHcJu8"]]];