UIWebview 展示一个 xml 文件。使用了NSURLRequest 但是 UIWebview 显示一片空白。我把代码贴到下面了,不知道有方法能从网站获取文件然后显示吗?
NSString *htmlFile=[ [NSBundle mainBundle] pathForResource:@"location" ofType:@"xml"];
NSData * htmlData=[NSData dataWithContentsOfFile:htmlFile];
[aWebView loadData:htmlData MIMEType:@"text/text" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
如果你的 htmlFile 路径是正确的,你的代码应该能够正常工作。
但是,你给出的 MIMEType 是 "text/text",这是不正确的。MIME 类型是一种 Internet 标准,用于指定文件的类型和编码。
XML 文件的 MIME 类型是 "text/xml" 或 "application/xml"。
因此,你应该将代码修改为:
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"location" ofType:@"xml"];
NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
[aWebView loadData:htmlData MIMEType:@"text/xml" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
这样,你的 XML 文件应该能够在 UIWebView 中正常显示。